- PDFBox - Home
- PDFBox - Overview
- PDFBox - Environment
- PDFBox - Creating a PDF Document
- PDFBox - Adding Pages
- PDFBox - Loading a Document
- PDFBox - Removing Pages
- PDFBox - Document Properties
- PDFBox - Adding Text
- PDFBox - Adding Multiple Lines
- PDFBox - Reading Text
- PDFBox - Inserting Image
- Encrypting a PDF Document
- JavaScript in PDF Document
- PDFBox - Splitting a PDF Document
- Merging Multiple PDF Documents
- PDFBox - Converting PDF To Image
- PDFBox - Adding Rectangles
PDFBox - Useful Resources
PDFBox - Encrypting a PDF Document
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.
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.
The StandardProtectionPolicy class is used to add a password based protection to a document.
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 loadPDF() of the Loader class. This method accepts a RandomAccessReadBufferedFile object as a parameter, since this is a static method you can invoke it using class name as shown below.
// Loading an existing document
PDDocument document = Loader.loadPDF(
new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf"));
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 - Encrypting a PDF Document
Suppose, we have a PDF document named sample.pdf, in the path D:/Projects/PDFBox/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 PDFBoxDemo.java.
PDFBoxDemo.java
package com.tutorialspoint.pdfbox;
import java.io.IOException;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
public class PDFBoxDemo {
public static void main(String args[]) throws IOException {
// Loading an existing document
PDDocument document = Loader.loadPDF(
new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf"));
//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("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf");
//Closing the document
document.close();
}
}
Output
Compile and run the code to verify the following output −
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.