How to Sanitize PDF
Sanitizing PDFs is a crucial process with many benefits. Primarily, it enhances document security by removing potentially harmful elements like embedded scripts or metadata, thereby reducing the risk of exploitation by malicious entities. Additionally, it improves compatibility across different platforms by removing complex or proprietary elements, enhancing accessibility. By mitigating risks of data leakage and ensuring document integrity, sanitizing PDFs contributes significantly to overall security and trustworthiness in document management practices.
How to Sanitize PDF in C#
- Download C# library to sanitize PDFs
- Use the Cleaner class to sanitize PDFs in multiple ways
- Scan the PDFs using the
ScanPdf
method - Provide a custom YARA file that meets the requirements
- Recieve the new sanitized PDF document
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLSanitize PDF Example
The trick behind sanitizing a PDF is to convert the PDF document into a type of image, which removes JavaScript code, embedded objects, and buttons, and then convert it back to a PDF document. We provide Bitmap and SVG image types. The key differences of SVG from Bitmap are:
- Quicker than sanitizing with a bitmap
- Results in a searchable PDF
- Layout might be inconsistent
: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")
Scan PDF Example
Use the ScanPdf
method of the Cleaner class to check if the PDF has any potential vulnerabilities. This method will check with the default YARA file. However, feel free to upload a custom YARA file that meets your requirements to the second parameter of the method.
A YARA file for PDF documents contains rules or patterns used to identify characteristics associated with malicious PDF files. These rules help security analysts automate the detection of potential threats and take appropriate actions to mitigate risks.
: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)