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.
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.
Following are the steps to encrypt an existing PDF document.
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.
Encrypting 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.
- Print the document
- Modify the content of the document
- Copy or extract content of the document
- Add or modify annotations
- Fill in interactive form fields
- Extract text and graphics for accessibility to visually impaired people
- Assemble the document
- Print in degraded quality
Following are the steps to encrypt an existing PDF document.
Step 1: Loading 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);
Step 2: Creating Access Permission Object
Instantiate the AccessPermission class as shown below.AccessPermission accessPermission = new AccessPermission();
Step 3: Creating StandardProtectionPolicy Object
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);
Step 4: Setting the Length of the Encryption Key
Set the encryption key length using the setEncryptionKeyLength() method as shown below.spp.setEncryptionKeyLength(128);
Step 5: Setting the Permissions
Set the permissions using the setPermissions() method of the StandardProtectionPolicy class. This method accepts an AccessPermission object as a parameter.spp.setPermissions(accessPermission);
Step 6: Protecting the Document
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);
Step 7: Saving the Document
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");
Step 8: Closing the Document
Finally, close the document using close() method of PDDocument class as shown below.document.close();
Example
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 EncriptingPDFUpon execution, the above program encrypts the given PDF document displaying the following message.
Document encryptedIf 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.
No comments:
Post a Comment