Class PdfSecurity

信息

表示 Documentize.PdfSecurity 组件。用于加密、解密、签名、清理 PDF 文档。

public static class PdfSecurity

继承

objectPdfSecurity

继承成员

方法

Decrypt(DecryptOptions)

解密 PDF 文档。

public static ResultContainer Decrypt(DecryptOptions options)

参数

返回值

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

示例

以下示例演示如何解密 PDF 文档。

// Create DecryptOptions object to set instructions
var options = new DecryptOptions("123456");
// 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
PdfSecurity.Decrypt(options);

异常

ArgumentException

如果未设置 options

Encrypt(EncryptOptions)

加密 PDF 文档。

public static ResultContainer Encrypt(EncryptOptions options)

参数

返回值

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

示例

以下示例演示如何加密 PDF 文档。

// Create EncryptOptions object to set instructions
var options = new EncryptOptions("123456", "qwerty");
// 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
PdfSecurity.Encrypt(options);

异常

ArgumentException

如果未设置 options

Sanitize(SanitizeOptions)

从 PDF 文档中清除隐藏数据,确保元数据、注释、JavaScript 或私有内容等敏感或不必要的信息被移除或转换。

public static ResultContainer Sanitize(SanitizeOptions options)

参数

返回值

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

示例

以下示例演示如何清理 PDF 文档。

// Create SanitizeOptions object to set input and output files
var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示如何在流之间清理 PDF。

// Prepare input and output streams
using var inputStream = File.OpenRead("path_to_your_pdf_file.pdf");
using var outputStream = new MemoryStream();
// Create SanitizeOptions object to set input and output streams
var options = new SanitizeOptions(inputStream, outputStream);
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示如何将文件清理后输出到流。

// Prepare output stream
using var outputStream = new MemoryStream();
// Create SanitizeOptions object to set input and output streams
var options = new SanitizeOptions("path_to_your_pdf_file.pdf", outputStream);
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示如何手动设置输入输出属性进行文件清理。

// Create SanitizeOptions object
var options = new SanitizeOptions();
//Set Input file
options.Input = new FileData("path_to_your_pdf_file.pdf");
//Set Output file
options.Output = new FileData("path_to_result_pdf_file.pdf");
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示在不删除元数据的情况下清理 PDF。

// Create SanitizeOptions object to set input and output files
var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
options.RemoveMetadata = false;
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示在不删除附件的情况下清理 PDF。

// Create SanitizeOptions object to set input and output files
var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
options.RemoveAttachments = false;
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示将所有页面转换为图像并设置结果 DPI。

// Create SanitizeOptions object to set input and output files
var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
// Turn on conversion and set dpi
options.ConvertPagesToImages = true;
options.ImageDpi = 200;
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示在不删除 JavaScript 和动作的情况下清理 PDF。

// Create SanitizeOptions object to set input and output files
var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
options.RemoveJavaScriptsAndActions = false;
// Perform the process
PdfSecurity.Sanitize(options);

以下示例演示以最简方式清理 PDF 文件。

// Perform the process
PdfSecurity.Sanitize(new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf"));

异常

ArgumentException

如果未设置 options

Sign(SignOptions)

使用数字签名对 PDF 文档进行签名。

public static ResultContainer Sign(SignOptions options)

参数

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

返回值

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

示例

以下示例演示如何签名 PDF 文档。

// Create SignOptions object to set instructions
var options = new SignOptions("path_to_your_pfx_file.pfx", "password_of_your_pfx_file");
// 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
PdfSecurity.Sign(options);

以下示例演示如何使用 PFX 文件流签名 PDF 文档。

using var pfxStream = File.OpenRead(@"path_to_your_pfx_file.pfx");
var options = new SignOptions(pfxStream, "password_of_your_pfx_file");
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Perform the process
PdfSecurity.Sign(options);

以下示例演示如何使用不可见签名。

var options = new SignOptions("path_to_your_pfx_file.pfx", "password_of_your_pfx_file");
options.AddInput(new FileData("path_to_your_pdf_file.pdf"));
options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
// Configure invisible signature
signOptions.Visible = false;
// Perform the process
PdfSecurity.Sign(options);

以下示例演示如何添加额外的签名选项。

// Create SignOptions object to set instructions
var options = new SignOptions("path_to_your_pfx_file.pfx", "password_of_your_pfx_file");
// 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"));
// Optional parameters
options.Reason = "my Reason";
options.Contact = "my Contact";
options.Location = "my Location";
options.PageNumber = 3;
// Perform the process
PdfSecurity.Sign(options);

以下示例演示如何为 PDF 签名添加时间戳。

// Create SignOptions object to set instructions
var options = new SignOptions("path_to_your_pfx_file.pfx", "password_for_your_pfx_file");
options.TimestampOptions = new TimestampOptions("server_url");
// 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
PdfSecurity.Sign(options);

异常

ArgumentException

如果未设置 options

Namespace: Documentize Assembly: Documentize.dll

 中文