DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Build a Java Backend That Connects With Salesforce
  • How to Convert Between PDF and TIFF in Java
  • How to Split PDF Files into Separate Documents Using Java
  • How to Create Buttons in Java Applications

Trending

  • AI Agents: A New Era for Integration Professionals
  • Useful System Table Queries in Relational Databases
  • System Coexistence: Bridging Legacy and Modern Architecture
  • Simpler Data Transfer Objects With Java Records
  1. DZone
  2. Coding
  3. Java
  4. How to Password Protect a PDF File Using Java: A Step-By-Step Guide

How to Password Protect a PDF File Using Java: A Step-By-Step Guide

Learn how to password protect PDF files using Java and Apache PDFBox. Follow this step-by-step guide to secure sensitive information and implement robust PDF encryption.

By 
Prasanna Manjunatha user avatar
Prasanna Manjunatha
·
Oct. 11, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
8.5K Views

Join the DZone community and get the full member experience.

Join For Free

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.

XML
 
<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,

Kotlin
 
 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.

Java
 
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.

Java
 
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.

Java
 
document.protect(protectionPolicy);


5. Save the Password-Protected PDF

Finally, save the password-protected PDF to the desired location using the save() method.

Java
 
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.

Integrated development environment Java Development Kit PDF Java (programming language)

Published at DZone with permission of Prasanna Manjunatha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Build a Java Backend That Connects With Salesforce
  • How to Convert Between PDF and TIFF in Java
  • How to Split PDF Files into Separate Documents Using Java
  • How to Create Buttons in Java Applications

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!