Apache PDFBox Encrypt Decrypt PDF Document Java
This tutorial demonstrates how to add a password and encrypt a PDF document in Java using Apache PDFBox. We also show how to decrypt a password protected PDF document.
Maven Dependencies
We use Apache Maven to manage our project dependencies. Make sure the following dependencies reside on the class-path.
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.8</version>
</dependency>
Apache PDFBox Encrypt PDF Document
The AccessPermission
represents the access permissions to a document. These permissions are specified in the PDF format specifications, they include:
- 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
ProtectionPolicy
in this example we used the StandardProtectionPolicy
which is username/password protection based. There is also a PublicKeyProtectionPolicy
which you can use.package com.memorynotfound.pdf.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.File;
import java.io.IOException;
public class Encrypt {
public static void main(String[] args) throws Exception{
try (final PDDocument doc = new PDDocument()){
PDPage page = new PDPage();
doc.addPage(page);
AccessPermission ap = new AccessPermission();
ap.setCanPrint(false);
ap.setCanPrintDegraded(false);
ap.setCanExtractContent(false);
ap.setCanExtractForAccessibility(false);
ap.setCanFillInForm(true);
ap.setCanModify(false);
ap.setReadOnly();
StandardProtectionPolicy spp = new StandardProtectionPolicy("user", "password", ap);
spp.setEncryptionKeyLength(128);
spp.setPreferAES(true);
spp.setPermissions(ap);
doc.protect(spp);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(PDType1Font.HELVETICA, 18);
content.newLineAtOffset(100, 700);
content.showText("Apache PdfBox Encrypt Decrypt PDF Document");
content.endText();
content.close();
doc.save(new File("/tmp/encrypt.pdf"));
} catch (IOException e){
System.err.println("Exception while trying to create pdf document - " + e);
}
}
}
Demo
When we execute the application, a password protected PDF document is created. Take a look at the following encrypted PDF document.
Apache PDFBox Decrypt PDF Document
We can decrypt or read a password protected PDF document by passing in a password in the PDDocument.Load()
method.
package com.memorynotfound.pdf.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.File;
import java.io.IOException;
public class Decrypt {
public static void main(String[] args) throws Exception{
try (PDDocument document = PDDocument.load(new File("/tmp/encrypt.pdf"), "password")) {
document.setAllSecurityToBeRemoved(true);
PDFTextStripper reader = new PDFTextStripper();
String pageText = reader.getText(document);
System.out.println(pageText);
} catch (IOException e){
System.err.println("Exception while trying to read pdf document - " + e);
}
}
}
References
- Apache PdfBox Official Website
- Apache PdfBox API Javadoc
- AccessPermission JavaDoc
- StandardProtectionPolicy JavaDoc