通過Java加密 PDF

使用 Java鎖定帶有.PDF庫的“檔”。

使用 Java 庫加密 PDF 文件

為了加密PDF文件,我們將使用[Aspose.PDF用於Java](https://products.aspose.com/pdf/java)API,這是一個功能豐富,功能強大且易於使用的Java平台轉換API。您可以直接從 [Maven](https://repository.aspose.com/webapp/#/artifacts/browse/tree/General/repo/com/aspose/aspose-pdf)下載其最新版本,並通過將以下配置添加到 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 文件。
  2. 為使用者和擁有者創建密碼。
  3. 呼叫「文件加密」 方法。
  4. 保存 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");