DEV Community

Alexis
Alexis

Posted on

Java - How to Encrypt or Decrypt PDF Documents

Numerous confidential documents such as contracts, invoices, and other files are saved in PDF format. Sharing these sensitive documents over the internet would be a risk since others may be able to access the document content without your authorization. Encrypting the documents with passwords would be an efficient way to resolve this problem. In this article, I will introduce how to encrypt or decrypt PDF documents in Java using Spire.PDF for Java library.

Add Dependencies

Method 1: If you are using maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>8.7.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Method 2: If you are not using maven, you can download the JAR file from this link, extract the zip file and then import the Spire.Pdf.jar file under the lib folder into your project as a dependency.

Encrypt PDF Documents using Java

A PDF document can be encrypted with two types of passwords: document open password and permissions password.

Document open password
A document open password, also known as a user password, is required to open the PDF.

Permissions password
A permissions password, also known as a master or owner password, is used to change the permission settings of the PDF. This password allows you to restrict certain features in PDF, such as printing, editing, and copying content.

If a PDF document is encrypted with both types of passwords, recipients can open the PDF with either password. However, if they need to change the restrictions you have set, they will need to enter the permissions password.

Spire.PDF enables developers to encrypt a PDF document with open password and permissions password by using the PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize) method. The following are the detail steps to implement this feature:

  • Initialize an instance of PdfDocument class.
  • Load the PDF file using PdfDocument.loadFromFile() method.
  • Encrypt the PDF with open and permissions passwords using PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize) method.
  • Save the resultant PDF using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

import java.util.EnumSet;

public class Encryption {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load the PDF document
        pdf.loadFromFile("Sample.pdf");

        //Encrypt the PDF with open and permissions passwords
        pdf.getSecurity().encrypt("openPassword", "permissionPassword", EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Copy_Content), PdfEncryptionKeySize.Key_256_Bit);

        //Save and close
        pdf.saveToFile("Encryption.pdf");
        pdf.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Decrypt PDF Documents using Java

You can decrypt an encrypted PDF document by setting its open password and permissions password as empty using the PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize, originalPermissionPassword) method, as shown in the steps below.

  • Initialize an instance of PdfDocument class.
  • Load the encrypted PDF with password using PdfDocument.loadFromFile(filePath, password) method.
  • Decrypt the document by setting the open and permissions passwords as empty using PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize, originalPermissionPassword) method.
  • Save the resultant PDF using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class Decryption {
    public static void main(String []args) throws Exception {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load the encrypted PDF with password
        pdf.loadFromFile("Encryption.pdf", "openPassword");

        //Decrypt the PDF by setting the open and permissions password as empty
        pdf.getSecurity().encrypt("", "", PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key_256_Bit, "permissionPassword");

        //Save and close
        pdf.saveToFile("Decryption.pdf");
        pdf.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
phlash profile image
Phil Ashby

Hi Alexis, thanks and it's nice to know that there is a decent PDF engine for Java that can do this, however the PDF standard has issues with security (seclists.org/fulldisclosure/2019/O...), which as far as I can tell have not been addressed by Adobe?

I had a conversation with my financial advisor last year about sending out secure documents, some of which are PDF, but others may not be. Our best option was to enclose the whole document set in a secure ZIP archive (which does not suffer from the same design errors!), something that the Zip4J library can do well 😄