跳至頁尾內容
USING IRONPDF FOR JAVA

如何在 Java 中保護 PDF 的密碼

本文章將示範如何使用IronPDF處理PDF文件,並透過使用者密碼保護新檔案。

IronPDF - Java PDF程式庫

IronPDF 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專案的重要建置自動化工具,為管理專案及其依賴項所必需。 如果您尚未安裝,請從Apache Maven網站下載Maven或JAR文件。
  3. 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
  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

在您設置完具有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

現在,在main方法中,使用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

第一步是使用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文件

編輯文件安全設定

可以用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文件設置所有必要的安全選項。

如何在Java中對PDF進行密碼保護,圖3:新PDF安全設置 新PDF安全設置

摘要

本文章解釋了如何使用IronPDF Library for Java在Java中打開現有的PDF文件並新增密碼保護。 IronPDF使在Java中處理PDF文件變得更加容易。 無論您是想建立新的文件還是製作PDF查看器,IronPDF都能通過一行程式碼幫助實現這一任務。 IronPDF的引擎非常適合Java程式語言,因為它快速且記憶體效率高。 使用IronPDF,您可以設置使用者密碼和擁有者密碼。 它提供完整的保護選項,以及其他功能,如從其他格式轉換為PDF拆分文件合併文件

IronPDF可以在IronPDF的免費試用版中免費使用,並且可以授權用於商業用途。 其精簡套件從$999開始。 下載IronPDF並嘗試一下。

常見問題

如何在Java中為PDF文件加密密碼?

要在Java中為PDF文件加密密碼,您可以使用IronPDF的SecurityManager類來設置使用者密碼。這涉及初始化PdfDocument物件,使用setPassword設置所需密碼,然後使用saveAs方法保存文件。

我需要什麼來設置一個Java中的PDF專案?

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

您如何在Java中操作PDF文件?

您可以通過匯入必要的類如PdfDocument來使用IronPDF在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來說,他的工作是有意義的,因為它有價值且對社會有真正的影響。

Iron 支援團隊

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