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