Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will demonstrate how to use IronPDF to work with PDF documents and also protect new files with a user password.
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.
To use IronPDF to work with PDFs in a Java Maven project, you will need to make sure that you have the following prerequisites:
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>com.ironsoftware</artifactId>
<version>2024.11.4</version>
</dependency>
Another dependency required is Slf4j. Add the Slf4j dependency in the pom.xml
file.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
Once you have downloaded and installed your password protect PDF in a Java program, you are ready to use IronPDF to protect a PDF file with password protection.
The first step is to import the IronPDF required classes in 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;
Now, in the main method, enter your license key using the IronPDF setLicenseKey
method.
License.setLicenseKey("Your license key");
The following code snippet will open a document that was encrypted with the password "password":
PdfDocument pdf = PdfDocument.fromFile(Paths.get("encrypted.pdf", "secretPassword"));
In the above code snippet, an encrypted PDF file was opened with a password "password".
Encrypted PDF document looks like the following:
Opening an Encrypted PDF document
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();
securityManager.removePasswordsAndEncryption();
securityManager.setPassword("secret-key");
The first step is to remove the password using the [IronPDF removePasswordsAndEncryption method
](/java/object-reference/api/com/ironsoftware/ironpdf/security/SecurityManager.html#removePasswordsAndEncryption()) method, and then set a new password using the IronPDF setPassword method
method.
Finally, save the PDF document with the following line of code:
pdf.saveAs(Paths.get("assets/secured.pdf"));
The output file is now opened with the "secret-key" password.
Newly-encrypted PDF Document
Important security options can be set easily with IronPDF in Java using the IronPDF SecurityOptions permission class
permission 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.
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setAllowUserCopyPasteContent(false);
securityOptions.setAllowUserAnnotations(false);
securityOptions.setAllowUserPrinting(PdfPrintSecurity.NO_PRINT);
securityOptions.setAllowUserFormData(false);
SecurityManager securityManager = pdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
This will set all the necessary security options of the PDF document. This can be seen in the output below:
New PDF Security Settings
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.
9 .NET API products for your office documents