MyJavaCode.com

Tutorial and How to Guide on Java Programming. Topics covered include Java Programming Language - Core Java, Spring, Webservices, REST, Hibernate, Maven and Microservices.

  • Core Java
  • REST API’s
  • Spring boot
  • Hibernate
  • Interview Questions
  • Contact Us
  • Projects
  • Offerings

How to Password Protect a PDF File using Java

January 13, 2025 By Prasanna Manjunatha

Password Protect PDF

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:

  1. Java Development Kit (JDK): Ensure that you have JDK installed on your system. If not, you can download it from here.
  2. 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")

Also Read: How to Create Immutable Objects in Java

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 PDDocument class from the PDFBox library.

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;

try {
    String inputFilePath = "input.pdf"; // Replace with the path to your input PDF file
    PDDocument document = Loader.loadPDF(new File(inputFilePath));
    // Your code to password protect the document will go here
    document.close();
} catch (IOException e) {
    e.printStackTrace();
}

Also Read: What Are REST APIs? A Beginner’s Guide for Java Developers

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;

String userPassword = "your_user_password"; // Replace with your desired user password
String ownerPassword = "your_owner_password"; // Replace with your desired owner password

AccessPermission accessPermission = new AccessPermission();
accessPermission.setCanPrint(false); // Set to true if you want to allow printing

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);

Also Read: Getting Started with Spring Boot: A Spring Boot Setup Tutorial

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.

email
print

About Prasanna Manjunatha

Prasanna is a Toronto based Java Consultant with more than 15 years of Software Development experience. His areas of expertise include Core Java, Spring boot, REST, Microservices, Hibernate, NoSQL, Docker, Kubernetes and AWS.

Currently Trending

Connect with us

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

TAG CLOUD

Axis2 CodeCoverage Code Coverage cron expression Data Structure Frameworks Hashing HashMap Hibernate IntelliJ java Java Mail jboss jpa 2.1 Maven MyBatis MySQL PDF Quartz REST REST API SOAP Spring boot Springboot SQL Tools Tutorial web service

All time Popular Posts

Copyright © 2025 MyJavaCode.com