Automatically Create Table of Contents with EVO HTML to PDF Converter for .NET

EVO HTML to PDF Converter for .NET can be configured to automatically create a table of contents in PDF document based on H1 to H6 head tags found in HTML document by simply turning on a flag.

You can also define a table of contents directly in HTML using a set of special attributes. You can set the attribute ‘data-toc-item‘ on a HTML element to enable the creation a table of contents entry for that element in PDF. Additionally you can specify the item level in table of contents with ‘data-toc-level‘ attribute and the bookmark title with ‘data-toc-title‘ attribute.

The automatically generated table of contents is hierarchical, having the items structured on many levels and can contain the page number in PDF for each item. Each item in the table of contents is a link to the corresponding content in the PDF document and when you click on that link the PDF viewer will scroll to that content in PDF document.

You can customize the table of contents style for title, for each level of items and for the page numbers. You can change the font, color, background using the well known CSS syntax.

The relevant C# code for automatically creating a table of contents is:

protected void convertToPdfButton_Click(object sender, EventArgs e)
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

    // Enable the creation of a table of contents from H1 to H6 tags found in HTML
    htmlToPdfConverter.TableOfContentsOptions.AutoTocItemsEnabled = autoTableOfContentsCheckBox.Checked;

    // Optionally set the table of contents title
    htmlToPdfConverter.TableOfContentsOptions.Title = "Table of Contents";

    // Optionally set the title style using CSS sttributes
    htmlToPdfConverter.TableOfContentsOptions.TitleStyle = "color:navy; font-family:'Times New Roman'; font-size:28px; font-weight:normal";

    // Optionally set the style of level 1 items in table of contents
    string level1TextStyle = "color:black; font-family:'Times New Roman'; font-size:20px; font-weight:normal; font-style:normal; background-color:#F0F0F0";
    htmlToPdfConverter.TableOfContentsOptions.SetItemStyle(1, level1TextStyle);

    // Optionally set the page numbers style of level 1 items in table of contents
    string level1PageNumberStyle = "color:black; padding-right:3px; background-color:#F0F0F0; font-size:14px; font-weight:bold";
    htmlToPdfConverter.TableOfContentsOptions.SetPageNumberStyle(1, level1PageNumberStyle);

    // Convert the HTML page to a PDF document in a memory buffer
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(urlTextBox.Text);

    // Send the PDF as response to browser

    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");

    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Auto_Create_Table_of_Contents.pdf; size={0}", outPdfBuffer.Length.ToString()));

    // Write the PDF document buffer to HTTP response

    // End the HTTP response and stop the current page processing


You can see the live demo for automatically creating table of contents using the HTML to PDF Converter  for .NET, with full C# source code and detailed description.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s