Create table of contents in PDF documents

With the release of XFINIUM.PDF 6.2 the table of contents can be generated automatically for flow PDF documents.

The table of contents is generated with the help of a new flow content class: PdfFlowHeadingContent. Heading content is similar to text content but it allows to create a hierarchy based on its Level property. 8 levels (1 to 8) of heading content are supported.
The heading level is used to generate the parent-child bookmarks in document outline and to generate the heading numbers, if required.

Adding heading content is done like this:

PdfFormattedContent fc = new PdfFormattedContent("Chapter One");
(fc.Paragraphs[0].Blocks[0] as PdfFormattedTextBlock).Font = heading1Font;
fc.Paragraphs[0].SpacingBefore = fc.Paragraphs[0].SpacingAfter = 12;
PdfFlowHeadingContent fhc = new PdfFlowHeadingContent(fc);
// Top level heading
fhc.Level = 1;
// Generate a number for the heading.
// If this is the first heading in the document, it will look like this:
// 1. Chapter One
fhc.AutoNumber = true;
doc.AddContent(fhc);
Dim fc As New PdfFormattedContent("Chapter One")
TryCast(fc.Paragraphs(0).Blocks(0), PdfFormattedTextBlock).Font = heading1Font
fc.Paragraphs(0).SpacingBefore = 12
fc.Paragraphs(0).SpacingAfter = 12
Dim fhc As New PdfFlowHeadingContent(fc)
' Top level heading
fhc.Level = 1
' Generate a number for the heading.
' If this is the first heading in the document, it will look like this:
' 1. Chapter One
fhc.AutoNumber = true
doc.AddContent(fhc)

The table of contents is generated from all the heading objects included in the document. It should be generated at the end after the entire document content has been created.

The PdfFlowContent.GenerateTableOfContents() method generates the table of contents in the document. The method can accept a PdfFlowDocumentTOCSettings object as parameter for customizing the table of contents. The properties of the PdfFlowDocumentTOCSettings object control how the table of contents is generated.

// Setup the document's table of contents.
PdfFlowDocumentTOCSettings tocSettings = new PdfFlowDocumentTOCSettings();
// Generate the table of contents as document outline.
tocSettings.GenerateDocumentOutline = true;
// If only a few top heading levels (3 in example below) need to be included in the outline.
// Heading levels 4 to 8 will not appear in the document outline.
// tocSettings.MaxHeadingLevelForDocumentOutline = 3;
// Generate the table of contents as a separate page in the document.
tocSettings.GenerateContentsPage = true;
// Set the font and color for the text used to display the table of contents.
tocSettings.ContentsTextFont = new PdfStandardFont(PdfStandardFontFace.Helvetica, 10);
tocSettings.ContentsTextColor = new PdfBrush(PdfRgbColor.Black);
// Insert the TOC page at position 1 (after the first page of the document).
tocSettings.ContentsPagePosition = 1;
// Visually connect the TOC entries with the page numbers using dots
// See the table of contents sample below
tocSettings.ContentsEntryFiller = '.';
// Create a title for the TOC
// The title is a flow content object that is displayed above the table of contents, usually is a simple text such as "Contents"
fc = new PdfFormattedContent("TABLE OF CONTENTS");
(fc.Paragraphs[0].Blocks[0] as PdfFormattedTextBlock).Font = new PdfStandardFont(heading1Font);
(fc.Paragraphs[0].Blocks[0] as PdfFormattedTextBlock).Font.Size = 24;
fc.Paragraphs[0].HorizontalAlign = PdfStringHorizontalAlign.Center;
fc.Paragraphs[0].SpacingAfter = 24;
tocSettings.ContentsTitle = new PdfFlowTextContent(fc);
// Indent the entries in the TOC
// The entries in the table of contents will appear indented to the right
// 1. Chapter One .................................. 1
//   1.1. Section One .............................. 1
//   1.2. Section Two .............................. 2
//       1.2.1. Module Three ....................... 3
tocSettings.ContentsHeadingIndent = 10; // 10 points
// Generate the table of contents in the document.
// This method should be called only once per document.
doc.GenerateTableOfContents(tocSettings);
' Setup the document's table of contents.
Dim tocSettings As New PdfFlowDocumentTOCSettings()
' Generate the table of contents as document outline.
tocSettings.GenerateDocumentOutline = True
' If only a few top heading levels (3 in example below) need to be included in the outline.
' Heading levels 4 to 8 will not appear in the document outline.
' tocSettings.MaxHeadingLevelForDocumentOutline = 3;
' Generate the table of contents as a separate page in the document.
tocSettings.GenerateContentsPage = True
' Set the font and color for the text used to display the table of contents.
tocSettings.ContentsTextFont = New PdfStandardFont(PdfStandardFontFace.Helvetica, 10)
tocSettings.ContentsTextColor = New PdfBrush(PdfRgbColor.Black)
' Insert the TOC page at position 1 (after the first page of the document).
tocSettings.ContentsPagePosition = 1
' Visually connect the TOC entries with the page numbers using dots
' See the table of contents sample below
tocSettings.ContentsEntryFiller = "."C
' Create a title for the TOC
' The title is a flow content object that is displayed above the table of contents, usually is a simple text such as "Contents"
fc = New PdfFormattedContent("TABLE OF CONTENTS")
TryCast(fc.Paragraphs(0).Blocks(0), PdfFormattedTextBlock).Font = New PdfStandardFont(heading1Font)
TryCast(fc.Paragraphs(0).Blocks(0), PdfFormattedTextBlock).Font.Size = 24
fc.Paragraphs(0).HorizontalAlign = PdfStringHorizontalAlign.Center
fc.Paragraphs(0).SpacingAfter = 24
tocSettings.ContentsTitle = New PdfFlowTextContent(fc)
' Indent the entries in the TOC
' The entries in the table of contents will appear indented to the right
' 1. Chapter One .................................. 1
'   1.1. Section One .............................. 1
'   1.2. Section Two .............................. 2
'       1.2.1. Module Three ....................... 3
tocSettings.ContentsHeadingIndent = 10
' 10 points
' Generate the table of contents in the document.
' This method should be called only once per document.
doc.GenerateTableOfContents(tocSettings)

The code above is taken from the TableOfContents sample included in the XFINIUM.PDF install kits.
After running the sample the table of contents is generated like in the screenshot below.

Table of contents
Table of contents

On the left the table of contents as document outline and in the center the page that displays the table of contents. The entries in the table of contents page are clickable links that take the reader to the corresponding section in the document.

4 thoughts on “Create table of contents in PDF documents”

  1. Hi, i’m evaluating Xfinium, and I just want to know how to display a pdf? I can’t find any info on how to show a pdf in my app, using xfinium. Can you please point me to a resource that explains how to do that?

    1. PDF viewer controls are under development and they will be available later this year. For the moment a PDF file can be displayed if it is rendered as image and the image is displayed. The PDFToImage sample shows this functionality.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.