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

Inheritance

objectPdfManager

Inherited Members

Methods

AddTable(TableOptions)

向 PDF 文档添加表格。

public static ResultContainer AddTable(TableOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何向 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);

Exceptions

ArgumentException

如果未设置 options。

AddTableOfContents(TocOptions)

向 PDF 文档添加目录(TOC)。

public static ResultContainer AddTableOfContents(TocOptions options)

Parameters

  • options TocOptions: 包含操作指令的选项对象。

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何向 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);

Exceptions

ArgumentException

如果未设置 options。

Compress(CompressOptions)

压缩 PDF 文档,尝试减小文档大小。

public static ResultContainer Compress(CompressOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何压缩 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);

Exceptions

ArgumentException

如果未设置 options。

CreatePdfByChatGptRequestAsync(ChatGptRequestOptions)

通过 ChatGpt 的回复创建 PDF 文档。
用于直接向 ChatGPT 发送请求,或通过添加 PDF 文件源并将回复保存到输出源。

public static Task<resultcontainer> CreatePdfByChatGptRequestAsync(ChatGptRequestOptions options)

Parameters

Returns

Task<ResultContainer>

包含操作结果的对象。

Examples

示例演示如何通过添加消息使用 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.

Exceptions

ArgumentException : 如果未设置 options。

Merge(MergeOptions)

合并 PDF 文档。

public static ResultContainer Merge(MergeOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何合并两个 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);

Exceptions

ArgumentException

如果未设置 options。

Optimize(OptimizeOptions)

优化 PDF 文档。
线性化文档以便

  • 尽快打开首页;
  • 尽快显示下一页或通过链接跳转的页面;
  • 在慢速通道上逐页增量显示数据(先显示最有用的数据);
  • 允许用户在整页尚未全部接收和显示之前进行交互(例如点击链接)。
public static ResultContainer Optimize(OptimizeOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何优化 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);

Exceptions

ArgumentException

如果未设置 options。

Resize(ResizeOptions)

调整 PDF 文档页面大小。

public static ResultContainer Resize(ResizeOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何调整 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);

Exceptions

ArgumentException

如果未设置 options。

Rotate(RotateOptions)

旋转 PDF 文档页面。

public static ResultContainer Rotate(RotateOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何旋转 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);

Exceptions

ArgumentException

如果未设置 options。

Split(SplitOptions)

按页拆分 PDF 文档。

public static ResultContainer Split(SplitOptions options)

Parameters

Returns

ResultContainer : 包含操作结果的对象。

Examples

示例演示如何拆分 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);

Exceptions

ArgumentException

如果未设置 options。

Namespace: Documentize Assembly: Documentize.dll

 中文