使用 IRONPDF FOR JAVA 如何在 Java 中保護 PDF 的密碼 Darrius Serrant 更新日期:7月 28, 2025 Download IronPDF Maven 下載 JAR 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 本文將演示如何使用 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 項目的重要構建自動化工具,需要用於管理項目及其依賴項。 如果尚未安裝,請從 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 一旦設置好支持 PDF 密碼保護的 Java 程序後,您即可使用 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 文件。 打開加密的 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 文檔 編輯文件安全設置 可以使用 IronPDF 在 Java 中使用 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,您可以設置用戶密碼和擁有者密碼。 It provides full protection options along with other features like converting to PDF from other formats with IronPDF, splitting documents with IronPDF, and merging documents with IronPDF. IronPDF can be used for free in a free trial of IronPDF and can be licensed for commercial use with IronPDF. 其精簡包從 $799 開始。 下載IronPDF 並嘗試一下。 常見問題解答 如何在 Java 中對 PDF 文件進行密碼保護? 要在 Java 中對 PDF 文件進行密碼保護,您可以使用 IronPDF 的 `SecurityManager` 類設置用戶密碼。這需要初始化一個 `PdfDocument` 物件,使用 `setPassword` 設置所需密碼,然後使用 `saveAs` 方法保存文件。 設置 Java 中的 PDF 項目需要哪些條件? 要使用 IronPDF 在 Java 中設置 PDF 項目,您需要 Java Development Kit (JDK)、用于依賴管理的 Maven,並且必須在項目的 `pom.xml` 文件中包含 IronPDF 庫。 如何在 Java 中操作 PDF 文件? 您可以使用 IronPDF 通過匯入必要的類如 `PdfDocument` 在 Java 中操作 PDF 文件。這允許您編輯內容、合併文件以及使用 `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 來說,工作令人滿意因為它被重視且有實際影響。 相關文章 更新日期 6月 22, 2025 如何在 Java 中將 TIFF 轉換為 PDF 本綜合指南將引導您逐步在 Java 中使用 IronPDF 無縫將 TIFF 圖像轉換為 PDF。 閱讀更多 更新日期 7月 28, 2025 如何在 Java 中將 PDF 轉換為 PDF/A 在本文中,我們將探討如何使用 IronPDF 在 Java 中將 PDF 文件轉換為 PDF/A 格式。 閱讀更多 更新日期 7月 28, 2025 如何在 Java 中創建 PDF 文檔 本文將提供一個全面指南,涵蓋 Java 中的 PDF 操作,包括關鍵概念、最佳庫和示例。 閱讀更多 Java PDF 渲染庫(開發人員教程)如何在 Java 中將位元組陣...