Create Outlines, Internal Links and Table of Contents in PDF

If your HTML is structured using the heading tags H1 to H6 then you can use the special option PdfDocumentControlGenerateDocumentOutline to automatically create a hierarchy of outlines based on document structure.

If the bookmarks auto-creation is enabled, you can further choose a custom algorithm to generate the bookmarks or use the internal capabilities of the browser using the PdfDocumentControlUseBrowserOutlineMode property. By default, a custom algorithm is used.

The HTML document below contains both internal and HTTP links and the HTML to PDF converter will be configured to automatically generate a table of contents with outlines for chapters.

HTML Document to Create a Table of Contents

XML
<html>
<head>
    <title>Auto Outlines and Links</title>
</head>
<body>
    <h1 class="pdf_outlines">Contents</h1>
    <a href="#Chapter1">Go To Chapter 1</a>
    <br />
    <a href="#Chapter2">Go To Chapter 2</a>
    <br />
    <a href="#Chapter3">Go To Chapter 3</a>
    <br />
    <a href="http://www.hiqpdf.com">Visit Website</a>
    <h2 class="pdf_outlines" style="page-break-before: always" id="Chapter1">Chapter 1</h2>
    This is the chapter 1 content.
    <h2 class="pdf_outlines" style="page-break-before: always" id="Chapter2">Chapter 2</h2>
    This is the chapter 2 content.
    <h2 class="pdf_outlines" style="page-break-before: always" id="Chapter3">Chapter 3</h2>
    This is the chapter 3 content.
</body>
</html>

Auto Outlines and Links Demo

In this demo you learn how to automatically create outlines, HTTP and internal links in PDF and how to force a HTML element to start on a new page in PDF. The demo creates a simple table of contents with internal links to chapters and a HTTP link to HiQPdf website. For each chapter there is also an outline in document outlines and each chapter is forced to start on a new PDF page using the page-break-before : always style.

Demo Source Code

C#
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

using HiQPdf.Chromium;

namespace HiQPdf_Chrome_AspNetDemo.Controllers
{
    public class AutoOutlinesAndLinksController : Controller
    {
        IWebHostEnvironment m_hostingEnvironment;

        public AutoOutlinesAndLinksController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        private void SetViewData()
        {
            ViewData["ContentRootPath"] = m_hostingEnvironment.ContentRootPath + "/wwwroot";
        }

        // GET: AutoOutlinesAndLinks
        public ActionResult Index()
        {
            SetViewData();

            return View();
        }

        [HttpPost]
        public ActionResult ConvertToPdf(IFormCollection collection)
        {
            // create the HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // set a demo serial number
            htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // create a hierarchy of bookmarks from H1 to H6 tags
            if (collection["autoBookmarksCheckBox"].Count > 0)
            {
                // enable the generation of a PDF outline with bookmarks for H1 to H6 tags
                htmlToPdfConverter.Document.GenerateDocumentOutline = collection["autoBookmarksCheckBox"].Count > 0;

                htmlToPdfConverter.WaitBeforeConvert = 2;

                // optionally enable the outline mode to utilize browser capabilities
                htmlToPdfConverter.Document.UseBrowserOutlineMode = collection["useBrowserOutlineModeCheckBox"].Count > 0;

                // display the bookmarks panel in PDF viewer when the generated PDF is opened
                htmlToPdfConverter.Document.Viewer.PageMode = PdfPageMode.Outlines;
            }

            // convert HTML to PDF
            byte[] pdfBuffer = null;

            if (collection["UrlOrHtmlCode"] == "radioButtonConvertUrl")
            {
                // convert URL to a PDF memory buffer
                string url = collection["textBoxUrl"];

                pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
            }
            else
            {
                // convert HTML code
                string htmlCode = collection["textBoxHtmlCode"];
                string baseUrl = collection["textBoxBaseUrl"];

                // convert HTML code to a PDF memory buffer
                pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
            }

            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "AutoOutlines.pdf";

            return fileResult;
        }
    }
}

See Also