Click or drag to resize
Pdf Library for .NET

PdfDocument Class

Represents a PDF document. Can be used to create a new empty PDF document or to load an existing one.
Inheritance Hierarchy
SystemObject
  SelectPdf.UniversalPdfDocument

Namespace: SelectPdf.Universal
Assembly: SelectPdf.HtmlToPdf.Universal (in SelectPdf.HtmlToPdf.Universal.dll) Version: 26.4
Syntax
public class PdfDocument

The PdfDocument type exposes the following members.

Constructors
 NameDescription
Public methodPdfDocument Initializes a new instance of the PdfDocument class, creating an empty PDF document.
Public methodCode examplePdfDocument(PdfStandard) Creates an empty PDF document that conforms to the specified PDF standard — use this when you want from-scratch output to be PDF/A (archival) or PDF/X (print exchange) compliant. Equivalent to PdfDocument when pdfStandard is Full.
Top
Properties
 NameDescription
Public propertyBookmarks Gets the document-level bookmark (outline) collection. Use this to add navigation entries that appear in the PDF reader's outline pane.
Public propertyCompressionLevel Gets or sets the Flate/DEFLATE compression level applied to content streams, fonts, and non-image objects in the PDF. Complements JpegCompressionEnabled, which handles image XObjects. Default is Normal.
Public propertyDocumentInformation Gets the document information properties (title, author, subject, etc.).
Public propertyFonts Gets the document-level font collection. Use doc.Fonts.Add(...) to register fonts (standard, CJK, or embedded TrueType) that can be retrieved later by index and reused across PdfTextElement instances. For PDF/A output from scratch, add a TrueType font via Add(String) or Add(Byte) — the 14 built-in PdfStandardFont values are not allowed under PDF/A and will throw a PdfDocumentException when used.
Public propertyFooter Gets or sets the template repeated at the bottom of every page of the pdf document.
Public propertyHeader Gets or sets the template repeated at the top of every page of the pdf document.
Public propertyJpegCompressionEnabled Gets or sets a flag indicating if JPEG compression is applied to the images in the generated pdf document. Default is true.
Public propertyJpegCompressionLevel Gets or sets the JPEG compression level for images in the generated pdf document. Value range: 0 (lowest compression, best quality) to 100 (highest compression, worst quality). Only effective when JpegCompressionEnabled is true. Default is 10.
Public propertyLanguage Gets or sets the natural language of the document content (for example "en-US"), written as the document /Lang entry. Required for accessible / PDF/UA output.
Public propertyLFN Gets or sets the path of the diagnostic log file written while html content is rendered into this document. It is passed to the html elements added to the document's pages, and to the templates obtained through AddTemplate(Single, Single), Header and Footer. A template or html element that has a log file of its own keeps it. When none is set, no log is written.
Public propertyMargins The default pdf document margins. New pages will use the default margins if no margins are specified when the page is added.
Public propertyPages Gets the collection of pages in the document.
Public propertySecurity Gets the security settings of the document.
Public propertyTemplates Gets the collection of reusable templates of the document. A template is repeated on more than one page (headers, footers, watermarks, page numbers). Routed onto the document at save time.
Public propertyTitle Gets or sets the document title. For a tagged document this is written as the title metadata and, together with DisplayDocTitle, is required for PDF/UA conformance.
Public propertyViewerPreferences Gets the viewer-preferences block that controls how the document appears in a pdf reader — which panel opens (bookmarks, thumbnails, full-screen), whether the reader's tool bar / menu bar / window UI is hidden, whether the window resizes to the first page, and so on. Mutations write through to the underlying PDF immediately. Works on every document origin — newly-created documents, loaded documents, and HtmlToPdf output.

For the HtmlToPdf pipeline, ViewerPreferences is the pre-conversion knob; the two are complementary — set options before conversion, then fine-tune the returned document via this property.

Top
Methods
 NameDescription
Public methodAddAssociatedFile(String, PdfAttachmentRelationship, String, String) Embeds a file from disk into the document as an associated file.
Public methodAddAssociatedFile(Stream, String, PdfAttachmentRelationship, String, String) Embeds the content of a stream into the document as an associated file.
Public methodAddBookmark(String, PdfDestination) Adds a new bookmark to the document outline. Shorthand for doc.Bookmarks.Add(text, destination).
Public methodAddBookmark(String, PdfDestination, PdfBookmark) Adds a new bookmark as a child of the specified parent bookmark. Shorthand for parent.ChildNodes.Add(text, destination).
Public methodAddFont(PdfStandardCJKFont) Adds one of the standard CJK pdf fonts to the pdf document.
Public methodAddFont(PdfStandardFont) Adds one of the standard pdf fonts to the pdf document.
Public methodAddFont(PdfSystemFont) Adds a system font to the pdf document. The font is always embedded.
Public methodAddFont(String) Adds a font loaded from a TrueType / OpenType file to the pdf document.
Public methodAddFont(PdfSystemFont, Boolean) Adds a system font to the pdf document.
Public methodCode exampleAddPage Adds a new page to the document. Shorthand for Add.
Public methodAddPage(PdfMargins) Adds a new page with the specified margins.
Public methodAddPage(PdfPage) Adds an existing PdfPage (for example, from another document) to this document.
Public methodAddPage(PdfCustomPageSize, PdfMargins) Adds a new page with the specified size and margins.
Public methodAddPage(PdfCustomPageSize, PdfMargins, PdfPageOrientation) Adds a new page with the specified size, margins, and orientation.
Public methodAddTemplate(RectangleF) Creates a template with the specified bounds and adds it to the pdf document.
Public methodAddTemplate(Single, Single) Creates a template with the specified dimensions and adds it to the pdf document.
Public methodCode exampleAppend(PdfDocument) Appends all pages from another PDF document to this document. The appended document must remain open until this document is saved.
Public methodAppend(Stream) Appends all pages from a PDF stream to this document.
Public methodAppend(String) Appends all pages from a PDF file to this document.
Public methodClose Closes the document and releases all resources.
Public methodClose(Boolean) Closes the document. When completely is true, all resources are released.
Public methodDetachStream Detaches the stream associated with the document.
Public methodStatic memberGetPagesCount(Stream) Gets the number of pages in a PDF document loaded from a stream.
Public methodStatic memberGetPagesCount(String) Gets the number of pages in a PDF file without fully loading the document.
Public methodInsertPage(Int32, PdfPage) Inserts an existing PdfPage at the specified index in this document.
Public methodInsertPage(Int32, PdfCustomPageSize, PdfMargins, PdfPageOrientation) Creates a new page and inserts it at the specified index with the given size, margins, and orientation.
Public methodRemovePage Removes the specified page from the document.
Public methodRemovePageAt Removes the page at the specified index.
Public methodSave Saves the PDF document and returns the content as a byte array.
Public methodSave(Stream) Saves the PDF document to a stream.
Public methodCode exampleSave(String) Saves the PDF document to a file.
Top
Example
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
document.DocumentInformation.Title = "Report";
document.Save("out.pdf");
document.Close();
See Also