Encrypt and Decrypt PDF Files using Java

encrypt or decrypt pdf 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 using Java. In addition, you will learn how to limit the user’s privileges in a PDF document.

Encrypt or Decrypt PDF Java API – Free Download

Aspose.PDF for Java is a feature-rich PDF manipulation API that lets you work with PDF encryption and decryption seamlessly. In addition, it lets you modify the user’s privileges in the PDF documents programmatically. You can download the API 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>20.9</version>
    <classifier>jdk17</classifier>
</dependency>

Encrypt a PDF File using 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 document using Aspose.PDF for Java.

The following code sample shows how to encrypt a PDF document using Java.

The following dialog appears when you open the encrypted PDF.

encrypted pdf

Decrypt a PDF File using 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 document using Aspose.PDF for Java.

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.

encrypted pdf

Conclusion

In this article, you have learned how to encrypt or decrypt PDF files using Java. In addition, you have seen how to modify the user’s privileges in a PDF document programmatically. You can explore and learn more about the Java PDF API using documentation.

See Also