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 |:
Constant
Meaning
Permissions.None
Allow all actions, including printing, modification, and data extraction
Permissions.DenyPrinting
Deny printing of the document
Permissions.DenyModification
Deny content modification, including filling forms and adding or modifying annotations
Permissions.DenyDataExtraction
Deny extraction of text and graphics from the document
Permissions.DenyAll
Deny 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
Signaturesignature=newSignature("sample.pdf");try{TextSignOptionstextSignOptions=newTextSignOptions("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
PdfSaveOptionssaveOptions=newPdfSaveOptions();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"
LoadOptionsloadOptions=newLoadOptions();loadOptions.setPassword("1234567890");Signaturesignature=newSignature("sample.pdf",loadOptions);try{TextSignOptionstextSignOptions=newTextSignOptions("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: