ImagingPrintExtensions Class

Prints the documents of a browsing context into pages (https://drafts.csswg.org/css-page-3/): the document is laid out with the print media type and paginated (the page size, margins and page names of the @page rules, the page breaks), and each page is rendered into its own image.

public static class ImagingPrintExtensions

Remarks

The printing does not change the document: its styles and its layout are computed by other engines while it prints, so the screen is neither styled nor laid out again afterwards (no transition starts, no event is fired). As for the screenshots, call it from the thread of the DomEventLoop of the browsing context, after the resources are loaded. The images and the fonts that only the print styles use (e.g. in @media print) are fetched by the printing, but they are not drawn by it when they were not loaded before: print the document again once they are.

Methods

NameDescription
RenderPages(BrowsingContext, PrintOptions?, ScreenshotOptions?)Prints the active document of a browsing context and renders each page.
RenderPages(Document, PrintOptions?, ScreenshotOptions?)Prints a document and renders each page.
RenderPagesAsPng(BrowsingContext, PrintOptions?, ScreenshotOptions?)Prints the active document of a browsing context and renders each page, encoded as PNG.
RenderPagesAsPng(Document, PrintOptions?, ScreenshotOptions?)Prints a document and renders each page, encoded as PNG.

RenderPages overloads

RenderPages(BrowsingContext, PrintOptions?, ScreenshotOptions?)

ImagingPrintExtensions.cs

Prints the active document of a browsing context and renders each page.

public static IReadOnlyList<Image<Rgba32>> RenderPages(this BrowsingContext browsingContext, PrintOptions? printOptions = null, ScreenshotOptions? options = null)
Parameters
browsingContext BrowsingContext
The browsing context.
printOptions PrintOptions?
The page size and the default margins, or null for PrintOptions.Default.
options ScreenshotOptions?
The options of the images (the device pixel ratio, the omission of the white paper), or null for the default options.
Returns

IReadOnlyList<Image<Rgba32>> One image per page, of the size of its page box in device pixels. The caller owns them.

Exceptions
InvalidOperationException
The document is not rendered.

RenderPages(Document, PrintOptions?, ScreenshotOptions?)

ImagingPrintExtensions.cs

Prints a document and renders each page.

public static IReadOnlyList<Image<Rgba32>> RenderPages(this Document document, PrintOptions? printOptions = null, ScreenshotOptions? options = null)
Parameters
document Document
The document, which must be the active document of a browsing context.
printOptions PrintOptions?
The page size and the default margins, or null for PrintOptions.Default.
options ScreenshotOptions?
The options of the images (the device pixel ratio, the omission of the white paper), or null for the default options.
Returns

IReadOnlyList<Image<Rgba32>> One image per page, of the size of its page box in device pixels. The caller owns them.

Exceptions
InvalidOperationException
The document is not rendered (it has no browsing context), or it is already being printed.

RenderPagesAsPng overloads

RenderPagesAsPng(BrowsingContext, PrintOptions?, ScreenshotOptions?)

ImagingPrintExtensions.cs

Prints the active document of a browsing context and renders each page, encoded as PNG.

public static IReadOnlyList<byte[]> RenderPagesAsPng(this BrowsingContext browsingContext, PrintOptions? printOptions = null, ScreenshotOptions? options = null)
Parameters
browsingContext BrowsingContext
printOptions PrintOptions?
options ScreenshotOptions?
Returns

IReadOnlyList<byte[]> The bytes of the PNG image of each page.

Exceptions
InvalidOperationException
The document is not rendered.

RenderPagesAsPng(Document, PrintOptions?, ScreenshotOptions?)

ImagingPrintExtensions.cs

Prints a document and renders each page, encoded as PNG.

public static IReadOnlyList<byte[]> RenderPagesAsPng(this Document document, PrintOptions? printOptions = null, ScreenshotOptions? options = null)
Parameters
document Document
printOptions PrintOptions?
options ScreenshotOptions?
Returns

IReadOnlyList<byte[]> The bytes of the PNG image of each page.

Exceptions
InvalidOperationException
The document is not rendered (it has no browsing context), or it is already being printed.