Link
Skip to main content

Encrypt PDF Files in Java

v2026.05

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.

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.


Why JPedal?

  • Actively developed commercial library with full support and no third party dependencies.
  • Process PDF files up to 3x faster than alternative Java PDF libraries.
  • Simple licensing options and source code access for OEM users.

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download