Thêm TOC
The Documentize PDF Manager for .NET là một thành phần mạnh mẽ được thiết kế để nâng cao việc tổ chức và điều hướng tài liệu PDF bằng cách tạo động Mục Lục (TOC). Thành phần này đơn giản hoá quá trình thêm TOC vào PDF của bạn, giúp tài liệu dễ dàng điều hướng và quản lý hơn.
Cách tạo TOC cho PDF
Để tạo Mục Lục trong một tệp PDF, thực hiện các bước sau:
- Tạo một thể hiện của
TocOptionsđể cấu hình các thiết lập tạo TOC. - Đặt Tiêu đề bằng thuộc tính
Title. - Thiết kế Các tiêu đề của TOC bằng phương thức
Headings.Add. - Thêm tệp PDF đầu vào bằng phương thức
AddInput. - Chỉ định tệp PDF đầu ra có TOC bằng phương thức
AddOutput. - Gọi phương thức
AddTableOfContentsđể tạo 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 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);Tạo bookmark trong TOC cho PDF
Bạn có thể sử dụng thuộc tính GenerateBookmarks để tạo bookmark.
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);Cách lấy kết quả dưới dạng 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 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);Tùy chỉnh tiêu đề TOC
Bạn có thể tùy chỉnh tiêu đề Mục Lục bằng cách sửa đổi lớp TocHeading. Ví dụ, bạn có thể sử dụng GenerateNumbering hoặc tự thực hiện. Thuộc tính PageNumber được dùng cho các liên kết trên trang. Ngoài ra bạn cũng có thể sử dụng thuộc tính 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);Các tính năng chính:
- Tạo TOC động: Tạo TOC cho bất kỳ tệp PDF nào với các mục được tạo tự động dựa trên tiêu đề hoặc bookmark.
- Tùy chỉnh: Kiểm soát giao diện và cấu trúc của TOC, bao gồm kiểu, định dạng và mức độ sâu.
- Quy trình làm việc hiệu quả: Giảm thiểu thời gian tạo TOC thủ công, đặc biệt với các tài liệu lớn hoặc phức tạp.