Encrypt PDF Documents
- 2 minutes to read
The PDF Document API allows you to encrypt PDF documents and work with documents that are already encrypted.
Use the EncryptionOptions class to configure document encryption settings.
Create an EncryptionOptions object and specify the encryption algorithm, passwords, and document permissions. Pass the configured object to the PdfDocument.encrypt() method to encrypt the PDF document.
Open Encrypted PDF Documents
Pass a password to a PdfDocument constructor to open an encrypted PDF document.
After you open an encrypted document, you can check its encryption settings, access mode, and permissions. Use the following methods to obtain this information:
| Method | Description |
|---|---|
| getAccessMode() | Returns the access level available to the current user. |
| getDataExtractionPermissions() | Returns whether the user can extract document content. |
| getInteractivityPermissions() | Returns whether the user can interact with document elements. |
| getModificationPermissions() | Returns whether the user can modify the document. |
| getPrintPermissions() | Returns whether the user can print the document. |
The following code snippet opens an encrypted PDF document, verifies the current access mode, and inspects document permissions:
try (InputStream documentStream =
Files.newInputStream(Path.of("Document.pdf"));
PdfDocument pdfDocument =
new PdfDocument(documentStream, "YOUR_PASSWORD")) {
// Display the current access mode and document permissions.
System.out.println(
"Access mode: " + pdfDocument.getAccessMode() +
"\nData extraction permissions: " + pdfDocument.getDataExtractionPermissions() +
"\nModification permissions: " + pdfDocument.getModificationPermissions() +
"\nInteractivity permissions: " + pdfDocument.getInteractivityPermissions());
}
The PdfDocument class preserves encryption settings and permissions when you save the PDF document.
Remove Encryption
Call the PdfDocument.removeEncryption() method to remove encryption from a document.