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 setPermissionsPassword and setPermissions methods of the PdfSaveOptions class, which should be passed to the sign method.

The permissions are int flags of the com.groupdocs.signature.options.Permissions class that can be combined with |:

ConstantMeaning
Permissions.NoneAllow all actions, including printing, modification, and data extraction
Permissions.DenyPrintingDeny printing of the document
Permissions.DenyModificationDeny content modification, including filling forms and adding or modifying annotations
Permissions.DenyDataExtractionDeny extraction of text and graphics from the document
Permissions.DenyAllDeny all actions, including printing, modification, and data extraction

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 call its setPermissionsPassword method (optional). Call the setPermissions method to control user actions (e.g., deny printing or modification).
  • Call the 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
Signature signature = new Signature("sample.pdf");
try
{
    TextSignOptions textSignOptions = new TextSignOptions("JohnSmith");
    textSignOptions.setLeft(0);
    textSignOptions.setTop(100);
    textSignOptions.setWidth(100);
    textSignOptions.setHeight(100);
    textSignOptions.setAllPages(true);
    textSignOptions.setForeColor(Color.BLACK);

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

Color is java.awt.Color.

Permissions of a loaded PDF document

In GroupDocs.Signature for Java, the permissions of the source document cannot be set through LoadOptions. LoadOptions.getPermissions() is read-only: it is filled with the permissions read from the PDF document when the document is loaded, and a value set before loading is replaced. To change the permissions of the signed document, call PdfSaveOptions.setPermissions as shown above.

When you do not set permissions, what happens to the permissions of the source document depends on the password of the signed document:

  • If the signed document is saved with an open password, the permissions of the source document are kept. That is the case when you load a password-protected document and save it without a new password, because setUseOriginalPassword(true) is the default of the save options.
  • If the signed document is saved without an open password, it is saved without permissions. Call PdfSaveOptions.setPermissions to keep the restrictions of such a source document.

Following example demonstrates how to sign a password-protected PDF document and keep its permissions.

// The source document is protected with the open password "1234567890"
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("1234567890");

Signature signature = new Signature("sample.pdf", loadOptions);
try
{
    TextSignOptions textSignOptions = new TextSignOptions("JohnSmith");
    textSignOptions.setLeft(0);
    textSignOptions.setTop(100);
    textSignOptions.setWidth(100);
    textSignOptions.setHeight(100);

    // No save options: the signed copy is saved with the same open password
    // and keeps the permissions of the source document.
    signature.sign("result.pdf", textSignOptions);
}
finally
{
    signature.dispose();
}

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 Java 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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.