How to Password Protect a PDF File Using Java: A Step-By-Step Guide
PDF (Portable Document Format) files are widely used for sharing documents due to their platform-independent nature and consistent formatting. However, some documents may contain sensitive information that requires an extra layer of security. If you are a Java developer, you can easily accomplish this task programmatically using Apache PDFBox. Apache PDFBox library is open source and free library for both commercial and non commercial use.
In this blog post, we'll dive into how you can use Java and Apache PDFBox to add password protection to a PDF file.
Prerequisites
Before we begin, ensure you have the following in place:
- Java Development Kit (JDK): Ensure that you have JDK installed on your system. If not, you can download it from here.
- A Java IDE (Integrated Development Environment) like Eclipse or IntelliJ IDEA os Microsoft VS Code.
1. Set up the Project
Create a new Java project in your IDE and add the Apache PDFBox library to the project's build path. If you are using Maven or Gradle to manage the dependencies, you can integrate the Apache PDFBox to your project by including the dependencies to your pom.xml or build.gradle file.
If you are using Maven, you can add the following dependency in your pom.xml file to include Apache PDFBox.
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.0</version>
</dependency>
If you are using Gradle, you can add the below dependency,
implementation("org.apache.pdfbox:pdfbox:3.0.0")
2. Load the PDF Document
Now let's look into how we can add password protection to an existing PDF file. To start, you need to load the PDF document you want to protect using the Loader
class from the PDFBox library.
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
try {
// Replace with the path to your input PDF file
String inputFilePath = "input.pdf";
PDDocument document = Loader.loadPDF(new File(inputFilePath));
// Your code to password protect the document will go here
document.close();
} catch (IOException e) {
e.printStackTrace();
}
3. Create the Password Protection Policy
Next, create the password protection policy using the StandardProtectionPolicy
class from PDFBox. This policy allows you to set both user and owner passwords, along with various access permissions.
Here, ownerPassword
is the password that grants full access and control over the PDF document, while userPassword
is what you'd use just to open the document.
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
// Replace with your desired user password
String userPassword = "your_user_password";
// Replace with your desired owner password
String ownerPassword = "your_owner_password";
AccessPermission accessPermission = new AccessPermission();
// Set to true if you want to allow printing
accessPermission.setCanPrint(false);
StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPassword, userPassword, accessPermission);
4. Apply the Password Protection
Now, apply the password protection policy to the PDF document using the protect()
method.
document.protect(protectionPolicy);
5. Save the Password-Protected PDF
Finally, save the password-protected PDF to the desired location using the save()
method.
String outputFilePath = "output.pdf"; // Replace with the desired path for the output protected PDF file
try {
document.save(outputFilePath);
System.out.println("PDF file is password protected successfully!");
} catch (IOException e) {
e.printStackTrace();
}
Password protecting a PDF file is crucial when dealing with sensitive information. With Java and the Apache PDFBox library, you can easily implement robust password protection for your PDF documents. This step-by-step guide has shown you how to load a PDF file, create a password protection policy, apply it, and save the password-protected PDF file to a new location. By following these steps, you can ensure the confidentiality and security of your important documents.