Automatic Table of Contents |
The HTML to PDF converter can give a converted document a table of contents with the numbers of the PDF pages the entries are on. Set it up with TableOfContentsOptions; its Mode chooses one of three ways.
With HtmlTableOfContentsMode.FillExisting, the page already has a table of contents made of links to anchors in the page. Name the element that holds it with ContainerSelector: each link in it gets a leader and the number of the page its target is on.
using ExpertPdf.Anywhere.HtmlToPdf; PdfConverter converter = new PdfConverter(); converter.TableOfContentsOptions.Mode = HtmlTableOfContentsMode.FillExisting; converter.TableOfContentsOptions.ContainerSelector = "#toc"; converter.SavePdfFromUrlToFile("https://www.example.com/manual", "manual.pdf");
With HtmlTableOfContentsMode.InsertHtml, the converter writes a table of contents into the page from the headings matched by HeadingSelectors, then numbers it. It is HTML, styled by the page's CSS: a nav.expertpdf-toc element with a h2.expertpdf-toc-title title and nested lists whose items have the classes expertpdf-toc-entry and expertpdf-toc-level-N. PlaceholderSelector names an element to put it in, and PageBreak gives it a page of its own.
PdfConverter converter = new PdfConverter(); HtmlTableOfContentsOptions toc = converter.TableOfContentsOptions; toc.Mode = HtmlTableOfContentsMode.InsertHtml; toc.HeadingSelectors = new string[] { "h1", "h2" }; toc.Title = "Contents"; converter.CustomCSS = "nav.expertpdf-toc li.expertpdf-toc-level-1 { font-weight: bold; }"; byte[] pdf = converter.GetPdfBytesFromUrl("https://www.example.com/manual");
With HtmlTableOfContentsMode.GeneratePages, the table of contents is drawn on PDF pages of its own, inserted at the start, after the first page or at the end (PagePosition). The title, the fonts, the colors, the leaders, numbered entries and a style per level are options; the header and footer of the conversion appear on those pages too.
PdfConverter converter = new PdfConverter(); HtmlTableOfContentsOptions toc = converter.TableOfContentsOptions; toc.Mode = HtmlTableOfContentsMode.GeneratePages; toc.HeadingSelectors = new string[] { "h1", "h2" }; toc.PagePosition = HtmlTableOfContentsPagePosition.Start; toc.Title = "Contents"; toc.TitleAlign = HorizontalTextAlign.Center; toc.NumberEntries = true; converter.SavePdfFromUrlToFile("https://www.example.com/manual", "manual.pdf");
The entries of every mode are links to their headings. The numbers start at the first page number of the footer (or of the header) when page numbering is offset, and right-to-left pages get their numbers on the left. In a tagged document the table of contents is part of the logical structure.