How to Add Table of Contents

This article was translated from English: Does it need improvement?
Translated
View the article in English

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', 'h5', and 'h6' elements. The default styling of this table of contents will not conflict with other styles in the HTML content.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

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

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


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 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. To understand this feature better, you can download the sample HTML file below:

コード

:path=/static-assets/pdf/content-code-examples/how-to/table-of-contents.cs
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");
Imports IronPdf

' 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")
$vbLabelText   $csharpLabel

出力PDF

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

ご注意Using the Merge method on the document will break the hyperlinks of the table of contents.


Table of Contents Placement on the 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>
<div id="ironpdf-toc"></div>
HTML
  1. In the render options, choose to render the table of contents either with or without page numbers.

Styling 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.

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

警告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.

:path=/static-assets/pdf/content-code-examples/how-to/table-of-contents-overwrite-styling.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,
    CustomCssUrl = "./custom.css"
};

// Read HTML text from file
string html = File.ReadAllText("tableOfContent.html");
PdfDocument pdf = renderer.RenderHtmlAsPdf(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,
	.CustomCssUrl = "./custom.css"
}

' Read HTML text from file
Dim html As String = File.ReadAllText("tableOfContent.html")
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

pdf.SaveAs("tableOfContents.pdf")
$vbLabelText   $csharpLabel

Style Headers

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;
 }
Style headers

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, we can 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;
 }
Set custom font family

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;
}
Set custom indentation

Dot Line

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;
 }
Remove dots

次に何ができるのかを見てみましょうか? 私たちのチュートリアルページをご覧ください: PDFの変換

よくある質問

.NET C# で PDF に目次を追加するにはどうすればいいですか?

IronPDF を使用すると、PDF 生成設定で TableOfContents プロパティを設定することで PDF に目次を追加できます。これにより、PDF ドキュメント内の見出し要素にリンクされたナビゲーション インデックスが自動的に作成されます。

IronPDF では目次にどのようなオプションがありますか?

IronPDF は目次のために 3 つのオプションを提供します。None (TOCなし)、Basic (ページ番号なしの TOC)、および WithPageNumbers (ページ番号を含む TOC) です。

目次を PDF の特定の場所に表示させるにはどうすればよいですか?

目次を特定の場所に配置するには、HTML ドキュメント内に ID 'ironpdf-toc' を持つ div を挿入します。IronPDF はこの場所に TOC を配置します。

CSS を使用して目次をスタイリングすることはできますか?

はい、IronPDF を使用すれば、CSS を使って目次をスタイルすることができます。特定の TOC 要素をターゲットとして外観を変更し、カスタム CSS URL を利用して追加スタイリングを行うことができます。

PDF の目次のフォントを変更することが可能ですか?

#ironpdf-toc li .title#ironpdf-toc li .page の CSS セレクタを使用して、目次のフォントを変更できます。カスタムフォントは、@font-face 属性を使用して実装できます。

PDF ドキュメントをマージするときに、TOC のハイパーリンクが破損するのを防ぐにはどうすればよいですか?

TOC 作成後に Merge メソッドを使用するときは、TOC を更新または再生成して、動作するハイパーリンクを維持していることを確認してください。マージするとハイパーリンクが破損する可能性があります。

目次のスタイリングにおいて、ページブレークに関して注意しなければならないことは何ですか?

CSS で page-break-before および page-break-after プロパティを変更しないでください。これにより TOC のページ番号計算に影響を与える可能性があります。

TOC のタイトルとページ番号の間の点線を削除するにはどうすればよいですか?

タイトルとページ番号の間の点線を削除するには、TOC 項目の '::after' セレクタの CSS を変更し、background-image プロパティを 'transparent 1px' に設定します。

目次の見出しのインデントを制御するにはどうすればよいですか?

目次内の見出しのインデントを制御するには、':root' CSS セレクタを使用します。これにより、各見出しのインデントレベルを定義できます。

PDF ドキュメントにおける目次の役割とは何ですか?

目次は PDF ドキュメント内のナビゲーション ツールとして機能し、セクションや章とそのページ番号をリストして、ユーザーが特定のコンテンツを素早く見つけるのを助けます。

IronPDF は目次を追加するときに .NET 10 と完全に互換性がありますか? また、特別な考慮事項はありますか?

はい。IronPDF は、以前の .NET バージョンと同様に、.NET 10 と完全に互換性があります。.NET 10 で目次を追加する場合、特別な設定変更は必要ありません。同じ API とプロパティ( ChromePdfRenderOptionsTableOfContentsなど)がそのまま使用できます。目次機能を使用するには、NuGet for .NET 10 経由でプロジェクトに IronPDF を追加し、レンダリングエンジンで JavaScript が有効になっていることを確認してください。

Chaknith Bin
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeに取り組んでいます。彼はC#と.NETの深い専門知識を持ち、ソフトウェアの改善や顧客サポートに貢献しています。ユーザーとの対話から得られる洞察が、より良い製品、ドキュメント、および全体的な経験に寄与しています。
準備はいいですか?
Nuget ダウンロード 16,154,058 | バージョン: 2025.11 ただ今リリースされました