Work with PDF layers using C#

A PDF document may contain various hidden data that could compromise confidentiality or pose potential threats when the file is shared.
By using the HiddenDataSanitizer class, you can remove the hidden data you choose.
You can flexibly configure the sanitization process using the HiddenDataSanitizerOptions class.

HiddenDataSanitizationOptions

This class configures the sanitization process for hidden data in a PDF document.
It allows centralized enabling and configuration of the removal of potentially sensitive, invisible, or unwanted information (annotations, scripts, metadata, attachments, indexes, etc.),
as well as controlling page rasterization and image compression parameters.

It is suitable for scenarios such as:

  • preparing documents for distribution;
  • enhancing security and privacy;
  • simplifying PDF structure (flattening forms and layers);
  • removing hidden content that might not be visible during normal viewing.

Options

  • ImageCompressionOptions: ImageCompressionOptions
    Image compression settings for optimization. Ignored if page rasterization is enabled. Not automatically activated by the All() method — configure manually if needed.

  • ConvertPagesToImages: bool
    Converts pages to raster images. When enabled, ImageCompressionOptions is ignored. Executed after cleaning the main hidden content by other options. Not automatically activated by the All() method.

  • ImageDpi: int (default 150)
    Resolution (DPI) used when converting pages to images.

  • RemoveAnnotations: bool
    Removes all document annotations. Redact annotations will be applied.

  • RemoveSearchIndexAndPrivateInfo: bool
    Removes embedded search indexes and private information.

  • FlattenForms: bool
    Flattens forms: interactive fields become static, non-editable content.

  • FlattenLayers: bool
    Flattens layers: all layers are merged into one, simplifying structure and removing hidden layer data.

  • RemoveJavaScriptsAndActions: bool
    Removes JavaScript and related actions to eliminate potential vulnerabilities.

  • RemoveMetadata: bool
    Removes metadata (document properties and additional embedded info).

  • RemoveAttachments: bool
    Removes all embedded files.

Methods

  • static All(): HiddenDataSanitizationOptions
    Returns a preconfigured instance with the following options enabled:
    RemoveAnnotations, RemoveJavaScriptsAndActions, RemoveMetadata, RemoveAttachments, RemoveSearchIndexAndPrivateInfo, FlattenForms, FlattenLayers;
    and with page conversion to images (ConvertPagesToImages = false) and image processing (ImageCompressionOptions = null) disabled.
    You can manually configure ImageCompressionOptions and ConvertPagesToImages after calling All().

HiddenDataSanitizer

The HiddenDataSanitizer class provides functions for cleaning PDF documents of hidden data (metadata, attachments, annotations, JavaScript, private information, etc.).
It uses a set of options defined in HiddenDataSanitizationOptions to flexibly control the sanitization process.

Static Method SanitizeAllToImages

public static void SanitizeAllToImages(Document document, int dpi = 150)

Converts all PDF document pages to images and then removes any remaining hidden data. This method is useful when you need to completely “seal” a document as raster images, eliminating all hidden elements.

Instance Method Sanitize

public void Sanitize(Document document)

Performs the PDF sanitization according to the options provided in the constructor.

Example Usage