How to Decrypt PDF in Java

This tutorial explains how to decrypt PDF in Java. It contains all the details required to establish the environment, detailed steps to write the program, and a runnable sample code to decrypt PDF document in Java. You do not need any PDF software or tool to be installed to perform this task.

Steps to Decrypt PDF File in Java

  1. Establish the environment to use Aspose.PDF for Java in your application
  2. Instantiate the PdfFileInfo class object to test the encryption status
  3. If PDF is encrypted, declare the PdfFileSecurity to continue the decryption process
  4. Bind the PdfFileSecurity class object with the source PDF file
  5. Decrypt the PDF file using the decryptFile() method along with the password
  6. Save the resultant PDF file without encryption

These steps describe the process to decrypt the PDF file in Java. First, the encryption state of the file is tested, and then it is loaded using the PdfFileSecurity() object using the bind() method. In the final step, it is decrypted by providing the password to the decryptFile() method and saved as a new PDF file if required.

Code to Decrypt PDF in Java

This code sample demonstrates the process to convert encrypted PDF to unencrypted in Java. It uses PdfFileInfo.isEncrypted() method to check if the target file is encrypted or not however this step is not necessary and you may ignore it in case of a confirmed encrypted file and load it directly using the PdfFileSecurity.bindPdf() method. The PdfFileSecurity class not only helps in decrypting the PDF file but also helps to change the password, and encrypt the PDF file.

This topic has taught us to decrypt PDF files protected with user passwords in Java. If you want to learn the process to encrypt a Pdf file, refer to the article on how to protect PDF with password in Java.