June 25, 2022

Password Protected PDF Using PDFBox in Java

In this post we’ll see how to password protect a PDF in Java using PDFBox library and how to open an encrypted PDF using PDFBox.

To know more about PDFBox library and PDF examples in Java using PDFBox check this post- Generating PDF in Java Using PDFBox Tutorial

Password protected PDF using PDFBox

In order to encrypt a PDF using PDFBox two classes are used-

1- AccessPermission- This class represents the access permissions to a document which includes the f ollowing permissions-

  • Print the document
  • modify the content of the document
  • copy or extract content of the document
  • add or modify annotations
  • fill in interactive form fields
  • extract text and graphics for accessibility to visually impaired people
  • assemble the document
  • print in degraded quality

2- StandardProtectionPolicy- This class represents protection policy to add to a document for password-based protection. Constructor of this class takes the AccessPermission class object, owner password and user password as arguments.

Two passwords that are required for PDF encryption are-

  • user password- to open and view the file with restricted permissions.
  • owner password- to access the file with all permission.

Encrypted PDF using PDFBox Java Program

In this example an existing PDF is loaded and encrypted.

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;

public class PasswordProtected {
  public static final String ENCRYPTED_PDF = "F://knpcode//result//PDFBox//Content.pdf";
  // User and owner password
  final static String USER_PASSWORD = "user";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      //load an existing PDF
      PDDocument document = PDDocument.load(new File(ENCRYPTED_PDF));
      AccessPermission ap = new AccessPermission();
      /** Setting access permissions */
      // Printing not allowed
      ap.setCanPrint(false);
      // Copying not allowed
      ap.setCanExtractContent(false);
        
      StandardProtectionPolicy standardPP = new StandardProtectionPolicy(OWNER_PASSWORD, USER_PASSWORD, ap);
      standardPP.setEncryptionKeyLength(128);
      document.protect(standardPP);
      document.save(ENCRYPTED_PDF);
      document.close();
        
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

Opening Encrypted PDF using PDFBox Java Program

If you want to open a PDF that is password protected using PDFBox then you can use load method of the PDDocument class and pass the password required for decryption.

PDDocument document = PDDocument.load(new File(ENCRYPTED_PDF), OWNER_PASSWORD);

That's all for the topic Password Protected PDF Using PDFBox 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