In the previous chapter, we have seen how to insert an image in a PDF document. In this chapter, we will discuss how to encrypt a PDF document.
You can encrypt a PDF document using the methods provided by StandardProtectionPolicy and AccessPermission classes.
The AccessPermission class is used to protect the PDF Document by assigning access permissions to it. Using this class, you can restrict users from performing the following operations.
The StandardProtectionPolicy class is used to add a password based protection to a document.
Following are the steps to encrypt an existing PDF document.
Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.
File file = new File("path of the document") PDDocument document = PDDocument.load(file);
Instantiate the AccessPermission class as shown below.
AccessPermission accessPermission = new AccessPermission();
Instantiate the StandardProtectionPolicy class by passing the owner password, user password, and the AccessPermission object as shown below.
StandardProtectionPolicy spp = new StandardProtectionPolicy("1234","1234",accessPermission);
Set the encryption key length using the setEncryptionKeyLength() method as shown below.
spp.setEncryptionKeyLength(128);
Set the permissions using the setPermissions() method of the StandardProtectionPolicy class. This method accepts an AccessPermission object as a parameter.
spp.setPermissions(accessPermission);
You can protect your document using the protect() method of the PDDocument class as shown below. Pass the StandardProtectionPolicy object as a parameter to this method.
document.protect(spp);
After adding the required content save the PDF document using the save() method of the PDDocument class as shown in the following code block.
document.save("Path");
Finally, close the document using close() method of PDDocument class as shown below.
document.close();
Suppose, we have a PDF document named sample.pdf, in the path C:/PdfBox_Examples/ with empty pages as shown below.
This example demonstrates how to encrypt the above mentioned PDF document. Here, we will load the PDF document named sample.pdf and encrypt it. Save this code in a file with name EncriptingPDF.java.
import java.io.File; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.encryption.AccessPermission; import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; public class EncriptingPDF { public static void main(String args[]) throws Exception { //Loading an existing document File file = new File("C:/PdfBox_Examples/sample.pdf"); PDDocument document = PDDocument.load(file); //Creating access permission object AccessPermission ap = new AccessPermission(); //Creating StandardProtectionPolicy object StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap); //Setting the length of the encryption key spp.setEncryptionKeyLength(128); //Setting the access permissions spp.setPermissions(ap); //Protecting the document document.protect(spp); System.out.println("Document encrypted"); //Saving the document document.save("C:/PdfBox_Examples/sample.pdf"); //Closing the document document.close(); } }
Compile and execute the saved Java file from the command prompt using the following commands.
javac EncriptingPDF.java java EncriptingPDF
Upon execution, the above program encrypts the given PDF document displaying the following message.
Document encrypted
If you try to open the document sample.pdf, you cannot, since it is encrypted. Instead, it prompts to type the password to open the document as shown below.