HiQPdf Documentation

Create Table of Contents from Heading Tags Structure

Quickly Create High Quality PDFs

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#
protected void buttonCreatePdf_Click(object sender, EventArgs e)
{
    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    // automatically create a table of contents from H1 to H6 tags
    htmlToPdfConverter.Document.TableOfContents.TocEntriesForHeadingTags = checkBoxCreateTOC.Checked;

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

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

    // inform the browser about the binary data format
    HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

    // let the browser know how to open the PDF document, attachment or inline, and the file name
    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=AutoTableOfContents.pdf; size={0}",
                    pdfBuffer.Length.ToString()));

    // write the PDF buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(pdfBuffer);

    // call End() method of HTTP response to stop ASP.NET page processing
    HttpContext.Current.Response.End();
}
See Also

Other Resources