June 24, 2022

Password Protected PDF Using OpenPDF in Java

In this post we’ll see how to create a password protected PDF in Java using OpenPDF library and how to read an encrypted PDF in Java using OpenPDF.

OpenPDF is open source software with a LGPL and MPL license. To know more about OpenPDF library and PDF examples check this post- Generating PDF in Java Using OpenPDF Tutorial

Password protected PDF using OpenPDF

In order to create an encrypted PDF following steps are required.

  1. Get an instance of PDFWriter.
  2. Using the setEncryption() method of the PDFWriter set the user and owner password, open permissions and encryptionType.
  3. User and owner password can be null or empty.
  4. The open permissions for the document can be AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. The permissions can be combined by ORing them.
  5. Encryption type can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128. Optionally DO_NOT_ENCRYPT_METADATA can be ORed to output the metadata in clear text.
  6. For encrypting PDF you will also need Bouncy Castle Provider. The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. Maven dependency for the same is-

Encrypted PDF using OpenPDF Java Program

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class PasswordProtectedPDF {
  public static final String ENCRYPTED_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
  // User and owner password
  final static String USER_PASSWORD = "user";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      Document doc = new Document();
      PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(ENCRYPTED_PDF));
      // set password, user permissions and encryption
      writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 
      Paragraph para = new Paragraph("Password protected PDF where only content printing is permitted content can't be copied.");
    } catch (DocumentException | FileNotFoundException e) {
      // TODO Auto-generated catch block

Reading Password protected PDF using OpenPDF

For reading a password protected PDF you need to pass owner password as byte array while creating a PDFReader instance.
import java.io.IOException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

public class ReadPDF {
  // PDF to be read
  public static final String READ_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    PdfReader pdfreader = null;
    try {
      pdfreader = new PdfReader(READ_PDF, OWNER_PASSWORD.getBytes());
      // get pages in PDF
      int pages = pdfreader.getNumberOfPages();
      PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfreader);
      // Iterate through pages to read content
      for(int i = 1; i <= pages; i++) {
        // Extract content of each page
        String contentOfPage = pdfTextExtractor.getTextFromPage(i, true);
        System.out.println(contentOfPage );
    } catch (IOException e) {
      // TODO Auto-generated catch block
    }finally {
      if(pdfreader != null) {

That's all for the topic Password Protected PDF Using OpenPDF in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment