PDF encryption restricts access to document content, ensuring only recipients with the correct password can open or read a file. JPedal provides a simple API to apply password-protection and configure encryption settings programmatically in Java.
This tutorial uses JPedal’s PDF Manipulator class, which provides many more features useful in document processing workflows.
Encrypt PDF files in Java
Add JPedal to your project as a Maven or Gradle dependency, then use PdfManipulator to load, encrypt, and write a PDF:
final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.writeDocument(new File("outputFile.pdf"), "password".getBytes(StandardCharsets.UTF_8));
pdf.closeDocument();
The password supplied to writeDocument() sets the password required to open the document. There are also byte[] and OutputStream versions of the method.
See the PdfManipulator Javadoc.
Configure encryption settings
You can configure additional encryption behaviour via EncryptionSettings before calling writeDocument().
For example, whether document metadata should be encrypted:
pdf.getEncryptionSettings().setEncryptMetadata(false);
See the EncryptionSettings Javadoc for all available options.
Encrypt a file in one line
If you only need to encrypt a single file and do not need to perform any other manipulations, PdfManipulator provides a static convenience method which loads, encrypts, and writes the file for you:
PdfManipulator.encrypt(new File("inputFile.pdf"), new File("outputFile.pdf"), "password".getBytes(StandardCharsets.UTF_8));
There is also an overload which accepts the input file’s existing password, for re-encrypting an already password-protected PDF with a new password:
PdfManipulator.encrypt(new File("inputFile.pdf"), new File("outputFile.pdf"),
"oldPassword".getBytes(StandardCharsets.UTF_8), "newPassword".getBytes(StandardCharsets.UTF_8));
If you want to perform other manipulations alongside encryption, create an instance of
PdfManipulatorinstead, as shown above, since this will only read and write the file once. See Static methods for more information.
Open a password-protected PDF
To load an encrypted PDF in JPedal, supply the password to loadDocument():
pdf.loadDocument(new File("encryptedFile.pdf"), "password".getBytes(StandardCharsets.UTF_8));
This is useful when re-encrypting an existing document with a new password.
Frequently asked questions
Can I open an existing encrypted PDF and re-encrypt it with a new password?
Yes. Pass the existing password to loadDocument(), make any changes, then call writeDocument() with the new password.
Why would I disable metadata encryption?
Some document management systems read PDF metadata (title, author, subject) without opening the full file. Disabling metadata encryption allows those systems to index your files while the document content remains protected.
Does encrypting a PDF affect existing annotations or bookmarks?
No. Encryption is applied at write time and wraps the existing document structure. Annotations, bookmarks, and other content remain intact.
Related tutorials
- Sanitize PDF files — strip JavaScript, metadata, and embedded files
- Optimize PDF — reduce file size before distributing
- Merge PDF files — combine PDFs before encrypting
- PDF Manipulator overview — full list of manipulation features