Add TOC
The Documentize PDF Manager for .NET is a powerful plugin designed to enhance the organization and navigation of PDF documents by dynamically generating a Table of Contents (TOC). This plugin simplifies the process of adding TOCs to your PDFs, making documents easier to navigate and manage.
How to Generate a TOC for a PDF
To create a Table of Contents in a PDF file, follow these steps:
- Create an instance of
TocOptions
to configure TOC generation settings. - Set a Title using the
Title
property. - Design Headings of TOC using the
Headings.Add
method. - Add the input PDF file using the
AddInput
method. - Specify the output PDF file with the TOC using the
AddOutput
method. - Call the
AddTableOfContents
method to generate the TOC.
1// Create TocOptions object to set instructions
2var options = new TocOptions();
3// Set the Title
4options.Title = "My Table of Contents";
5// Design Headings
6options.Headings.Add(new TocHeading("Introduction", 2));
7options.Headings.Add(new TocHeading("Chapter I", 3));
8options.Headings.Add(new TocHeading("Chapter II", 4));
9options.Headings.Add(new TocHeading("Chapter III", 5));
10// Add input file path
11options.AddInput(new FileDataSource("path_to_your_pdf_file.pdf"));
12// Set output file path
13options.AddOutput(new FileDataSource("path_to_result_pdf_file.pdf"));
14// Perform the process
15PdfManager.AddTableOfContents(options);
Generate bookmarks in a TOC for a PDF
You can use ‘GenerateBookmarks’ property for bookmarks generation.
1// Create TocOptions object to set instructions
2var options = new TocOptions();
3// Set the Title
4options.Title = "My Table of Contents";
5// Generate links in bookmarks
6options.GenerateBookmarks = true;
7// Design Headings
8options.Headings.Add(new TocHeading("Introduction", 2, false, 1));
9options.Headings.Add(new TocHeading("Chapter I", 3, true, 1));
10options.Headings.Add(new TocHeading("Chapter II", 4, true, 1));
11options.Headings.Add(new TocHeading("Example A", 4, true, 2));
12options.Headings.Add(new TocHeading("Example B", 4, true, 2));
13options.Headings.Add(new TocHeading("Example C", 4, true, 2));
14options.Headings.Add(new TocHeading("Example D", 4, true, 2));
15options.Headings.Add(new TocHeading("Chapter III", 5, true, 1));
16// Add input file path
17options.AddInput(new FileDataSource("path_to_your_pdf_file.pdf"));
18// Set output file path
19options.AddOutput(new FileDataSource("path_to_result_pdf_file.pdf"));
20// Perform the process
21PdfManager.AddTableOfContents(options);
How to get Result as Stream
1// Create TocOptions object to set instructions
2var options = new TocOptions();
3// Set the Title
4options.Title = "My Table of Contents";
5// Design Headings
6options.Headings.Add(new TocHeading("Introduction", 2, false, 1));
7// Add input file path
8options.AddInput(new FileDataSource("path_to_your_pdf_file.pdf"));
9// Set output stream
10var outputStream = new MemoryStream();
11options.AddOutput(new StreamDataSource(outputStream));
12options.CloseOutputStreams = false;
13// Perform the process
14PdfManager.AddTableOfContents(options);
Customizing the TOC Heading
You can customize the Table of Contents Heading by modifying the TocHeading
class. For example, you can use ‘GenerateNumbering’ or done it manually.
Property ‘PageNumber’ used for links on pages. Also you can use Level property.
1var heading = new TocHeading();
2heading.Text = "Intro";
3heading.PageNumber = 5;
4heading.GenerateNumbering = true;
5heading.Level = 2;
6
7var tocOptions = new TocOptions();
8options.Headings.Add(heading);
9// Add input and output files
10tocOptions.AddInput(new FileDataSource(@"C:\Samples\input.pdf"));
11tocOptions.AddOutput(new FileDataSource(@"C:\Samples\output_with_toc.pdf"));
12
13// Generate the TOC with customized options
14PdfManager.AddTableOfContents(tocOptions);
Key Features:
- Dynamic TOC Generation: Create a TOC for any PDF file with automatically generated entries based on headings or bookmarks.
- Customization: Control the appearance and structure of the TOC, including styles, formatting, and levels of depth.
- Efficient Workflow: Minimize time spent manually creating TOCs, especially for large or complex documents.