Click or drag to resize
Pdf Library for .NET

PdfSanitizeManager Class

Sanitizes a PDF document from an untrusted source: removes what could run, launch, phone home or hide data, while the pages look the same.
Inheritance Hierarchy
SystemObject
  SelectPdf.UniversalPdfSanitizeManager

Namespace: SelectPdf.Universal
Assembly: SelectPdf.Universal (in SelectPdf.Universal.dll) Version: 26.6
Syntax
public class PdfSanitizeManager : IDisposable

The PdfSanitizeManager type exposes the following members.

Constructors
 NameDescription
Public methodPdfSanitizeManagerInitializes a new instance of the PdfSanitizeManager class. Call one of the Load overloads first.
Top
Properties
 NameDescription
Public propertyOptionsGets or sets what is removed (Clean mode). Default by default.
Public propertyRasterize Gets or sets whether each page is replaced by an image of itself (a new document built from the rendered pages) instead of removing the dangerous objects. False by default. Options does not apply then: nothing of the original is kept, its text included.
Public propertyResolutionGets or sets the resolution the pages are rendered at with Rasterize, in dpi (36 to 600; 150 by default).
Top
Methods
 NameDescription
Public methodCloseCloses the loaded document.
Public methodDisposeReleases the loaded document, the same as Close; calling it more than once does nothing.
Public methodGetDocumentReturns the sanitized document as a new PdfDocument; the document is sanitized first when Sanitize was not called.
Public methodLoad(Byte)Loads a PDF document from a byte array.
Public methodLoad(PdfDocument)Loads an existing PdfDocument. It is not changed: the result is saved with Save(String) or obtained from GetDocument.
Public methodLoad(Stream)Loads a PDF document from a stream.
Public methodLoad(String)Loads a PDF document from a file.
Public methodLoad(Byte, String)Loads a password-protected PDF document from a byte array.
Public methodLoad(PdfDocument, String)Loads an existing password-protected PdfDocument.
Public methodLoad(Stream, String)Loads a password-protected PDF document from a stream.
Public methodLoad(String, String)Loads a password-protected PDF document from a file.
Public methodSanitize Sanitizes the document: removes what Options names, or with Rasterize replaces every page with an image of itself. The result is what Save writes.
Public methodSaveSaves the sanitized document and returns its bytes; the document is sanitized first when Sanitize was not called.
Public methodSave(Stream)Saves the sanitized document to a stream; the document is sanitized first when Sanitize was not called.
Public methodSave(String)Saves the sanitized document to a file; the document is sanitized first when Sanitize was not called.
Public methodScanReports everything the document holds of every kind PdfSanitizeOptions names; changes nothing.
Top
Remarks

Two ways. By default the dangerous objects are removed (Options says which) and the file is written again, so the text stays searchable and the vectors, fonts, internal links and tags stay as they were. With Rasterize each page is rendered as an image at Resolution and a new document is built from the images: nothing of the original survives, at the cost of text, size and sharpness.

Always. The document is written whole, so earlier revisions hidden in the file are gone, and its signatures are removed (no signature survives a rewrite) and reported in Signatures. An encrypted document keeps its passwords and permissions in the default way; a rasterized one is not encrypted.

Scan only reports what the document holds, so a server can refuse a file instead of cleaning it.

Example
using (PdfSanitizeManager sanitizer = new PdfSanitizeManager())
{
    sanitizer.Load("upload.pdf");
    PdfSanitizeReport found = sanitizer.Scan();
    if (found.HasActiveContent)
    {
        PdfSanitizeReport removed = sanitizer.Sanitize();
        sanitizer.Save("clean.pdf");
    }
}
See Also