Class PdfManager
表示 Documentize.PdfManager 组件。用于合并、拆分、优化、旋转、调整大小、压缩 PDF 文档以及向 PDF 文档添加表格、添加目录。可以将多个 PDF 文档合并为一个 PDF。可以将 PDF 文档拆分为单独的页面。可以优化、旋转、调整大小、压缩 PDF 文档。可以旋转、调整 PDF 文档的页面大小。可以向 PDF 文档添加表格。可以向 PDF 文档添加目录。
表示 Documentize.PdfManager 组件。用于合并、拆分、优化、旋转、调整大小、压缩 PDF 文档以及向 PDF 文档添加表格、添加目录。
可以将多个 PDF 文档合并为一个 PDF。
可以将 PDF 文档拆分为单独的页面。
可以优化、旋转、调整大小、压缩 PDF 文档。
可以旋转、调整 PDF 文档的页面大小。
可以向 PDF 文档添加表格。
可以向 PDF 文档添加目录。
public static class PdfManager继承
继承成员
- object.GetType(),
- object.MemberwiseClone(),
- object.ToString(),
- object.Equals(object?),
- object.Equals(object?, object?),
- object.ReferenceEquals(object?, object?),
- object.GetHashCode()
方法
AddTable(TableOptions)
向 PDF 文档添加表格。
public static ResultContainer AddTable(TableOptions options)参数
optionsTableOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何向 PDF 文件添加表格。
// Configure table options
var options = new TableOptions();
options.InsertPageBefore(1)
.AddTable()
.AddRow()
.AddCell().AddParagraph("Name")
.AddCell().AddParagraph("Age")
.AddRow()
.AddCell().AddParagraph("Bob")
.AddCell().AddParagraph("12")
.AddRow()
.AddCell().AddParagraph("Sam")
.AddCell().AddParagraph("20")
.AddRow()
.AddCell().AddParagraph("Sandy")
.AddCell().AddParagraph("26")
.AddRow()
.AddCell().AddParagraph("Tom")
.AddCell().AddParagraph("12")
.AddRow()
.AddCell().AddParagraph("Jim")
.AddCell().AddParagraph("27");
// Add input file path
options.AddInput(new FileData("path_to_input.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_output.pdf"));
// Perform the process
PdfManager.AddTable(options);示例展示如何在第 2 页之前向 PDF 文件添加表格。
// Configure table options
var options = new TableOptions();
options.InsertPageBefore(2) // Add table before page 2
.AddTable()
.AddRow()
.AddCell().AddParagraph("Name")
.AddCell().AddParagraph("Age");
// Add input file path
options.AddInput(new FileData("path_to_input.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_output.pdf"));
// Perform the process
PdfManager.AddTable(options);异常
如果未设置 options。
AddTableOfContents(TocOptions)
向 PDF 文档添加目录(TOC)。
public static ResultContainer AddTableOfContents(TocOptions options)参数
optionsTocOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何向 PDF 文件添加目录。
// Create TocOptions object to set instructions
var options = new TocOptions();
// Set the Title
options.Title = "My Table of Contents";
// Design Headings
options.Headings.Add(new TocHeading("Introduction", 2));
options.Headings.Add(new TocHeading("Chapter I", 3));
options.Headings.Add(new TocHeading("Chapter II", 4));
options.Headings.Add(new TocHeading("Chapter III", 5));
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.AddTableOfContents(options);示例展示如何向 PDF 文件添加目录并生成书签。
// Create TocOptions object to set instructions
var options = new TocOptions();
// Set the Title
options.Title = "My Table of Contents";
// Generate links in bookmarks
options.GenerateBookmarks = true;
// Design Headings
options.Headings.Add(new TocHeading("Introduction", 2, false, 1));
options.Headings.Add(new TocHeading("Chapter I", 3, true, 1));
options.Headings.Add(new TocHeading("Chapter II", 4, true, 1));
options.Headings.Add(new TocHeading("Example A", 4, true, 2));
options.Headings.Add(new TocHeading("Example B", 4, true, 2));
options.Headings.Add(new TocHeading("Example C", 4, true, 2));
options.Headings.Add(new TocHeading("Example D", 4, true, 2));
options.Headings.Add(new TocHeading("Chapter III", 5, true, 1));
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.AddTableOfContents(options);示例展示如何向 PDF 文件添加目录并保存为流。
// Create TocOptions object to set instructions
var options = new TocOptions();
// Set the Title
options.Title = "My Table of Contents";
// Design Headings
options.Headings.Add(new TocHeading("Introduction", 2, false, 1));
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output stream
var outputStream = new MemoryStream();
options.AddOutput(new StreamData(outputStream));
options.CloseOutputStreams = false;
// Perform the process
PdfManager.AddTableOfContents(options);示例展示如何自定义目录标题来添加目录。
// Create TocOptions object to set instructions
var heading = new TocHeading();
heading.Text = "Intro";
heading.PageNumber = 5;
heading.GenerateNumbering = true;
heading.Level = 2;
var tocOptions = new TocOptions();
tocOptions.Headings.Add(heading);
// Add input and output files
tocOptions.AddInput(new FileData("path_to_your_pdf_file.pdf"));
tocOptions.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Generate the TOC with customized options
PdfManager.AddTableOfContents(tocOptions);异常
如果未设置 options。
Compress(CompressOptions)
压缩 PDF 文档,尝试减小文档大小。
public static ResultContainer Compress(CompressOptions options)参数
optionsCompressOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何压缩 PDF 文档。
// Create CompressOptions object to set instructions
var options = new CompressOptions();
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.Compress(options);异常
如果未设置 options。
CreatePdfByChatGptRequestAsync(ChatGptRequestOptions)
通过 ChatGpt 的回复创建 PDF 文档。
用于直接向 ChatGPT 发送请求或添加 PDF 文件来源并将回复保存到输出来源。
public static Task<resultcontainer> CreatePdfByChatGptRequestAsync(ChatGptRequestOptions options)参数
optionsChatGptRequestOptions: 包含操作指令的选项对象。
返回值
包含操作结果的对象。
示例
示例展示如何通过向请求添加消息来使用 ChatGpt。
var options = new ChatGptRequestOptions();
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
options.ApiKey = "Your API key."; // You need to provide the key to access the API.
options.MaxTokens = 1000; // The maximum number of tokens to generate in the chat completion.
// Add the request messages.
options.Messages.Add(new Message
{
Content = "You are a helpful assistant.",
Role = Role.System
});
options.Messages.Add(new Message
{
Content = "What is the biggest pizza diameter ever made?",
Role = Role.User
});
// Process the request.
var result = await PdfManager.CreatePdfByChatGptRequestAsync(options);
var fileResultPath = result.ResultCollection[0].Data;
var chatCompletionObject = result.ResultCollection[1].Data as ChatCompletion; // The ChatGPT API chat completion object.示例展示如何通过仅添加一条消息来使用 ChatGpt。
var options = new ChatGptRequestOptions();
options.AddOutput(new FileData("path_to_result_pdf_file.pdf")); // Add the output file path.
options.ApiKey = "Your API key."; // You need to provide the key to access the API.
options.MaxTokens = 1000; // The maximum number of tokens to generate in the chat completion.
// Add the request message.
// In this case, the system message with Content = "You are a helpful assistant." is added by default.
// The role of the query message is "user" by default.
options.Query = "What is the lowest temperature recorded on the Earth?";
// Process the request.
var result = await PdfManager.CreatePdfByChatGptRequestAsync(options);
var fileResultPath = result.ResultCollection[0].Data;
var chatCompletionObject = result.ResultCollection[1].Data as ChatCompletion; // The ChatGPT API chat completion object.示例展示如何通过将文件作为消息来源来使用 Chat。
var options = new ChatGptRequestOptions();
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Add the PDF text source.
// In case of multiple sources, the text from each document will be added to the request message collection
// as a separate message with the role "user".
options.AddInput(new FileData("TextSource.pdf"));
options.ApiKey = "Your API key."; // You need to provide the key to access the API.
options.MaxTokens = 1000; // The maximum number of tokens to generate in the chat completion.
// Add the request message.
// In this case, the system message with Content = "You are a helpful assistant." is added by default.
// The role of the query message is "user" by default.
options.Query = "How many letters in the provided text?";
// Process the request.
var result = await PdfManager.CreatePdfByChatGptRequestAsync(options);
var fileResultPath = result.ResultCollection[0].Data;
var chatCompletionObject = result.ResultCollection[1].Data as ChatCompletion; // The ChatGPT API chat completion object.异常
ArgumentException : 如果未设置 options。
Merge(MergeOptions)
合并 PDF 文档。
public static ResultContainer Merge(MergeOptions options)参数
optionsMergeOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何合并两个 PDF 文档。
// Create MergeOptions object to set instructions
var options = new MergeOptions();
// Add input file paths
options.AddInput(new FileData("path_to_your_pdf_file_1.pdf"));
options.AddInput(new FileData("path_to_your_pdf_file_2.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.Merge(options);异常
如果未设置 options。
Optimize(OptimizeOptions)
优化 PDF 文档。
线性化文档,以便:
- 尽快打开第一页;
- 尽快显示下一页或跟随链接到下一页;
- 当页面数据通过慢速通道传输时,逐步增量显示页面(优先显示最有用的数据);
- 允许用户交互(如点击链接)在整个页面接收并显示之前就能进行。
public static ResultContainer Optimize(OptimizeOptions options)参数
optionsOptimizeOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何优化 PDF 文档。
// Create OptimizeOptions object to set instructions
var options = new OptimizeOptions();
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.Optimize(options);异常
如果未设置 options。
Resize(ResizeOptions)
调整 PDF 文档的页面大小。
public static ResultContainer Resize(ResizeOptions options)参数
optionsResizeOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何调整 PDF 文档的页面大小。
// Create ResizeOptions object to set instructions
var options = new ResizeOptions();
// Set new PageSize
options.PageSize = PageSize.A3;
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.Resize(options);异常
如果未设置 options。
Rotate(RotateOptions)
旋转 PDF 文档的页面。
public static ResultContainer Rotate(RotateOptions options)参数
optionsRotateOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何旋转 PDF 文档。
// Create RotateOptions object to set instructions
var options = new RotateOptions();
// Set new Rotation
options.Rotation = Rotation.On90;
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file path
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfManager.Rotate(options);异常
如果未设置 options。
Split(SplitOptions)
按页面拆分 PDF 文档。
public static ResultContainer Split(SplitOptions options)参数
optionsSplitOptions: 包含操作指令的选项对象。
返回值
ResultContainer : 包含操作结果的对象。
示例
示例展示如何拆分 PDF 文档。
// Create SplitOptions object to set instructions
var options = new SplitOptions();
// Add input file path
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
// Set output file paths
options.AddOutput(new FileData("path_to_result_pdf_file_1.pdf"));
options.AddOutput(new FileData("path_to_result_pdf_file_2.pdf"));
// Perform the process
PdfManager.Split(options);异常
如果未设置 options。
Namespace: Documentize Assembly: Documentize.dll