Java: How to encrypt or decrypt PDF documents?

 In daily work, we encrypt important documents and set user access permissions, which are not accessible to other external personnel. Only users who have obtained this permission are eligible to open documents. In addition, restricting users' access greatly prevents those who intentionally tamper with or copy the content, thereby improving the security of the document. At the same time, another major role of document encryption is to prevent loss, as there may be situations where employees intentionally or unintentionally delete documents during business trips or resignations, resulting in document loss and causing a certain impact on the company's business and image. So how can we encrypt PDF documents? How to decrypt a document after encrypting it? The following article provides a detailed introduction to how to programmatically encrypt or decrypt PDF documents through Java code.

  • Encrypt a PDF File with Password
  • Remove Password to Decrypt a PDF File

Programming Environment

Method 1:

Introduced manually. Download Free Spire.PDF for Java locally, unzip it, and find the Spire.PDF.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

If you use 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Encrypt a PDF File with Password

There are two kinds of passwords for encrypting a PDF file - open password and permission password. The former is set to open the PDF file, while the latter is set to restrict printing, contents copying, commenting, etc. If a PDF file is secured with both types of passwords, it can be opened with either password.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.loadFromFile() method.
  • Set open password, permission password, encryption key size and permissions.
  • Encrypt the PDF file using PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet<PdfPermissionsFlags> permissions, PdfEncryptionKeySize keySize) method.
  • Save the result file using PdfDocument.saveToFile () method.

Full Code

Java

import java.util.EnumSet;
import
com.spire.pdf.PdfDocument;
import
com.spire.pdf.security.PdfEncryptionKeySize;
import
com.spire.pdf.security.PdfPermissionsFlags;

public class
EncryptPDF {

   
public static void main(String[] args) {

       
//Create a PdfDocument instance
       
PdfDocument pdf = new PdfDocument();

       
//Load a sample PDF file
       
pdf.loadFromFile("E:\\Files\\sample.pdf");

       
//Encrypt the file
       
PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;
       
String openPassword = "e-iceblue";
       
String permissionPassword = "test";
       
EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);
       
pdf.getSecurity().encrypt(openPassword, permissionPassword, flags, keySize);

       
//Save and close
       
pdf.saveToFile("Encrypt.pdf");
       
pdf.close();

   
}

}

Effective Shot



Remove Password to Decrypt a PDF File

When you need to remove the password from a PDF file, you can set the open password and permission password to empty. The detailed steps are as follows.

  • Create a PdfDocument object.
  • Load the encrypted PDF file with password using PdfDocument.loadFromFile(java.lang.String filename, java.lang.String password) method.
  • Decrypt the PDF file by setting the open password and permission password to empty using PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet<PdfPermissionsFlags> permissions, PdfEncryptionKeySize keySize, java.lang.String originalPermissionPassword) method.
  • Save the result file using PdfDocument.saveToFile() method.

Full Code

Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;
public class
DecryptPDF {
   
public static void main(String[] args) throws Exception {

       
//Create a PdfDocument instance         PdfDocument pdf = new PdfDocument();
       
//Load the encrypted PDF file with password         pdf.loadFromFile("Encrypt.pdf", "e-iceblue");
       
//Decrypt the file         pdf.getSecurity().encrypt("", "", PdfPermissionsFlags.getDefaultPermissions(), PdfEncryptionKeySize.Key_256_Bit, "test");
       
//Save and close
       
pdf.saveToFile("Decrypt.pdf");         pdf.close();
   
}
}

Effective Shot


Conclusion:

In this article, you have learned how to encrypt or decrypt PDF documents in Java. Not only that, we also have other functions, such as, Java: Add Security Permissions to a PDF Document, Java: Add or Delete Digital Signatures in PDF and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for Java.






Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application