使用 IRONPDF FOR JAVA 如何在 Java 中保護 PDF 的密碼 Darrius Serrant 更新:2025年7月28日 下載 IronPDF Maven 下載 JAR 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 本文將展示如何使用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 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 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 在上面的代碼片段中,一個加密的PDF文件使用"password"密碼打開。 打開加密的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"密碼打開。 新加密的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文件設置所有必要的安全選項。 新的PDF安全設置 總結 本文解釋了如何使用Java中的IronPDF Library for Java打開現有的PDF文件並添加密碼保護。 IronPDF讓在Java中處理PDF文件變得更加容易。 無論您是要創建新的文件還是想製作PDF查看器,IronPDF都可以幫助您以一行代碼實現這一任務。 IronPDF的引擎非常適合Java編程語言,因為它速度快且記憶體效率高。 使用IronPDF,您可以設置用戶密碼以及擁有者密碼。 它提供了完整的保護選項,還有其他功能,如使用IronPDF從其他格式轉換為PDF、 使用IronPDF拆分文件和使用IronPDF合併文件。 IronPDF可以在IronPDF免費試用中免費使用,並可授權給IronPDF以進行商業使用。 它的Lite包從$799開始。 下載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 來說,工作令人滿意因為它被重視且有實際影響。 相關文章 更新2026年1月18日 如何在 Java 中將 TIFF 轉換為 PDF 本綜合指南將引導您逐步在 Java 中使用 IronPDF 無縫將 TIFF 圖像轉換為 PDF。 閱讀更多 更新2025年7月28日 如何在 Java 中將 PDF 轉換為 PDF/A 在本文中,我們將探討如何使用 IronPDF 在 Java 中將 PDF 文件轉換為 PDF/A 格式。 閱讀更多 更新2025年7月28日 如何在 Java 中創建 PDF 文檔 本文將提供一個全面指南,涵蓋 Java 中的 PDF 操作,包括關鍵概念、最佳庫和示例。 閱讀更多 Java PDF 渲染庫(開發人員教程)如何在 Java 中將位元組陣...