PdfPrintExtensions Class

Prints the documents of a browsing context into PDF documents (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 a page of the PDF document, drawn with vector graphics and text.

public static class PdfPrintExtensions

Remarks

The text is shown with the font files the layout used, embedded as subsets, at the positions the layout computed: it can be selected and extracted. The images are embedded (the JPEG images as they are). What PDF cannot draw (blurred shadows, filters, some compositing operators, 3D transforms, conic gradients, color glyphs) is drawn as images.

The printing does not change the document. As for the screenshots, call it from the thread of the DomEventLoop of the browsing context, after the resources are loaded. The images are only embedded when the browsing context has a BrowsingContextOptions.RenderingBackend (which keeps their bytes), such as the ImagingRenderingBackend of Meziantou.Framework.WebPlatform.Imaging.

Methods

NameDescription
PrintToPdf(BrowsingContext, PrintOptions?, PdfPrintOptions?)Prints the active document of a browsing context into a PDF document.
PrintToPdf(BrowsingContext, Stream, PrintOptions?, PdfPrintOptions?)Prints the active document of a browsing context into a PDF file.
PrintToPdf(Document, PrintOptions?, PdfPrintOptions?)Prints a document into a PDF document.
PrintToPdf(Document, Stream, PrintOptions?, PdfPrintOptions?)Prints a document into a PDF file.

PrintToPdf overloads

PrintToPdf(BrowsingContext, PrintOptions?, PdfPrintOptions?)

PdfPrintExtensions.cs

Prints the active document of a browsing context into a PDF document.

public static PdfDocument PrintToPdf(this BrowsingContext browsingContext, PrintOptions? printOptions = null, PdfPrintOptions? options = null)
Parameters
browsingContext BrowsingContext
The browsing context.
printOptions PrintOptions?
The page size and the default margins, or null for PrintOptions.Default.
options PdfPrintOptions?
The options of the PDF document, or null for PdfPrintOptions.Default.
Returns

PdfDocument The PDF document, with one page per printed page. Save it with PdfDocument.Save.

Exceptions
InvalidOperationException
The document is not rendered, or it is already being printed.

PrintToPdf(BrowsingContext, Stream, PrintOptions?, PdfPrintOptions?)

PdfPrintExtensions.cs

Prints the active document of a browsing context into a PDF file.

public static void PrintToPdf(this BrowsingContext browsingContext, Stream destination, PrintOptions? printOptions = null, PdfPrintOptions? options = null)
Parameters
browsingContext BrowsingContext
The browsing context.
destination Stream
The stream the PDF file is written to.
printOptions PrintOptions?
The page size and the default margins, or null for PrintOptions.Default.
options PdfPrintOptions?
The options of the PDF document, or null for PdfPrintOptions.Default.
Exceptions
InvalidOperationException
The document is not rendered, it is already being printed, or it does not meet the requirements of the conformance of options.

PrintToPdf(Document, PrintOptions?, PdfPrintOptions?)

PdfPrintExtensions.cs

Prints a document into a PDF document.

public static PdfDocument PrintToPdf(this Document document, PrintOptions? printOptions = null, PdfPrintOptions? 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 PdfPrintOptions?
The options of the PDF document, or null for PdfPrintOptions.Default.
Returns

PdfDocument The PDF document, with one page per printed page. Save it with PdfDocument.Save.

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

PrintToPdf(Document, Stream, PrintOptions?, PdfPrintOptions?)

PdfPrintExtensions.cs

Prints a document into a PDF file.

public static void PrintToPdf(this Document document, Stream destination, PrintOptions? printOptions = null, PdfPrintOptions? options = null)
Parameters
document Document
The document, which must be the active document of a browsing context.
destination Stream
The stream the PDF file is written to.
printOptions PrintOptions?
The page size and the default margins, or null for PrintOptions.Default.
options PdfPrintOptions?
The options of the PDF document, or null for PdfPrintOptions.Default.
Exceptions
InvalidOperationException
The document is not rendered, it is already being printed, or it does not meet the requirements of the conformance of options.