跳過到頁腳內容
使用 IRONPDF FOR JAVA

如何在 Java 中保護 PDF 的密碼

本文將示範如何使用IronPDF處理 PDF 文檔,以及如何使用使用者密碼保護新文件。

IronPDF - Java PDF 庫

IroqPDF Java PDF Library是一個用來處理 PDF 文件的 Java 函式庫。 它提供了一系列用於生成和操作 PDF 的功能,包括添加文字、圖像和其他類型的內容,以及控製文件的佈局和格式。 它還提供了許多保護 PDF 內容的重要功能,例如在IronPDF中使用密碼保護功能

在 Java 應用程式中使用密碼保護 PDF 的步驟

項目設定的前提條件

要在 Java Maven專案中使用IronPDF處理 PDF 文件,您需要確保具備以下先決條件:

  1. Java 開發工具包 (JDK):您的電腦上必須安裝最新版本的 Java。 如果沒有 JAR 文件,請從Oracle 網站下載最新的 JDK。
  2. Maven: Maven是 Java 專案的重要建置自動化工具,用於管理專案及其相依性。 如果尚未安裝 Maven,請從Apache Maven網站下載Maven或 JAR 檔案。
  3. IronPDF 適用於 Java 庫:您還需要IronPDF 適用於 Java 庫,該庫將作為依賴項新增到您的Maven專案中。 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 依賴項: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 檔案了。

編寫程式碼前的重要步驟

首先,將必要的IronPDF類別匯入到您的 Java 程式碼中。 在"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文檔

編輯檔案安全設定

使用 Java 中的IronPDF ,可以透過 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庫開啟現有的 PDF 文件並新增密碼保護。 IronPDF讓在 Java 中處理 PDF 檔案變得更容易。 無論您是想建立新文件還是製作 PDF 檢視器, IronPDF都能透過一行程式碼幫助您完成此任務。 IronPDF 的引擎非常適合 Java 程式語言,因為它速度快、記憶體效率高。 使用IronPDF,您可以設定使用者密碼以及所有者密碼。 它提供全面的保護選項以及其他功能,例如使用IronPDF將其他格式轉換為 PDF使用IronPDF分割文件以及使用IronPDF合併文件

IronPDF可以在IronPDF的免費試用版中免費使用,也可以透過IronPDF購買商業用途許可。 它的Lite軟體包從 $999 開始。 下載IronPDF並試試看。

常見問題解答

如何在Java中為PDF文件新增密碼保護?

若要在 Java 中為 PDF 文件設定密碼保護,可以使用 IronPDF 的SecurityManager類別來設定使用者密碼。這包括初始化一個PdfDocument對象,使用setPassword設定所需的密碼,然後使用saveAs方法儲存檔案。

如何在Java中設定PDF項目?

要在 Java 中使用IronPDF設定 PDF 項目,您需要 Java 開發工具包 (JDK)、用於依賴項管理的 Maven,並且必須將IronPDF庫包含在專案的pom.xml檔案中。

如何在Java中操作PDF檔案?

您可以使用IronPDF在 Java 中操作 PDF 文件,只需匯入必要的類,例如PdfDocument 。這樣,您就可以編輯內容、合併文檔,並使用SecurityOptions類別套用安全設定。

如何使用Java限制PDF文件的列印?

使用IronPDF,您可以透過設定SecurityOptions類別來限制 PDF 的列印。設定對應的權限以禁止列印等操作,然後將這些設定套用到您的PdfDocument物件。

如何在Java中開啟加密的PDF檔案?

若要使用IronPDF在 Java 中開啟加密的 PDF,請使用PdfDocument.fromFile方法,提供檔案路徑和密碼作為參數來解密和存取文件。

是否有適用於 Java 的免費 PDF 庫版本?

IronPDF提供免費試用版,供開發者體驗其各項功能。對於需要長期使用或商業項目的用戶,則可購買授權版本。

如何在Java中更改現有PDF文件的密碼?

若要使用IronPDF變更現有 PDF 的密碼,請開啟具有目前密碼的文檔,使用SecurityManager刪除它,然後在儲存文件之前設定新密碼。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我