Create Table of Contents from Heading Tags Structure

The converter can be configured to automatically create table of contents based on heading tags H1 to H6 using the PdfDocumentControlTableOfContents property of the HiQPdfPdfTableOfContents type. A reference to an object of HiQPdf.PdfDocumentControl type is given by the Document property of the HiQPdf.HtmlToPdf class.

In order to enable the translation of HTML forms to PDF forms you have to set PdfTableOfContentsTocEntriesForHeadingTags property on true.

HTML Document Containing the Structured Heading Tags

XML
<html>
<head>
    <title>Auto Create Table of Contents</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 Create Table of Contents Demo

In this demo you learn how to automatically create a table of contents in the beginning of the generated PDF document based on the H1 to H6 tags found in HTML document.

Demo Source Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

using HiQPdf;

namespace HiQPdf_Demo.Controllers
{
    public class AutoCreateTableOfContentsController : Controller
    {
        // GET: AutoCreateTableOfContents
        public ActionResult Index()
        {
            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==";

            // automatically create a table of contents from H1 to H6 tags
            htmlToPdfConverter.Document.TableOfContents.TocEntriesForHeadingTags = collection["checkBoxCreateTOC"].Count > 0;

            // make the outlines visible in viewer
            htmlToPdfConverter.Document.Viewer.PageMode = PdfPageMode.Outlines;

            // convert the HTML code to PDF
            byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(collection["textBoxHtmlCode"], null);

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

            return fileResult;
        }
    }
}

See Also