Javatpoint Logo
Javatpoint Logo

Encripting PDF Document

PDF documents are popular replacements for paper documents. They have the same look and feel on every platform. As with paper documents, some PDF documents are confidential. We can secure PDF documents with password protection. We can also specify the permissions and encryption type in the PDF document. In this section, we describe how to encrypt and decrypt password protected PDF documents by using PDFBox library.

A PDF document can be encrypted by using the methods provided by StandardProtectionPolicy and AccessPermission classes.

The AccessPermission class provide access permissions to protect the PDF Document. These permissions are specified in the PDF format specifications to restrict users from performing the following operations:

  • 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

PDF documents requires two types of Passwords Encryption:


PDFBox Encripting PDF Document

1. Owner Password- Also referred as the "permissions password". This password is used to access the file with all permissions.

2. User Password- Also referred as the "open password". This password allows to open and view the file with restricted permissions.

Follow the below steps to encrypt an existing PDF document -

Load Existing Document

We can load the existing PDF document by using the static load() method. This method accepts a file object as a parameter. We can also invoke it using the class name PDDocument of the PDFBox.

Create AccessPermission Object

We instantiate AccessPermission class to create an object which can be shown below.

Create StandardProtectionPolicy object

We instantiate StandardProtectionPolicy class to create an object. In this class, we need to provide the owner password and user password as a parameter which can be shown in the below code.

Set Length of Encryption Key

We can set the length of the encryption key by using the setEncryptionKeyLength() method which can be shown in the following code.

Set Access Permission

We can set the permission by using the setPermissions() method. This method accepts an AccessPermission object as a parameter.

Protect document

We can protect our document by using protect() method. This method accepts the StandardProtectionPolicy object as a parameter.

Save Document

After adding the required content, we need to save the document to our desired location. The save() method is used to save the document. The save() method accepts a string value and passes a path of the document as a parameter.

Close Document

Finally, after completing the task, we need to close the PDDocument class object by using the close() method.

Example-

Suppose, this is a PDF document file named Merged.pdf. We can perform encryption in PDF file through the PDFBox library of a Java Program.


PDFBox Encripting PDF Document

Java Program

Output:

After successful execution of the above program, we will see the following output message.


PDFBox Encripting PDF Document

To verify the given path, we can observe that a PDF document with name EncryptPDF.pdf is created. This document contains the password for owner and user both. To open this PDF document, it requires the Password which can be shown in the following output.


PDFBox Encripting PDF Document
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA