PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
J2SE J2EE Windows Linux Mac (OS X)

Encrypting and Decrypting PDF Documents Using PDFOne Java

Learn about securing and restricting the use of a PDF document in Java.
By L. Santhanam

This is a PDFOne Java version of PDFOne .NET article published last year.

Here is what we mentioned in that article.

An encrypted PDF document places certain restrictions on how the document can be used - to open it, to modify it, to print it, to copy content, etc... An encrypted document may require either one of two passwords - owner or user password. If you provide the owner password, you have full access to the document including the ability to specify new password and change document restrictions. If you provide the user password, you will be able to open the document but may be subject to restrictions placed by the owner of the document. If a document creator encrypted a document without specifying a user password, then anyone can open the document without providing a password althought document restrictions would still apply.

In this article, we will see how to password-protect PDF documents and also how to read an encrypted PDF document.

While creating an encrypted PDF document, we will need to use the PdfEncryption class extensively. The PdfDocument.getEncryptor() method returns a PdfEncryption object. By setting its properties and then applying the modified PdfEncryption to the document using the PdfDocument.setEncryptor() method, a document can be encrypted.

While reading an encrypted document, we will need to implement the PdfPasswordHandler interface. We will have to provide the password when we implement the PdfPasswordHandler.onPassword() event. This event will be called when we try to create a PdfDocument object for the encrypted document.

import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfEncryption;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfPasswordHandler;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfWriter;

public class September_2008_Encryption 
   implements PdfPasswordHandler {
 static {
 public static void main(String[] args) 
   throws PdfException, IOException {
 static void createEncryptedDoc() 
   throws IOException, PdfException {
  // Create a blank document
  PdfDocument document = new PdfDocument();

  // Create a document encryption object
  PdfEncryption crypt = document.getEncryptor();
  // Set owner and user passwords 
  // Set encryption key length to 128 bits
  // Specify document restrictions
  PdfEncryption.AllowPrinting |

  // Write text to default page 1 of the document 
    "This document is encrypted. "
    + "The user password is PDFOne. "
    + "The owner password is Gnostice.");
  // Apply encryption settings to the document
  // Write the document to file

 static void readEncryptedDoc() 
   throws IOException, PdfException {
  PdfDocument document = new PdfDocument();

  try {
      new September_2008_Encryption());
  // Load the document 
   // Display the number of pages after 
   // reading from the decrypted document 
     "Success: Password is correct and the file has " 
     + document.getPageCount() + " page(s).");
  } catch (PdfException e) {
   System.out.println("Failure: " + e.getMessage());
  } finally {

 public String onPassword(
     PdfDocument d, boolean[] flags) {
  return "PDFOne";  // Return user password
  // return "Gnostice"; // Return owner password


Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.


A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.