Automatic Bookmarks Generation |
The converter can create the bookmarks (the outline shown next to the document in PDF readers) from the headings or other elements of the HTML page. Each bookmark takes the text of its element and goes to the place where the element is in the PDF.
List the elements to bookmark as CSS selectors in HtmlElementSelectors (property PdfBookmarkOptions). Without selectors, no bookmarks are created. "h1" selects all h1 elements, "*.chapter" the elements with the CSS class chapter, "*#summary" the element with the id summary. Only the elements that have a text are bookmarked.
PdfConverter converter = new PdfConverter(); // a bookmark for every h1 and h2 heading of the page converter.PdfBookmarkOptions.HtmlElementSelectors = new string[] { "h1", "h2" }; converter.SavePdfFromUrlToFile("https://www.example.com/manual", "manual.pdf");
By default every bookmarked element is a top-level entry. With HierarchicalBookmarks set to true the bookmarks are nested by the order of the selectors: the elements matched by the first selector are the top-level entries, the elements matched by the second one nest under the closest earlier entry of a lower level, and so on.
PdfConverter converter = new PdfConverter(); // chapters (h1) with their sections (h2) and subsections (h3) nested under them converter.PdfBookmarkOptions.HtmlElementSelectors = new string[] { "h1", "h2", "h3" }; converter.PdfBookmarkOptions.HierarchicalBookmarks = true; converter.SavePdfFromUrlToFile("https://www.example.com/manual", "manual.pdf");
MaxTitleLength limits the length of the bookmark titles. HTML added to a document with the HtmlToPdfElement creates bookmarks the same way.
A table of contents printed in the document, with the page number of each heading and a link to it, is built from the headings of the page with TableOfContentsOptions - see Automatic Table of Contents.
To build a bookmark tree of your own - titles that are not in the page, an order of your own - convert to a Document and add the bookmarks yourself, using the positions of the elements (see HTML elements mapping and Bookmarks).