如何在 Java 中保護 PDF 的密碼
本文將示範如何使用 IronPDF 處理 PDF 文檔,以及如何使用使用者密碼保護新文件。
IronPDF - Java PDF 庫
IroqPDF Java PDF Library是一個用來處理 PDF 文件的 Java 函式庫。 它提供了一系列用於生成和操作 PDF 的功能,包括添加文字、圖像和其他類型的內容,以及控製文件的佈局和格式。 它還提供了許多保護 PDF 內容的重要功能,例如在 IronPDF 中使用密碼保護功能。
在 Java 應用程式中使用密碼保護 PDF 的步驟
項目設定的前提條件
若要在 Java Maven 專案中使用 IronPDF 處理 PDF 文件,您需要確保具備以下先決條件:
- Java 開發工具包 (JDK):您的電腦上必須安裝最新版本的 Java。 如果沒有 JAR 文件,請從Oracle 網站下載最新的 JDK。
- Maven: Maven 是 Java 專案的重要建置自動化工具,用於管理專案及其相依性。 如果尚未安裝 Maven,請從Apache Maven 網站下載 Maven 或 JAR 檔案。
IronPDF for Java 庫:您還需要 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>XMLSlf4j 依賴項:在
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;現在,在主方法中,使用 IronPDF setLicenseKey方法輸入您的許可證密鑰。
// Set your IronPDF license key
License.setLicenseKey("Your license key");// Set your IronPDF license key
License.setLicenseKey("Your license key");開啟加密的 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");上面的程式碼片段使用密碼"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");第一步是使用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"));現在使用"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);這將為 PDF 文件設定所有必要的安全選項。
如何在 Java 中為 PDF 設定密碼保護,圖 3:新的 PDF 安全性設定 新的 PDF 安全設定
摘要
本文介紹如何使用 Java 中的IronPDF 庫開啟現有的 PDF 文件並新增密碼保護。 IronPDF 讓在 Java 中處理 PDF 檔案變得更容易。 無論您是想建立新文件還是製作 PDF 檢視器,IronPDF 都能透過一行程式碼幫助您完成此任務。 IronPDF 的引擎非常適合 Java 程式語言,因為它速度快、記憶體效率高。 使用 IronPDF,您可以設定使用者密碼以及所有者密碼。 它提供全面的保護選項以及其他功能,例如使用 IronPDF 將其他格式轉換為 PDF 、使用 IronPDF 分割文件以及使用 IronPDF 合併文件。
IronPDF 可以在IronPDF 的免費試用版中免費使用,也可以透過 IronPDF 購買商業用途許可。 其精簡版套餐起價為$799 。 下載IronPDF並試試看。
常見問題解答
如何在 Java 中對 PDF 文件進行密碼保護?
要在 Java 中用密碼保護 PDF 文件,您可以使用 IronPDF for Java 的 SecurityManager 類來設定使用者密碼。這包括初始化一個 PdfDocument 物件,使用 setPassword 設定所需的密碼,然後用 saveAs 方法儲存檔案。
在 Java 中建立 PDF 專案需要什麼?
要在 Java 中使用 IronPDF 建立 PDF 專案,您需要 Java 開發套件 (JDK)、用於相依性管理的 Maven,而且您必須在專案的 pom.xml 檔案中包含 IronPDF 函式庫。
如何在 Java 中處理 PDF 檔案?
您可以使用 IronPDF for Java 在 Java 中操作 PDF 檔案,方法是匯入必要的類別,例如 PdfDocument 。這可讓您編輯內容、合併文件,以及使用 SecurityOptions 類應用安全性設定。
如何使用 Java 限制 PDF 的列印?
使用 IronPDF,您可以通過配置 SecurityOptions 類來限制 PDF 的列印。設定適當的權限以禁止列印等動作,然後將這些設定套用至您的 PdfDocument 物件。
在 Java 中開啟加密 PDF 的步驟是什麼?
要在 Java 中使用 IronPDF 開啟加密的 PDF,請使用 PdfDocument.fromFile 方法,提供檔案路徑和密碼作為參數,以解密並存取文件。
是否有適用於 Java 的免費版 PDF 函式庫?
IronPDF 提供免費的試用版,開發人員可以用來探索其功能。對於擴展使用或商業專案,則提供授權版本。
如何在 Java 中變更現有 PDF 的密碼?
若要使用 IronPDF 更改現有 PDF 的密碼,請使用目前的密碼開啟文件,使用 SecurityManager 移除密碼,並在儲存文件前設定新密碼。







