How to Add Table of Contents in C#

Curtis Chau
Curtis Chau
Updated: October 7, 2026

IronPDF lets you add a table of contents to PDF documents in C# by setting the TableOfContents property, which automatically generates hyperlinked navigation from HTML headers (h1-h6) with optional page numbers.

Quickstart: Add Table of Contents to PDF in C#
  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy and run this code snippet.

    new ChromePdfRenderer { RenderingOptions = { TableOfContents = TableOfContentsTypes.WithPageNumbers, FirstPageNumber = 1 } }
        .RenderHtmlFileAsPdf("myDocument.html")
        .SaveAs("withToc.pdf");
  3. 3Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

What Is a Table of Contents in PDF?

A table of contents (TOC) is 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, h5, and h6 elements. The default styling of this table of contents will not conflict with other styles in the HTML content. When you create new PDFs using IronPDF, the table of contents feature automatically scans your HTML headers and builds a hierarchical navigation structure that mirrors your document's organization.

The generated table of contents includes clickable links that allow readers to jump directly to any section, making it particularly useful for long documents, reports, and technical documentation. IronPDF's TOC implementation preserves the semantic structure of your HTML while providing professional PDF navigation capabilities.

How Do I Add a Table of Contents to My PDF?

Use the TableOfContents property to enable the creation of a table of contents in the output PDF document. This property can be assigned to 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

This feature uses JavaScript to build the table of contents; therefore, the engine must have JavaScript enabled. When converting HTML files to PDF, IronPDF's JavaScript engine processes your header tags and generates the appropriate navigation structure. To understand this feature better, download the sample HTML file below:

What Code Do I Need to Generate the Table of Contents?

using IronPdf;

// 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");

For more advanced scenarios, you can combine the table of contents with other rendering options to create comprehensive PDF documents:

using IronPdf;

// Create renderer with multiple options
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions = new ChromePdfRenderOptions
{
    // Enable table of contents with page numbers
    TableOfContents = TableOfContentsTypes.WithPageNumbers,
    
    // Add margins for better formatting
    MarginTop = 40,
    MarginBottom = 40,
    
    // Enable JavaScript for dynamic content
    EnableJavaScript = true,
    
    // Set paper orientation
    PaperOrientation = PdfPaperOrientation.Portrait,
    
    // Add first page number offset
    FirstPageNumber = 1
};

 // Convert HTML with multiple header levels
string htmlContent = @"
<h1>Introduction</h1>
<p>Welcome to our comprehensive guide...</p>

<h2>Chapter 1: Getting Started</h2>
<p>Let's begin with the basics...</p>

<h3>1.1 Prerequisites</h3>
<p>Before we start, ensure you have...</p>

<h2>Chapter 2: Advanced Topics</h2>
<p>Now let's explore more complex features...</p>
";

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("document-with-toc.pdf");

From IronPDF v2026.10.1, FirstPageNumber also offsets the page numbers printed inside a WithPageNumbers table of contents, keeping them aligned with the {page} token in headers and footers. The default value of 1 reproduces the previous numbering exactly, so the numbers only shift if you set a non-default value.

What Does the Generated PDF Look Like?

The table of contents will be created with hyperlinks to each of the h1, h2, h3, h4, h5, and h6. The hierarchical structure of your headers is preserved, with sub-headers properly indented under their parent sections. You can also add page numbers to your PDF for additional navigation support alongside the table of contents.

Please note: Using the Merge method on the document will break the hyperlinks of the table of contents.

When working with merged or split PDFs, generate the table of contents after all document assembly is complete to ensure accurate page references and functional hyperlinks.


Where Should I Place the Table of Contents in My PDF?

  1. Ensure that the HTML document has proper header tags (h1 up to h6).

  2. Optionally insert a div for where you want the Table of Contents to appear. If the below div is not provided, IronPDF will insert the Table of Contents at the start.

    <div id="ironpdf-toc"></div>
    HTML
  3. In the render options, choose to render the table of contents either with or without page numbers.

For documents with complex layouts, combine the table of contents with headers and footers to create a professional document structure. Here's an example of proper HTML structure for optimal TOC generation:

<!DOCTYPE html>
<html>
<head>
    <title>My Document</title>
</head>
<body>
    <!-- Table of Contents placeholder -->
    <div id="ironpdf-toc"></div>
    
    <!-- Page break after TOC -->
    <div style="page-break-after: always;"></div>
    
    <!-- Main content starts here -->
    <h1>Executive Summary</h1>
    <p>This document provides...</p>
    
    <h2>Market Analysis</h2>
    <h3>Current Trends</h3>
    <p>The market shows...</p>
    
    <h3>Future Projections</h3>
    <p>We anticipate...</p>
    
    <h2>Recommendations</h2>
    <p>Based on our analysis...</p>
</body>
</html>
HTML

How Do I Add a Title to the Table of Contents?

From IronPDF v2026.10.1, TableOfContentsTitle renders a heading at the top of the generated table of contents, and TableOfContentsIncludesTitleEntry optionally lists that title as the first row.

using IronPdf;

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

// Configure render options
renderer.RenderingOptions = new ChromePdfRenderOptions
{
    // Generate a titled table of contents with page numbers
    TableOfContents = TableOfContentsTypes.WithPageNumbers,
    TableOfContentsTitle = "Table of Contents",
    // Also list the title as the first entry
    TableOfContentsIncludesTitleEntry = true,
};

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

pdf.SaveAs("titledToc.pdf");
C#

The heading is a div placed inside #ironpdf-toc above the list, in the normal document flow, so no coordinates are needed. It is plain text: HTML in the title is shown literally, and a \n becomes a line break. It is deliberately not an h1-h6 heading, so it adds no bookmark or PDF/UA heading tag and cannot list itself in the table of contents.

With TableOfContentsIncludesTitleEntry = true, the entry links to the first page of the table of contents itself and shows a page number only in WithPageNumbers mode; in Basic mode it renders the title and dotted leader with no number.

Three cases draw neither the heading nor the entry: a null, empty or whitespace-only title counts as unset; TableOfContents = None disables the feature; and a document with no h1-h6 headings generates no table of contents at all.

Please note: Remote IronPdfEngine deployments require engine v2026.10.1 or later. An older engine drops both title options without an error, and applies FirstPageNumber to headers and footers only, leaving the table of contents numbered from 1.

My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic

Microsoft MVP

View case study

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle

Chief Technology Officer, OPYN

View case study

How Do I Style the Table of Contents?

The Table of Contents can be styled using CSS by targeting the various CSS selectors that define the style of the Table of Contents. When managing fonts in your PDF, the table of contents will inherit your document's font settings by default, but can be customized independently.

In addition, styling modifications can be done using the CustomCssUrl property. Begin by downloading a CSS file that contains the original styling for the table of contents below.

Warning: Currently, it's not recommended to overwrite the page-break-before and page-break-after properties when styling the table of contents, as this will break page number calculations. The current implementation expects the Table of Contents to be on separate pages from other document content.
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,
    CustomCssUrl = "./custom.css"
};

// Read HTML text from file
string html = File.ReadAllText("tableOfContent.html");
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

pdf.SaveAs("tableOfContents.pdf");

When working with custom paper sizes, you may need to adjust the table of contents styling to accommodate different page dimensions and ensure proper text flow and pagination.

How Do I Style Different Header Levels?

Use the #ironpdf-toc ul li.h1 selector to apply different styling to the H1 header in the table of contents. Replace h1 with h2 up to h6 to change the styling for each respective header.

#ironpdf-toc ul li.h1 {
   font-style: italic;
   font-weight: bold;
}
CSS
Styled table of contents with dotted leaders connecting hierarchical chapters and sections to page numbers

How Do I Change the Font Family?

With both the '#ironpdf-toc li .title' and '#ironpdf-toc li .page' selectors, it is possible to overwrite the font family of the table of contents. To do this, use the cursive font for the title and utilize the @font-face attribute to use the custom 'Lemon' font designed by Eduardo Tunni.

#ironpdf-toc li .title {
   order: 1;
   font-family: cursive;
}

@font-face {
   font-family: 'lemon';
   src: url('Lemon-Regular.ttf')
}

#ironpdf-toc li .page {
   order: 3;
   font-family: 'lemon', sans-serif;
}
CSS
Table of contents with chapters, sections, and lessons showing dotted leaders and right-aligned page numbers

How Do I Control Indentation?

Indentation can be controlled using the :root selector. This value determines the amount of indent for each header level (h1, h2, ...) in the table of contents. It can be increased as needed, or there can be no indentation with a value of 0.

:root {
    --indent-length: 25px;
}
CSS
Table of contents with custom indented chapters, sections, and lessons showing dotted leaders to page numbers

How Do I Remove or Customize the Dot Lines?

To remove the dotted lines between the header title and page number, modify the background-image of the ::after selector. In the original styling, the second parameter is "currentcolor 1px". Change it to "transparent 1px" to remove the dots. It is important to specify other attributes as well because, in this selector, the new styling will completely override the old styling rather than just adding to it.

#ironpdf-toc li::after {
   background-image: radial-gradient(circle, transparent 1px, transparent 1.5px);
   background-position: bottom;
   background-size: 1ex 4.5px;
   background-repeat: space no-repeat;
   content: "";
   flex-grow: 1;
   height: 1em;
   order: 2;
}
CSS
Table of contents with chapters, sections, and lessons showing hierarchical indentation and right-aligned page numbers

For more advanced styling options, create custom leader lines using different patterns:

/* Dashed line leader */
#ironpdf-toc li::after {
    background-image: linear-gradient(to right, currentcolor 50%, transparent 50%);
    background-size: 8px 1px;
    background-repeat: repeat-x;
    background-position: bottom;
}

/* Solid line leader */
#ironpdf-toc li::after {
    border-bottom: 1px solid currentcolor;
    background: none;
}
CSS

Ready to see what else you can do? Check out our tutorial page here: Convert PDFs

How Do I Style the Title Heading and Title Entry?

The title heading is #ironpdf-toc .toc-heading, a div above the list. The title entry row is #ironpdf-toc .toc-heading-entry, an li that reuses the same .title, .toc-dots and .page classes as the other rows, with .page present only when there is a number.

#ironpdf-toc .toc-heading {
    font-size: 24pt;
    font-weight: bold;
    margin: 0 0 0.5em 0;
    color: #003366;
}

#ironpdf-toc .toc-heading-entry .title {
    font-style: italic;
}
CSS

How an override behaves depends on how the CSS is supplied to CustomCssUrl:

  • A local file is merged into the built-in stylesheet by exact selector text, so a #ironpdf-toc .toc-heading rule replaces the built-in rule wholesale. Restate font-size, font-weight and margin or they are dropped with the replaced rule.
  • A remote URL is linked before the built-in styles, so at equal specificity the built-in rule wins. Use a more specific selector, such as div#ironpdf-toc .toc-heading, or !important.

Frequently Asked Questions

What is the advantage of using IronPDF to add a Table of Contents in C#?

IronPDF simplifies the process of adding a table of contents by automating hyperlinked navigation from HTML headers. This feature ensures easy navigation through PDF documents using a structured TOC with page numbers.

How can I create a table of contents with page numbers using IronPDF?

You can create a table of contents with page numbers by setting the TableOfContents property to TableOfContentsTypes.WithPageNumbers in IronPDF's ChromePdfRenderOptions during PDF generation.

Can IronPDF automatically generate a table of contents from HTML headers?

Yes, IronPDF can automatically generate a table of contents from HTML header tags (h1-h6), preserving the hierarchy and allowing easy navigation across document sections.

How do I customize the styling of a Table of Contents generated with IronPDF?

The styling of a Table of Contents can be customized using CSS. You can modify the font, indentation, and leader lines by targeting CSS selectors used by IronPDF's TOC feature.

What are the rendering options available for the Table of Contents feature in IronPDF?

IronPDF offers three rendering options for the table of contents: None, Basic (without page numbers), and WithPageNumbers. These settings can be configured in the rendering options when generating PDFs.

Is it possible to use custom CSS to style the table of contents when using IronPDF?

Yes, you can use custom CSS to style the table of contents in IronPDF by setting the CustomCssUrl property in the ChromePdfRenderOptions during PDF creation.

Can I specify where the table of contents should appear in my PDF using IronPDF?

Yes, you can specify a placeholder in your HTML using a div with the id ironpdf-toc. If not specified, IronPDF will automatically place the table of contents at the start of the document.

Does IronPDF support dynamic content like JavaScript when generating PDFs?

Yes, IronPDF supports dynamic content and can execute JavaScript during the conversion process, enhancing the interactivity of the generated PDF.

How can I download a sample HTML file to see how Table of Contents is structured?

You can download a sample HTML file directly from the IronPDF guide to understand how the table of contents is structured before converting it to a PDF.

What should I be cautious of when styling the table of contents in IronPDF?

When styling the table of contents, avoid modifying page-break-before and page-break-after properties within the TOC styling, as they may disrupt page number calculations in the PDF.

How do I add a title to the table of contents in IronPDF?

Set TableOfContentsTitle on ChromePdfRenderOptions, available from IronPDF v2026.10.1. The heading is placed inside #ironpdf-toc above the list, in the normal document flow, so no coordinates are needed. Set TableOfContentsIncludesTitleEntry = true to also list the title as the first entry of the table of contents.

Why is my table of contents title not showing?

A null, empty or whitespace-only title counts as unset, so neither the heading nor the title entry is drawn. The feature is also disabled when TableOfContents is None, and a document with no h1 to h6 headings generates no table of contents at all.

Why is my .toc-heading CSS being ignored?

It depends on how the CSS reaches CustomCssUrl. A local file is merged by exact selector text, so a #ironpdf-toc .toc-heading rule replaces the built-in rule wholesale and should restate font-size, font-weight and margin. A remote URL is linked before the built-in styles, so ties lose to them; use a more specific selector such as div#ironpdf-toc .toc-heading, or !important.

Why did my table of contents page numbers change after upgrading?

From IronPDF v2026.10.1, a non-default FirstPageNumber also offsets the page numbers inside a WithPageNumbers table of contents, keeping them aligned with the header and footer {page} token. The default value of 1 reproduces the previous numbering exactly; set FirstPageNumber = 1 to keep the old 1-based numbering.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

Read More

Ready to Get Started?

NuGet Downloads 22,198,312Version:2026.11just released