How to protect a signed PDF document

How to secure PDF document with permissions

Signature class supports saving signed documents with permission settings and password protection for access and modification. This capability is achieved using the PermissionsPassword and Permissions properties of the PdfSaveOptions class, which should be passed to Sign method.

You can secure a PDF document by setting the following parameters:

  • Create new instance of Signature class and pass source document path or stream as a constructor parameter.
  • Instantiate required signature options.
  • Instantiate the PdfSaveOptions object and specify PermissionsPassword property(optional). Configure the Permissions to control user actions (e.g., deny printing or modification).
  • Call Sign method of Signature class instance and pass signature options and PdfSaveOptions object to it.

Following example demonstrates how to save signed document with permissions.

// Using the Signature class to create a signature for the specified PDF document
using (Signature signature = new Signature("sample.pdf"))
{
    TextSignOptions textSignOptions = new TextSignOptions("JohnSmith")
    {
        Left = 0,
        Top = 100,
        Width = 100,
        Height = 100,
        AllPages = true,
        ForeColor = Color.Black
    };

    // Create a new PdfSaveOptions object to configure the PDF save settings
    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.OverwriteExistingFiles = false;
    // Set permissions to deny printing and modification
    saveOptions.Permissions = Permissions.DenyPrinting | Permissions.DenyModification;
    // Set a password for modifying permissions(optional)
    saveOptions.PermissionsPassword = "0987654321";
    // Sign the document and save the result to a new file
    signature.Sign("result.pdf", textSignOptions, saveOptions);
}

How to load PDF document with permissions

The Signature class supports loading documents with specific permissions that are preserved after signing. This capability is managed through the LoadOptions class, where the Permissions property is set when loading the document. These permissions remain enforced in the signed document, ensuring consistent restrictions from loading to final save output when passed to the Sign method.

To load a PDF document with permissions and ensure those permissions are preserved throughout the signing process, follow these steps:

  • Create a new instance of LoadOptions , configuring the Permissions property to set restrictions (e.g., deny printing, modification, or data extraction).
  • Use this LoadOptions instance to initialize the Signature class with the document path or stream.
  • Instantiate the required signature options (e.g.,  TextSignOptions ) to set signature position and appearance.
  • Call the Sign method of the Signature class, passing the signature options. The document will retain the permissions specified in the LoadOptions , ensuring any restrictions applied during loading are preserved in the signed document.

Following example demonstrates how to load PDF document with permissions.

// Set up load options with specific permissions, denying all actions (printing, modification, extraction)
LoadOptions loadOptions = new LoadOptions()
{
    Permissions = Permissions.DenyAll
};

// Using the Signature class to create a signature for the specified PDF document
using (Signature signature = new Signature("sample.pdf", loadOptions))
{
    TextSignOptions textSignOptions = new TextSignOptions("JohnSmith")
    {
        Left = 0,
        Top = 100,
        Width = 100,
        Height = 100
    };
    
    signature.Sign("result.pdf", textSignOptions);
}

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.