How to add Table of Contents

by Chaknith Bin

A table of contents (TOC) is like a roadmap that helps readers navigate through the PDF document's contents. It typically appears at the beginning and lists the main sections or chapters of the PDF, along with the page numbers where each section begins. This allows readers to quickly find and jump to specific parts of the document, making it easier to access the information they need.

IronPDF provides a feature to create a table of contents with hyperlinks to the "h1," "h2," "h3," "h4," and "h5" elements. The default styling of this table of contents will not conflict with other styles in the HTML content.


C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
C# PDF DLL

Download DLL

Download DLL

Manually install into your project

Add Table of Contents Example

Use the TableOfContents property to enable the creation of a table of contents in the output PDF document. This property can be assigned one of three TableOfContentsTypes, which are described as follows:

  • None: Do not create a table of contents
  • Basic: Create a table of contents without page numbers
  • WithPageNumbers: Create a table of contents WITH page numbers

To understand this feature better, you can download the sample HTML file below:

Code

:path=/static-assets/pdf/content-code-examples/how-to/table-of-contents.cs
using IronPdf;
using System.IO;

// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure render options
renderer.RenderingOptions = new ChromePdfRenderOptions
{
    // Enable table of content feature
    TableOfContents = TableOfContentsTypes.WithPageNumbers,
};

PdfDocument pdf = renderer.RenderHtmlFileAsPdf("tableOfContent.html");

pdf.SaveAs("tableOfContents.pdf");
Imports IronPdf
Imports System.IO

' Instantiate Renderer
Private renderer As New ChromePdfRenderer()

' Configure render options
renderer.RenderingOptions = New ChromePdfRenderOptions With {.TableOfContents = TableOfContentsTypes.WithPageNumbers}

Dim pdf As PdfDocument = renderer.RenderHtmlFileAsPdf("tableOfContent.html")

pdf.SaveAs("tableOfContents.pdf")
VB   C#

Output PDF

The table of contents will be created with hyperlinks to each of the "h1", "h2", "h3", "h4", and "h5".

Table of Contents Placement on the PDF

Before generating the table of contents, IronPDF will attempt to find an element with the id 'ironpdf-toc.' If such an element doesn't exist, IronPDF will insert the table of contents at the beginning of the HTML content.

To control the placement of the table of contents, specify an element with the id 'ironpdf-toc.' Ideally, this element should be a div since the table of contents will then be placed inside a container to avoid any inconvenience. For example:

<div id="ironpdf-toc"></div>
<div id="ironpdf-toc"></div>
HTML

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.