Encrypting pdf using Apache PDFBox

Apache PDFBox provides way to protect your pdf by adding a password protection. In this post we will see an example on encrypting pdf using apache pdfbox.

The important classes that are required for encrypting pdf using apache pdfbox are

1) org.apache.pdfbox.pdmodel.encryption.AccessPermission

This class is used to specify the permissions given on the pdf. Few of the permissions that can be specified are
a) setCanPrint(boolean allowPrinting) print the document
b) setCanModify(boolean allowModifications) modify the content of the document
c) setCanExtractContent(boolean allowExtraction) copy or extract content of the document
d) setCanModifyAnnotations(boolean allowAnnotationModification) add or modify annotations
e) setCanFillInForm(boolean allowFillingInForm) fill in interactive form fields
f) setCanExtractForAccessibility(boolean allowExtraction) extract text and graphics for accessibility to visually impaired people
g) setCanAssembleDocument(boolean allowAssembly) assemble the document
h) setCanPrintDegraded(boolean canPrintDegraded) print in degraded quality

2) org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy

This class is used for the protection policy to add to a document for password-based protection. The constructor takes 3 parameters –
StandardProtectionPolicy(String ownerPassword, String userPassword, AccessPermission permissions)
ownerPassword – The owner’s password.
userPassword – The users’s password.
permissions – The access permissions given to the user.

Example

Output
Encrypting pdf using Apache PDFBox

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn