Skip to footer content
USING IRONPDF FOR JAVA

How to Password Protect PDF in Java

This article will demonstrate how to use IronPDF to work with PDF documents and also protect new files with a user password.

IronPDF - Java PDF Library

IroqPDF Java PDF Library is a Java library for working with PDF documents. It provides a wide range of features for generating and manipulating PDFs, including the ability to add text, images, and other types of content, and control the layout and formatting of the document. It also provides a number of important features for securing PDF content, such as using password protection features in IronPDF.

Steps to Protect PDF using Password in Java Applications

Prerequisites for Project Setup

To use IronPDF to work with PDFs in a Java Maven project, you will need to make sure that you have the following prerequisites:

  1. Java Development Kit (JDK): A running current version of Java must be installed on your computer. If you don't have JAR files, then download the latest JDK from the Oracle website.
  2. Maven: Maven is an important build automation tool for Java projects which is required to manage the project and its dependencies. Download Maven or JAR file from the Apache Maven website if you don't have it installed.
  3. IronPDF for Java Library: You will also require the IronPDF for Java library, which will be added to your Maven project as a dependency. This can be done by adding the following dependency to your project's pom.xml file. Maven will automatically download and install it in the project.

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR_VERSION_HERE</version>
    </dependency>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR_VERSION_HERE</version>
    </dependency>
    XML
  4. Slf4j Dependency: Add the Slf4j dependency in the pom.xml file.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>
    XML

Once you have set up your Java program with password protection capability for PDFs, you are ready to use IronPDF to protect a PDF file with password protection.

Important Steps Before Writing Code

First, import the necessary IronPDF classes into your Java code. Add the following code at the top of the "Main.java" file:

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.metadata.MetadataManager;
import com.ironsoftware.ironpdf.security.PdfPrintSecurity;
import com.ironsoftware.ironpdf.security.SecurityManager;
import com.ironsoftware.ironpdf.security.SecurityOptions;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.metadata.MetadataManager;
import com.ironsoftware.ironpdf.security.PdfPrintSecurity;
import com.ironsoftware.ironpdf.security.SecurityManager;
import com.ironsoftware.ironpdf.security.SecurityOptions;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;
JAVA

Now, in the main method, enter your license key using the IronPDF setLicenseKey method.

// Set your IronPDF license key
License.setLicenseKey("Your license key");
// Set your IronPDF license key
License.setLicenseKey("Your license key");
JAVA

Open an Encrypted PDF Document

The following code snippet will open a document that was encrypted with the password "password":

// Load an encrypted PDF file using its password
PdfDocument pdf = PdfDocument.fromFile(Paths.get("encrypted.pdf"), "secretPassword");
// Load an encrypted PDF file using its password
PdfDocument pdf = PdfDocument.fromFile(Paths.get("encrypted.pdf"), "secretPassword");
JAVA

In the above code snippet, an encrypted PDF file is opened with the password "password".

How to Password Protect PDF in Java, Figure 1: Opening an Encrypted PDF document Opening an Encrypted PDF document

Encrypt PDF Document using Password Protection

Let's change the owner password of the "encrypted.pdf" file, which was opened in the previous step. The following code helps to achieve this task:

// Change or set the document owner password
SecurityManager securityManager = pdf.getSecurity();
// Remove existing passwords and encryption from the document
securityManager.removePasswordsAndEncryption();
// Set a new password for the document
securityManager.setPassword("secret-key");
// Change or set the document owner password
SecurityManager securityManager = pdf.getSecurity();
// Remove existing passwords and encryption from the document
securityManager.removePasswordsAndEncryption();
// Set a new password for the document
securityManager.setPassword("secret-key");
JAVA

The first step is to remove the password using the removePasswordsAndEncryption method, and then set a new password using the setPassword method.

Save Password Protected PDF Documents

Finally, save the PDF document with the following line of code:

// Save the secured PDF document
pdf.saveAs(Paths.get("assets/secured.pdf"));
// Save the secured PDF document
pdf.saveAs(Paths.get("assets/secured.pdf"));
JAVA

The output file is now opened with the "secret-key" password.

How to Password Protect PDF in Java, Figure 2: Newly-encrypted PDF Document Newly-encrypted PDF Document

Edit File Security Settings

Important security options can be set easily with IronPDF in Java using the SecurityOptions class. The code below makes the PDF read-only and disallows users from copying, pasting, and printing, and sets passwords for the owner and user.

// Configure security options for the PDF document
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setAllowUserCopyPasteContent(false);
securityOptions.setAllowUserAnnotations(false);
securityOptions.setAllowUserPrinting(PdfPrintSecurity.NO_PRINT);
securityOptions.setAllowUserFormData(false);

SecurityManager securityManager = pdf.getSecurity();
// Apply the specified security options to the PDF
securityManager.setSecurityOptions(securityOptions);
// Configure security options for the PDF document
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setAllowUserCopyPasteContent(false);
securityOptions.setAllowUserAnnotations(false);
securityOptions.setAllowUserPrinting(PdfPrintSecurity.NO_PRINT);
securityOptions.setAllowUserFormData(false);

SecurityManager securityManager = pdf.getSecurity();
// Apply the specified security options to the PDF
securityManager.setSecurityOptions(securityOptions);
JAVA

This will set all the necessary security options for the PDF document.

How to Password Protect PDF in Java, Figure 3: New PDF Security Settings New PDF Security Settings

Summary

This article explained how to open an existing PDF document and add password protection using the IronPDF Library for Java in Java. IronPDF makes it a lot easier to work with PDF files in Java. Whether you want to create a new document or make a PDF viewer, IronPDF helps to achieve this task with a single line of code. IronPDF's Engine is well suited for the Java programming language, as it is fast and memory efficient. With IronPDF, you can set a user password along with the owner password. It provides full protection options along with other features like converting to PDF from other formats with IronPDF, splitting documents with IronPDF, and merging documents with IronPDF.

IronPDF can be used for free in a free trial of IronPDF and can be licensed for commercial use with IronPDF. Its lite package starts from $749. Download IronPDF and give it a try.

Frequently Asked Questions

How can I password protect a PDF document in Java?

To password protect a PDF document in Java, you can use IronPDF's SecurityManager class to set a user password. This involves initializing a PdfDocument object, setting the desired password using setPassword, and then saving the file with the saveAs method.

What do I need to set up a PDF project in Java?

To set up a PDF project using IronPDF in Java, you need the Java Development Kit (JDK), Maven for dependency management, and you must include the IronPDF library in your project's pom.xml file.

How do you manipulate PDF files in Java?

You can manipulate PDF files in Java using IronPDF by importing the necessary classes such as PdfDocument. This allows you to edit content, merge documents, and apply security settings using the SecurityOptions class.

How can I restrict a PDF from being printed using Java?

Using IronPDF, you can restrict printing of a PDF by configuring the SecurityOptions class. Set the appropriate permissions to disallow actions like printing, then apply these settings to your PdfDocument object.

What are the steps to open an encrypted PDF in Java?

To open an encrypted PDF in Java using IronPDF, use the PdfDocument.fromFile method, providing the file path and the password as parameters to decrypt and access the document.

Is there a free version of the PDF library available for Java?

IronPDF offers a free trial version that developers can use to explore its features. For extended use or commercial projects, a licensed version is available.

How can I change the password on an existing PDF in Java?

To change the password on an existing PDF using IronPDF, open the document with the current password, remove it using the SecurityManager, and set a new password before saving the document.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...Read More

Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?