This brief tutorial describes how to change PDF password in Java. It provides details to establish the environment to use the desired library, a list of easy-to-follow steps to write the application, and for better understanding, a ready-to-run sample code demonstrating how to change password in PDF in Java. With the help of this article, you will be able to change both the owner and user password however the owner password is must required to perform this task.
Steps to Change Password of PDF in Java
- Set the IDE to use Aspose.PDF for Java to update the PDF password
- Create a PdfFileInfo class object that supports checking the password protection of a PDF file
- Check the password protection and perform the following steps in case of confirmation
- Bind the target PDF file with the PdfFileSecurity class object
- Change the password using the PdfFileSecurity class method
- Save the resultant PDF file with the updated password for the user and owner both
These steps summarize how to change password for PDF in Java. In the beginning, the target PDF file is checked for having password protection using the PdfFileInfo class object as a password can be changed only if it is already encrypted by a password. Finally, we use the PdfFileSecurity class in the om.aspose.pdf.facades namespace that contains features to bind and change the password using the existing owner password and replacing both the passwords i.e. owner and user.
Code to Change Password of PDF File in Java
This code shows how to change password in PDF file in Java. The PdfFileInfo class contains different features to check the password and encryption state of a PDF file along with options to get/set a lot of other properties like author, creation date, creator, header, keywords, modification date, subject, title, checking if the target file is a PDF file or not, get the number of pages, and checking if a password is required to open or edit a PDF file. Similarly, the PdfFileSecurity class is used to modify the owner and user password.
This article has taught us how to change password of PDF in Java. If you want to learn how to protect a PDF file first time using a password, refer to the article on how to protect PDF with password in Java.