How to Sanitize PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

清理 PDF 是一个具有许多好处的关键过程。 首先,通过删除潜在的有害元素,如嵌入的脚本或元数据,从而减少被恶意实体利用的风险,提升了文档安全性。 此外,通过去除复杂或专有元素,提高了跨不同平台的兼容性,从而增强了可访问性。 通过降低数据泄漏风险并确保文档完整性,清理 PDF 显著提高了文档管理实践中的整体安全性和可信度。

快速入门:使用 IronPDF 轻松清理 PDF

使用 IronPDF 的 Cleaner 类可以轻松增强 PDF 文档的安全性。 本快速入门指南演示了如何使用 C# .NET 以最少的代码清理 PDF。 通过利用 ScanPdf 方法,您可以快速消除如嵌入脚本和元数据等漏洞,确保 PDF 的完整性和安全性。 遵循代码片段,快速清理您的 PDF 并防范潜在威胁。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    IronPdf.Cleaner.SanitizeWithSvg(PdfDocument.FromFile("input.pdf")).SaveAs("sanitized.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5 步)

  1. 从 NuGet 下载 IronPDF 库
  2. 使用 Cleaner 类以多种方式清理 PDF
  3. 使用 ScanPdf 方法扫描 PDF
  4. 提供一份符合要求的自定义 YARA 文件
  5. 接收新的清理后的 PDF 文档


清理 PDF 示例

清理 PDF 背后的秘诀是将 PDF 文档转换为一种图像类型,这可移除 JavaScript 代码、嵌入对象和按钮,然后再将其转换回 PDF 文档。 我们提供 Bitmap 和 SVG 图像类型。SVG 与 Bitmap 的关键区别是:

  • 比使用 Bitmap 清理更快
  • 生成可搜索的 PDF
  • 布局可能不一致
:path=/static-assets/pdf/content-code-examples/how-to/sanitize-pdf-sanitize-pdf.cs
using IronPdf;

// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Sanitize with Bitmap
PdfDocument sanitizeWithBitmap = Cleaner.SanitizeWithBitmap(pdf);

// Sanitize with SVG
PdfDocument sanitizeWithSvg = Cleaner.SanitizeWithSvg(pdf);

// Export PDFs
sanitizeWithBitmap.SaveAs("sanitizeWithBitmap.pdf");
sanitizeWithSvg.SaveAs("sanitizeWithSvg.pdf");
Imports IronPdf

' Import PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Sanitize with Bitmap
Private sanitizeWithBitmap As PdfDocument = Cleaner.SanitizeWithBitmap(pdf)

' Sanitize with SVG
Private sanitizeWithSvg As PdfDocument = Cleaner.SanitizeWithSvg(pdf)

' Export PDFs
sanitizeWithBitmap.SaveAs("sanitizeWithBitmap.pdf")
sanitizeWithSvg.SaveAs("sanitizeWithSvg.pdf")
$vbLabelText   $csharpLabel

带选项的清理

除了清理 PDF 外,IronPDF 还允许我们使用 ChromeRenderOptions 来清理 PDF,这使得可以修改参数如页边距、纸张大小和纸张方向。

SanitizeWithBitmapSanitizeWithSvg 均可接收第二个可选参数,即 ChromeRenderOptions 对象。 这里是一个简短示例,展示如何将 PDF 的目标下边距设置为50像素,通过将 MarginBottom 属性设置为 50 像素。

欲了解完整的可用选项列表,请参考 这里

:path=/static-assets/pdf/content-code-examples/how-to/santize-pdf-sanitize-chrome-render-options.cs
using IronPdf;

// Customize Chrome render options
var options = new ChromePdfRenderOptions();

// Set bottom margin to 50 pixels
options.MarginBottom = 50;

// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Sanitize with Bitmap with Chrome render options
PdfDocument sanitizeWithBitmap = Cleaner.SanitizeWithBitmap(pdf, options);

// Sanitize with SVG with Chrome render options
PdfDocument sanitizeWithSvg = Cleaner.SanitizeWithSvg(pdf, options);

// Export PDFs
sanitizeWithBitmap.SaveAs("sanitizeWithBitmap.pdf");
sanitizeWithSvg.SaveAs("sanitizeWithSvg.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

扫描 PDF 示例

使用 Cleaner 类的 ScanPdf 方法检查 PDF 是否有潜在漏洞。 此方法将使用默认的 YARA 文件进行检查。不过,欢迎上传符合您需求的自定义 YARA 文件到方法的第二个参数。

用于 PDF 文档的 YARA 文件包含用于识别与恶意 PDF 文件相关的特征的规则或模式。 这些规则帮助安全分析师自动检测潜在威胁并采取适当行动来降低风险。

:path=/static-assets/pdf/content-code-examples/how-to/sanitize-pdf-scan-pdf.cs
using IronPdf;
using System;

// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Scan PDF
CleanerScanResult result = Cleaner.ScanPdf(pdf);

// Output the result
Console.WriteLine(result.IsDetected);
Console.WriteLine(result.Risks.Count);
Imports IronPdf
Imports System

' Import PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Scan PDF
Private result As CleanerScanResult = Cleaner.ScanPdf(pdf)

' Output the result
Console.WriteLine(result.IsDetected)
Console.WriteLine(result.Risks.Count)
$vbLabelText   $csharpLabel

准备好看看您还能做些什么吗? 查看我们的教程页面:签名和保护PDFs

常见问题解答

如何在 C# 中净化 PDF?

要在 C# 中净化 PDF,使用 IronPDF 库的 Cleaner 类。这涉及将 PDF 转换为如 SVG 或位图的图像格式以去除有害元素,然后再将其转换回 PDF。

IronPDF 提供哪些方法来净化 PDF?

IronPDF 在 Cleaner 类中提供了诸如 SanitizeWithBitmapSanitizeWithSvg 的方法来净化 PDF。这些方法通过将 PDF 转换为图像再转回的过程来去除脚本和嵌入对象。

使用 SVG 进行 PDF 净化有什么好处?

使用 SVG 进行 PDF 净化更快,并且会生成可搜索的 PDF。然而,与使用位图相比,它可能会导致布局不一致。

如何使用自定义 YARA 文件扫描 PDF?

要在 IronPDF 中使用自定义 YARA 文件,将该文件作为第二个参数传递给 ScanPdf 方法。这允许您根据自定义安全规则检查特定漏洞。

Cleaner 类在 PDF 净化中有什么作用?

IronPDF 中的 Cleaner 类旨在通过去除潜在有害元素来净化 PDF。它提供了将 PDF 转换为图像的方法,这有助于去除脚本和嵌入对象。

在 PDF 净化过程中,可以修改文档参数吗?

是的,您可以使用 IronPDF 的 ChromeRenderOptions 修改 PDF 净化过程中的文档参数。这允许更改边距、纸张大小和方向。

ScanPdf 方法如何提高 PDF 的安全性?

IronPDF 中的 ScanPdf 方法通过使用 YARA 规则检查 PDF 漏洞来增强安全性。它可以识别潜在威胁,从而帮助减轻恶意 PDF 的风险。

为什么 PDF 净化很重要?

PDF 净化对于通过去除有害元素(如脚本或元数据)来增强文档安全性至关重要。它减少了利用风险,提高了兼容性,并确保文档完整性。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布