This article provides you with a step-by-step guide and code samples to encrypt and decrypt PDF files programmatically in Java.
In various cases, PDF documents are encrypted before they are shared among the stakeholders. The encryption makes the document secure from unauthorized access as well as content tampering. There are a couple of popular ways of securing PDF documents - encrypting PDF with a password or limiting the user’s access permissions such as printing, editing, copying, etc. This article demonstrates how to encrypt or decrypt a PDF in Java. In addition, you will learn how to limit the user’s privileges in a PDF document.
- Java Library to Encrypt or Decrypt PDF
- Encrypt a PDF File using Java
- Decrypt a PDF File using Java
- Change Security Permissions in a PDF using Java
Java Library to Encrypt or Decrypt PDF - Free Download
Aspose.PDF for Java is a feature-rich PDF manipulation library that lets you work with PDF encryption and decryption seamlessly. In addition, it lets you modify the user’s privileges in PDF documents programmatically. You can download the library for free or get it installed using the following Maven configurations.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>22.12</version> <classifier>jdk17</classifier> </dependency>
Encrypt a PDF File in Java
Encryption of a PDF document requires you to set a password that will be used to open and access the document. Along with this, you can choose one of the following cryptographic algorithms to be used in the encryption.
The following are the steps to encrypt a PDF file in Java.
- Load the PDF document using Document class.
- Encrypt PDF using Document.encrypt(“user_password”, “owner_password”, 0, CryptoAlgorithm) method.
- Save the encrypted PDF using the Document.save(string) method.
The following code sample shows how to encrypt a PDF document using Java.
The following dialog appears when you open the encrypted PDF.
Decrypt a PDF File in Java
The decryption of an encrypted PDF document requires the user’s or owner’s password. The following are the steps to decrypt a PDF file in Java.
- Load the encrypted PDF using the Document class by specifying the password in the constructor.
- Call Document.decrypt() method to decrypt the PDF.
- Save the decrypted PDF document using the Document.save(string) method.
The following code sample shows how to decrypt an encrypted PDF using Java.
Change Security Permissions in a PDF using Java
You can also limit the user’s access to perform different operations while encrypting the PDF documents. The following are the permissions you can set.
- Print Document – Allows document’s printing.
- Modify Content – Allows modification in the content.
- Extract Content – Allows copying the content from the document.
- Modify Text Annotations – Allows adding or updating text annotations.
- Fill Form – Allows filling in the interactive form fields.
- Extract Content with Disabilities – Allows extracting text and graphics (for users with disabilities).
- Assemble Document – Allows inserting, rotating, or deleting pages and creating bookmarks or thumbnail images.
- Printing Quality – Allows high-resolution printing of documents.
The following are the steps to set or modify permissions in a PDF document.
- Load the PDF using the Document class.
- Use the DocumentPrivilege class to set the permissions.
- Encrypt the PDF and save it as you have done in the previous examples.
The following code sample shows how to modify permissions in a PDF document using Java.
The following are the permissions in the encrypted PDF file.
Free Java PDF Encryption Library
You can get a free temporary license and encrypt or decrypt PDF files without any limitations.
Explore Java PDF Library
You can explore and learn more about the Java PDF library using documentation.
In this article, you have learned how to encrypt or decrypt PDF files in Java. In addition, you have seen how to modify the user’s privileges in a PDF document programmatically in Java. You can easily use the provided code samples in your Java applications.