Open In App

Encrypt PDF using Java

Improve
Improve
Like Article
Like
Save
Share
Report

We can encrypt any PDF using Java by using the external library PDFBox. Inside PDFBox library 2 classes are available StandardProtectionPolicy and AccessPermission Class.

Encryption Approach:

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 . 

stpp.setEncryptionKeyLength(128);

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. 

stpp.setPermission(ap);

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.  

pdd.protect(stpp);

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

Java




import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
import java.io.File;
import java.io.IOException;
 
class PdfEncryption {
    public static void main(String[] args)
        throws IOException
    {
        // step 1. Loading the pdf file
        File f = new File("D:\\demo.pdf");
        PDDocument pdd = PDDocument.load(f);
 
        // step 2.Creating instance of AccessPermission
        // class
        AccessPermission ap = new AccessPermission();
 
        // step 3. Creating instance of
        // StandardProtectionPolicy
        StandardProtectionPolicy stpp
            = new StandardProtectionPolicy("abcd", "abcd", ap);
 
        // step 4. Setting the length of Encryption key
        stpp.setEncryptionKeyLength(128);
 
        // step 5. Setting the permission
        stpp.setPermissions(ap);
 
        // step 6. Protecting the PDF file
        pdd.protect(stpp);
 
        // step 7. Saving and closing the PDF Document
        pdd.save("D:\\demo.pdf1");
        pdd.close();
 
        System.out.println("PDF Encrypted successfully...");
    }
}


 
 

Output:

 

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:

 

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.

 

 



Last Updated : 13 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads