Add TOC
Documentize PDF Manager for .NET 是一款强大的组件,旨在通过动态生成目录(TOC)来提升 PDF 文档的组织性和导航性。该组件简化了向 PDF 添加目录的过程,使文档更易于浏览和管理。
如何为 PDF 生成目录
要在 PDF 文件中创建目录,请按照以下步骤操作:
- 创建
TocOptions实例,以配置目录生成设置。 - 使用
Title属性设置标题。 - 通过
Headings.Add方法设计目录的章节标题。 - 使用
AddInput方法添加输入 PDF 文件。 - 使用
AddOutput方法指定带目录的输出 PDF 文件。 - 调用
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();
8tocOptions.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 文件创建目录,依据标题或书签自动生成条目。
- 高度可定制:控制目录的外观和结构,包括样式、格式以及层级深度。
- 高效工作流:大幅减少手动创建目录的时间,尤其适用于大型或结构复杂的文档。