HiQPdf Documentation

Create Outlines, Links and Table of Contents in PDF

Quickly Create High Quality PDFs

The converter can be configured to automatically generate outlines for a selected list of HTML elements using the PdfDocumentControlOutlines property of the HiQPdfPdfAutoOutlines type. A reference to an object of HiQPdf.PdfDocumentControl type is given by the Document property of the HiQPdf.HtmlToPdf class.

In order to select the HTML elements for which to create outlines automatically you have to set the PdfAutoOutlinesOutlinedElements property with a list of CSS selectors. For example it is possible to select all the HTML elements of a type having a given CSS class.

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

The converter transforms by default all the HTTP links and the internal links found in the HTML document to URI links and internal links in the generated PDF document. To disable the automatically generated links you can set the PdfDocumentControlConvertHttpLinks and PdfDocumentControlConvertInternalLinks properties on false.

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

    if (checkBoxHierarchical.Checked)
    {
        // automatically outline the H1 to H6 tags in a hierarchy
        htmlToPdfConverter.Document.Outlines.OutlineHeadingTags = true;
    }
    else
    {
        // create outlines for each chapter and for table of contents
        // the CSS selectors mean all the H1 and H2 elements with the CSS class name 'pdf_outlines'
        htmlToPdfConverter.Document.Outlines.OutlinedElements = new string[] { "H1[class=\"pdf_outlines\"]", "H2[class=\"pdf_outlines\"]" };
    }

    // 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=AutoOutlines.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