We can encrypt any PDF using Java by using the external library PDFBox. Inside PDFBox library 2 classes are available StandardProtectionPolicy and AccessPermission Class.
By using the PDFBox library, you will see how you can encrypt the PDF file. Encryption is used when a user wants their own data or file in protected mode. Encryption is used as the inbuilt algorithm for encrypting the file which is simply needed credentials to access the file.
AccessPermission class is used to protect the PDF by assigning access permission to it. This class will restrict the user from performing different operations on the PDF. Example. Printing, copying, modifying, etc.
StandardProtectionPolicy class is used to apply a password to the PDF document.
Maven Dependency for PDFBox :
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.21</version> </dependency>
Steps to apply a password to PDF
1. Load the PDF Document:
Load the PDF file using load() static method (we can access it by using the class name) of class PDDocument. The load() method will accept the PDF file as a parameter.
File f = new File("path_of_PDFfile"); PDDocument pdd = PDDocument.load(f);
2. Create an instance of AccessPermission class :
AccessPermission ap = new AccessPermission();
3. Create an instance of StandardProtectionPolicy:
During the instantiation of StandardProtectionPolicy class pass owner password, user password, and object of AccessPermission class that is ‘ap’.
StandardProtectionPolicy stpp = new StandardProtectionPolicy("Owner_pass" , "user_pass" , ap);
Here we can use any password to encrypt the PDF file.
Example: Here consider the password “abcd” as a user and owner password.
StandardProtectionPolicy stpp = new StandardProtectionPolicy("abcd" , "abcd" , ap);
4. Set the length of Encryption Key:
Set the length of encryption key by using setEncryptionKeyLength() method of StandardProtectionPolicy class .
5. Set Permission:
Set the permission to PDF using setPermission() method of StandardProtectionPolicy class. You have to pass the object of AccessPermissionclass as a parameter in setPermission() method.
6. Protect the PDF file:
Protect the PDF file using the protect() method of PDDocument class. Here in this, we have to pass the object of StandardProtectionPolicy class as a parameter.
7. Save and close the Document:
Finally, save and close the document by using the save() and close() method of PDDocument class.
pdd.save("path_of_PDFfile"); // save the document pdd.close(); // close the document
Before Encryption :
Here, you will see you don’t need any password to access the file. Before Encryption, You can directly access the file without need any credentials. Let’s have a look.
After Encryption, Your PDF file will be saved on a given location and will be saved in a protected mode that simply means you will need a password to access or to read the PDF file. Let’s have a look.
In these ways, you can easily protect your PDF Document using a password.
If you want to implement this program on Eclipse IDE then used the following steps given below.
- Open Eclipse IDE.
- Create a New Java Project for example PDFEncryption.
- Now, Create a New Java Class like PdfEncryption.
- Now, to add the dependencies add two .jar files that are using in the program.
- Add PDFBox library from this link download .jar file and Add PDFBox 2.0.21.
- Add Apache Commons Logging library from this link Apache Commons Logging 1.2.
You can check the given below screenshot for your reference.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.