Sanitize
Documentize PDF Security for .NET 组件允许用户对 PDF 文档进行清理。它提供了简化的流程,以删除元数据、附件、批注、JavaScript、表单、图层、搜索索引或私有内容等敏感或不必要的信息。组件提供了自定义要删除信息的选项。
清除信息
- 元数据
- 附件
- 批注
- Java 脚本
- 表单
- 图层
- 搜索索引
如何清理 PDF 文档
要清理 PDF 文件,请按照以下步骤操作:
- 使用输入文件路径和输出文件路径实例化
SanitizeOptions类。 - 调用
Sanitize方法执行清理。
1// Create SanitizeOptions object to set input and output files
2var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
3// Perform the process
4PdfSecurity.Sanitize(options);如何使用流进行输入和输出
也可以使用提供为流的 PDF 文档进行清理,而不是指定文件路径。
1// Prepare input and output streams
2using var inputStream = File.OpenRead("path_to_your_pdf_file.pdf");
3using var outputStream = new MemoryStream();
4// Create SanitizeOptions object to set input and output streams
5var options = new SanitizeOptions(inputStream, outputStream);
6// Perform the process
7PdfSecurity.Sanitize(options);如何从文件清理到流
可以设置不同类型的输入和输出数据。
1// Prepare output stream
2using var outputStream = new MemoryStream();
3// Create SanitizeOptions object to set input file and output stream
4var options = new SanitizeOptions("path_to_your_pdf_file.pdf", outputStream);
5// Perform the process
6PdfSecurity.Sanitize(options);如何通过属性将文件清理到流
可以使用属性设置不同类型的输入和输出数据。
1// Prepare output stream
2using var outputStream = new MemoryStream();
3// Create SanitizeOptions object
4var options = new SanitizeOptions();
5//Set Input file
6options.Input = new FileData("path_to_your_pdf_file.pdf");
7//Set Output stream
8options.Output = new StreamData(outputStream);
9// Perform the process
10PdfSecurity.Sanitize(options);如何在不删除元数据的情况下进行清理
只需将 RemoveMetadata 属性设为 false。
1// Create SanitizeOptions object to set input and output files
2var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
3options.RemoveMetadata = false;
4// Perform the process
5PdfSecurity.Sanitize(options);如何在不删除附件的情况下进行清理
只需将 RemoveAttachments 属性设为 false。
1// Create SanitizeOptions object to set input and output files
2var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
3options.RemoveAttachments = false;
4// Perform the process
5PdfSecurity.Sanitize(options);如何在将所有页面转换为图像并设置结果 DPI 的情况下进行清理
示例演示如何在清理 PDF 时将所有页面转换为图像并设置结果 DPI。将 ConvertPagesToImages 属性设为 true,如有需要更改 ImageDpi。
1// Create SanitizeOptions object to set input and output files
2var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
3// Turn on conversion and set dpi
4options.ConvertPagesToImages = true;
5options.ImageDpi = 200;
6// Perform the process
7PdfSecurity.Sanitize(options);如何在不删除 JavaScript 和操作的情况下进行清理
只需将 RemoveJavaScriptsAndActions 属性设为 false。
1// Create SanitizeOptions object to set input and output files
2var options = new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf");
3options.RemoveJavaScriptsAndActions = false;
4// Perform the process
5PdfSecurity.Sanitize(options);最简方式清理 PDF 文件
1PdfSecurity.Sanitize(new SanitizeOptions("path_to_your_pdf_file.pdf", "path_to_result_pdf_file.pdf"));关键特性:
- 清理 PDF 文档:清除文档中可能存在的危险数据。
- 可自定义选项:选择要删除的内容和保留的内容。
- 转换为图像:将页面转换为图像,同时保持 PDF 形式。