This article provides you with a step-by-step guide and code samples to encrypt and decrypt PDF files programmatically in 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 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 - 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.

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

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

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.

Conclusion

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.

See Also

Add and Verify Digital Signatures in PDF using JavaAdd Data from Database to PDF in C#Add or Remove JavaScript to PDF Files in C#
Add or Remove JavaScript to PDF Files in JavaAdd or Update Hyperlinks in PDF in C#Add or Update Hyperlinks in PDF in Java
Create Graphs and Charts in PDF in C#Create Graphs and Charts in PDF in JavaCreate Tables in PDF Files using C#
Create Tables in PDF Files using JavaCreate Tagged PDF in JavaCreated Tagged PDF Files in C#
Generate Thumbnails for PDF Files in C#Generate Thumbnails for PDF Files in JavaReplace Images in PDF Files in C#
Replace Images in PDF Files in JavaRotate Text inside PDF Documents in C#Rotate Text inside PDF in Java
Encrypt PowerPoint in C#