This article describes the process to create table of content in PDF using C#. It has the details to set the development environment, a list of steps, and a sample code to add table of contents in PDF using C#. You will learn all the configurations for the table of contents including the text, hyperlinks and connectivity with different pages from the PDF file.
Steps to Add Table of Contents to PDF using C#
- Set the environment to use Aspose.PDF for .NET to add a table of contents
- Load the source PDF document and insert a page at the start for adding TOC
- Create objects of TocInfo and TextFragment classes for setting the TOC title
- Create headings text for adding to the table of contents
- Run a loop to add a TOC heading for each page in the loaded PDF
- Set the destination page, its coordinates and the text of the heading in each iteration
- Save the output PDF file having TOC on the first page
These steps explain the process to create clickable table of contents in PDF using C#. Load the PDF file, insert a page at the start of the document for the table of contents, and use the TocInfo and TextFragment to set the characteristics of the TOC. For each page in the source document, add a hyperlink in the table of contents, set the hyperlink text and link a page.
Code to Add Clickable Table of Contents to PDF using C#
using System; | |
using System.Collections.Generic; | |
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
class Program | |
{ | |
static void Main(string[] args) // Table of content added in PDF | |
{ | |
new License().SetLicense("License.lic"); | |
// Load the pdf document | |
Document inputDoc = new Document("Document1.pdf"); | |
// Get count of pages in the PDF | |
int count = inputDoc.Pages.Count; | |
// Insert a page for table of contents | |
Page pageTOC = inputDoc.Pages.Insert(1); | |
// Instantiate an object of TocInfo for TOC information | |
TocInfo tocInfo = new TocInfo(); | |
// Create an object of TextFragment for setting TOC title | |
TextFragment title = new TextFragment("Table Of Contents"); | |
title.TextState.FontSize = 20; | |
// Set the title for Table of contents | |
tocInfo.Title = title; | |
pageTOC.TocInfo = tocInfo; | |
// Create a list of strings for TOC | |
List<string> tocTitles = new List<string>(); | |
for(int j = 1; j < count; j++) | |
tocTitles.Add($"Page {j + 1}"); | |
int i = 0; | |
while (i < count) | |
{ | |
// Instantiate an object of the Heading class | |
Heading heading = new Heading(1); | |
TextSegment textSegment = new TextSegment(); | |
heading.TocPage = pageTOC; | |
heading.Segments.Add(textSegment); | |
// Set the destination page for the heading object | |
heading.DestinationPage = inputDoc.Pages[i + 2]; | |
// Set the destination coordinates for TOC item | |
heading.Top = inputDoc.Pages[i + 2].Rect.Height; | |
// Set TOC item text | |
textSegment.Text = tocTitles[i]; | |
// Add heading to the TOC page | |
pageTOC.Paragraphs.Add(heading); | |
i += 1; | |
} | |
// Save PDF Document | |
inputDoc.Save("TOC.pdf"); | |
Console.WriteLine("TOC added successfully"); | |
} | |
} |
This code sample demonstrates how to add table of content in PDF using C#. In this example, TOC contents are generated manually by creating a list of strings using the page numbers. However, you may parse the file contents and use them similarly to the standard table of contents where headings from the PDF contents are used in the TOC and link them with the desired content in the PDF file.
This article has taught us how to create PDF table of contents using C#. To add hyperlinks in the contents of a PDF file, refer to the article on how to add hyperlink in PDF using C#.