跳至页脚内容
在 JAVA 中使用 IRONPDF

如何在 Java 中为 PDF 添加密码保护

本文将演示如何使用IronPDF处理PDF文档,并通过用户密码保护新文件。

IronPDF - Java PDF 库

IronPDF Java PDF Library是一个用于处理PDF文档的Java库。 它提供了广泛的功能来生成和操作PDF,包括添加文本、图像和其他类型的内容,以及控制文档的布局和格式。 它还提供了一些重要功能来保护PDF内容,例如在IronPDF中使用密码保护功能

在Java应用程序中使用密码保护PDF的步骤

项目设置的前提条件

要在Java Maven项目中使用IronPDF处理PDF,您需要确保具备以下前提条件:

  1. Java Development Kit (JDK): 您的计算机上必须安装当前版本的Java。 如果没有JAR文件,请从Oracle网站下载最新的JDK。
  2. Maven: Maven是Java项目的重要构建自动化工具,需用于管理项目及其依赖项。 如果未安装,请从Apache Maven网站下载Maven或JAR文件。
  3. IronPDF for Java Library: 您还需要IronPDF for Java库,将其作为依赖项添加到您的Maven项目中。 这可以通过在项目的pom.xml文件中添加以下依赖项来完成。Maven会自动下载并安装到项目中。

    <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:pom.xml文件中添加Slf4j依赖项。

    <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

一旦设置了Java程序的PDF密码保护功能,您就可以使用IronPDF来保护PDF文件。

编写代码前的重要步骤

首先,在您的Java代码中导入必要的IronPDF类。 在"Main.java"文件顶部添加以下代码:

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

现在,在主方法中,使用IronPDF的setLicenseKey方法输入您的许可证密钥。

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

打开加密的PDF文档

以下代码片段将打开一个用密码"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

在上述代码片段中,用"password"密码打开了一个加密的PDF文件。

在Java中如何使用密码保护PDF,图1:打开加密的PDF文档 打开加密的PDF文档

使用密码保护加密PDF文档

让我们更改之前步骤中打开的"encrypted.pdf"文件的所有者密码。以下代码有助于实现此任务:

// 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

第一步是使用removePasswordsAndEncryption方法删除密码,然后使用setPassword方法设置一个新密码。

保存密码保护的PDF文档

最后,使用以下代码行保存PDF文档:

// 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

输出文件现在使用"secret-key"密码打开。

在Java中如何使用密码保护PDF,图2:新加密的PDF文档 新加密的PDF文档

编辑文件安全设置

通过IronPDF在Java中,可以使用SecurityOptions类轻松设置重要的安全选项。 下面的代码将使PDF为只读状态,并禁止用户复制、粘贴和打印,并为所有者和用户设置密码。

// 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

这将为PDF文档设置所有必要的安全选项。

在Java中如何使用密码保护PDF,图3:新PDF安全设置 新PDF安全设置

摘要

本文解释了如何在Java中通过IronPDF Library for Java打开现有PDF文档并添加密码保护。 IronPDF使处理Java中的PDF文件变得非常简单。 无论您是要创建新文档还是制作PDF查看器,IronPDF都能通过一行代码帮助实现这一任务。 IronPDF的引擎非常适合Java编程语言,因为它速度快且内存高效。 通过IronPDF,您可以设置用户密码和所有者密码。 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. 它的轻量版套餐从$799开始。 下载IronPDF并尝试一下。

常见问题解答

如何在Java中对PDF文档进行密码保护?

要在Java中对PDF文档进行密码保护,可以使用IronPDF的SecurityManager类设置用户密码。这需要初始化一个PdfDocument对象,使用setPassword设置所需密码,然后使用saveAs方法保存文件。

设置Java中的PDF项目需要什么?

要在Java中使用IronPDF设置PDF项目,您需要Java Development Kit (JDK)、Maven用于依赖管理,并且必须在项目的pom.xml文件中包含IronPDF库。

如何在Java中操作PDF文件?

可以使用IronPDF通过导入必要的类如PdfDocument在Java中操作PDF文件。这允许您编辑内容、合并文档,并使用SecurityOptions类应用安全设置。

如何使用Java限制PDF的打印?

使用IronPDF,可以通过配置SecurityOptions类来限制PDF的打印。设置适当的权限以禁止打印等操作,然后将这些设置应用到PdfDocument对象。

如何在Java中打开加密的PDF?

要在Java中使用IronPDF打开加密的PDF,使用PdfDocument.fromFile方法,提供文件路径和密码作为参数以解密并访问文档。

Java中是否有免费的PDF库版本?

IronPDF提供了一个免费试用版本,开发人员可以用来探索其功能。对于长期使用或商业项目,可以使用授权版本。

如何在Java中更改现有PDF的密码?

要使用IronPDF更改现有PDF的密码,先使用当前密码打开文档,使用SecurityManager移除它,然后在保存文档之前设置新密码。

Darrius Serrant
全栈软件工程师(WebOps)

Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。

在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。

对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。