Click or drag to resize

Document Class

This class represents a PDF document in the HTML to PDF Converter framework. A Document object can be received as result of one of the render methods described in the Post Convert Customization Overview section. When creating a Document object from an existing PDF document, the existing PDF document is passed to the Document class constructor as the path of the .pdf file on disk or as a stream containing the PDF document binary content. In either case the resulted Document object can be further modified. There are also Document constructors accepting a password when creating a Document object from a password protected PDF document. Here are the Document class constructors:

public Document(Stream pdfStream);
public Document(string pdfFileName);
public Document(Stream pdfStream, string pdfPassword);
public Document(string pdfFileName, string pdfPassword);

The Document class offers access to the PDF pages collection, PDF fonts collection, bookmarks, templates and viewer preferences. For example you can add a new page to the document pages collection using the AddPage method, adding a new font to the fonts collection using the AddFont() method or adding a new template using the AddTemplate() method.

Page labels

Page labels are the page numbers a PDF viewer shows and accepts in its page box instead of the page positions - for example i, ii, iii for the front pages of a report and 1, 2, 3 for its body, or A-1, A-2 for an appendix. They do not change the pages. AddPageLabelRange starts a range at a page position: a PageLabelStyle (decimal, roman or letters, or no number), an optional prefix and the number of the first page of the range. A range ends where the next one starts; the pages before the first range are numbered 1, 2, 3. GetPageLabel(Int32) returns the label of a page and ClearPageLabels removes the ranges.

PdfConverter converter = new PdfConverter();
Document document = converter.GetPdfDocumentObjectFromUrl("https://www.example.com/report");

// i, ii, iii for the first three pages, then 1, 2, 3..., and A-1, A-2... for the appendix that starts on page 8
document.AddPageLabelRange(0, PageLabelStyle.LowercaseRoman);
document.AddPageLabelRange(3, PageLabelStyle.Decimal);
document.AddPageLabelRange(7, PageLabelStyle.Decimal, "A-", 1);

document.Save("labels.pdf");
document.Close();

The ranges refer to the page positions when the document is saved: add them after the pages are added or removed. Page labels can be used in PDF/A documents.

Named destinations

A named destination is a place in the document that a web page or another document can link to by name, with the name after # in the address of the document: report.pdf#summary. AddNamedDestination(String, ExplicitDestination) adds one for an ExplicitDestination (a page, a point on the page and how the page is displayed). The name has up to 127 characters: ASCII letters, digits and the characters - _ . :

// the summary starts 200 points down the second page
document.AddNamedDestination("summary", new ExplicitDestination(document.Pages[1], new PointF(0, 200)));
Table of contents

A TableOfContents lists headings with the numbers of the pages they are on, each entry a link to its heading. InsertTableOfContents(TableOfContents, Int32) inserts it at a page position, on as many pages as the entries need, and returns the number of pages inserted. Add the entries after the content: AddEntries(Document, HtmlElementMappingsCollection) takes the headings an HTML conversion mapped (HtmlElementsMappingOptions with the "h1", "h2"... selectors; h1 is level 1, h2 level 2), and AddEntry(String, Int32, PdfPage, Single) adds an entry of your own.

The page numbers are the page labels when the document has page label ranges. Ranges that start at or after the position where the table is inserted move with their pages, so the content can be numbered first and the table inserted before it. The title, fonts, colors, indent and dotted leaders are properties of the table of contents. In a tagged document the table is tagged as a TOC element.

PdfConverter converter = new PdfConverter();
converter.RenderingEngine = RenderingEngine.Chromium;
// the converter maps where the headings are
converter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "h1", "h2", "h3" };
Document document = converter.GetPdfDocumentObjectFromUrl("https://www.example.com/manual");

TableOfContents toc = new TableOfContents();
toc.AddEntries(document, converter.HtmlElementsMappingOptions.HtmlElementsMappingResult);

// the converted pages are numbered 1, 2, 3...; the table pages before them i, ii...
document.AddPageLabelRange(0, PageLabelStyle.Decimal);
document.InsertTableOfContents(toc, 0);
document.AddPageLabelRange(0, PageLabelStyle.LowercaseRoman);

document.Save("manual.pdf");
document.Close();