There are different security levels that you can provide to your confidential documents. You can apply watermarks, encrypt files, or you can make your documents password-protected. In this article, we will see how to programmatically add password protection to the PDF files within the Java applications. Further, we will learn to change the password and also to remove the passwords to unlock PDF files.
The following topics are discussed below:
- Java API for Password Protection of PDF Files
- Password Protect PDF Files in Java
- Change PDF Password in Java
- How to Remove PDF Password – Unlock PDF
Java API to Lock and Unlock PDF Files
GroupDocs.Merger for Java is the API that allows to lock and unlock documents. We will use it to add, change, and remove password security features for the PDF documents within the Java applications. Along with protecting and unprotecting documents, the API provides many more features like splitting, merging documents, and many more that are mentioned in the documentation.
You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger</artifactId>
<version>21.9</version>
</dependency>
Add Password to PDF in Java – Lock PDF
Let’s quickly jump to add password protection to the PDF files for security. The following steps show how to add a password to PDF documents in Java.
- Define the password using AddPasswordOptions class.
- Load the PDF file using Merger class.
- Protect the file by adding password using addPassword() method.
- Save the protected file using the save() method.
The following code snippet adds a password to a PDF file in Java.
If you try to open the password-protected PDF file, the PDF viewer will ask to enter the password.
Update Existing Password of PDF Files in Java
What if your secret is no more a secret? Make it secret again. Let’s change the password to a new one. The following steps change the existing password of a PDF file in Java.
- Set the loading options using the current password.
- Now set the update options using the new password.
- Load the PDF document using Merger class and the loading options.
- Change the existing password using updatePassword() method.
- Save the password protected file again with the updated password using the save() method.
The code snippet changes the current password of the PDF document using Java code.
Remove Password from PDF Files in Java – Unlock PDF
If file protection is not more needed, you can remove the password. The following steps show how to remove the password of a protected PDF file in Java.
- Prepare the load options using the existing password.
- Load the PDF document using Merger class using load options.
- Remove its password using removePassword() method.
- Save the unlocked file using the save() method.
The following is the Java code example to remove the password of a PDF file to make it unlocked.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
Conclusion
To conclude, we discussed the password protection of PDF documents. Initially, we locked the PDF file by adding a password. Then, we changed its password. Lastly, we removed the PDF file password to keep these unlocked. Now you can think about building your own password protector & password remover Java application.
To learn more about GroupDocs.Merger for Java, visit the documentation. For queries, contact us via the forum.