通过 Java 加密 PDF

使用 Java 使用 Aspose.PDF 库锁定 PDF 文件。

使用 Java 库加密 PDF 文档

为了加密 PDF 文件,我们将使用 Aspose.PDF for Java API,这是一款功能丰富、功能强大且易于使用的适用于 Java 平台的转换 API。你可以直接从 Maven 下载它的最新版本,然后通过在 pom.xml 中添加以下配置将其安装在基于 Maven 的项目中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

如何通过 Java 加密 PDF


你需要 Aspose.PDF for Java 才能在你的环境中试用代码。

1.使用文档对象打开 PDF 文档。 1.为用户和所有者创建密码。 1.拨打文档.加密方法。 1.保存 PDF 文件。

<% encrypt.code-block.text %>

加密 PDF 文档-Java。

<% encrypt.code-block.subtitle %>

Input file:

File not added

Password:

Output format:

Output file:


  Document document = new Document(_dataDir + "input.pdf");
  DocumentPrivilege documentPrivilege = DocumentPrivilege.getForbidAll();
  documentPrivilege.setAllowScreenReaders(true);
  document.encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false);
  document.save(_dataDir + "SetPrivileges_out.pdf");