添加目录

Documentize PDF Manager for .NET 是一个强大的组件,旨在通过动态生成目录(TOC)来增强 PDF 文档的组织和导航。该组件简化了向 PDF 添加目录的过程,使文档更易于导航和管理。

如何为 PDF 生成目录

要在 PDF 文件中创建目录,请按以下步骤操作:

  1. 创建 TocOptions 实例以配置目录生成设置。
  2. 使用 Title 属性设置标题。
  3. 使用 Headings.Add 方法设计目录标题。
  4. 使用 AddInput 方法添加输入 PDF 文件。
  5. 使用 AddOutput 方法指定带目录的输出 PDF 文件。
  6. 调用 AddTableOfContents 方法生成目录。
 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 FileData("path_to_your_pdf_file.pdf"));
12// Set output file path
13options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
14// Perform the process
15PdfManager.AddTableOfContents(options);

为 PDF 目录生成书签

可以使用 GenerateBookmarks 属性生成书签。

 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 FileData("path_to_your_pdf_file.pdf"));
18// Set output file path
19options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
20// Perform the process
21PdfManager.AddTableOfContents(options);

如何将结果作为流获取

 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 FileData("path_to_your_pdf_file.pdf"));
 9// Set output stream 
10var outputStream = new MemoryStream();
11options.AddOutput(new StreamData(outputStream));
12options.CloseOutputStreams = false;
13// Perform the process
14PdfManager.AddTableOfContents(options);

自定义目录标题

您可以通过修改 TocHeading 类来自定义目录标题。例如,您可以使用 GenerateNumbering 或手动完成。PageNumber 属性用于页面链接,也可以使用 Level 属性。

 1// Create TocOptions object to set instructions
 2var heading = new TocHeading();
 3heading.Text = "Intro";
 4heading.PageNumber = 5;
 5heading.GenerateNumbering = true;
 6heading.Level = 2;
 7var tocOptions = new TocOptions();
 8options.Headings.Add(heading);
 9// Add input and output files
10tocOptions.AddInput(new FileData("path_to_your_pdf_file.pdf"));
11tocOptions.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
12// Generate the TOC with customized options
13PdfManager.AddTableOfContents(tocOptions);

关键特性

  • 动态目录生成:为任何 PDF 文件创建目录,自动根据标题或书签生成条目。
  • 自定义:控制目录的外观和结构,包括样式、格式和深度层级。
  • 高效工作流:减少手动创建目录所花费的时间,尤其适用于大型或结构复杂的文档。
 中文