如何使用 Java 對 PDF 進行塗抹 – 完整 GroupDocs 教程
如果您需要 使用 Java 對 PDF 進行塗抹,您已來到正確的地方。無論是清理法律合約、醫療記錄,或是機密商業報告,本教學將帶您使用 GroupDocs.Annotation 完成一套可投入生產環境的解決方案。我們會涵蓋從環境設定到批次處理、安全性考量與除錯技巧的全部內容,讓您能自信地保護敏感資料。
快速答覆
- 哪個函式庫負責在 Java 中執行 PDF 塗抹? GroupDocs.Annotation Java API。
- 塗抹是永久性的嗎? 是 – 會真正移除底層文字,而非僅隱藏。
- 生產環境需要授權嗎? 必須使用完整授權;測試時可取得免費的臨時授權。
- 可以一次處理多個檔案嗎? 當然可以 – 本文說明批次處理與資源重用方式。
- 建議使用哪個 Java 版本? 為了取得最佳效能與安全性,建議使用 Java 11 以上。
什麼是 PDF 塗抹,為什麼要使用 GroupDocs.Annotation?
PDF 塗抹是永久移除或遮蔽文件中敏感內容的過程。GroupDocs.Annotation 之所以出色,是因為它提供 真正的塗抹、可供稽核的回覆,以及支援多種註解類型——這些都是合規導向產業的必備功能。
為什麼選擇 GroupDocs.Annotation 進行 PDF 塗抹?
- 永久移除文字(符合 HIPAA 等等級的安全性)。
- 豐富的註解生態系 – 可將塗抹與高亮、評論、箭頭等結合。
- 企業級效能,適用於高容量工作負載。
- 跨格式支援 – 不僅限於 PDF。
- 細緻的外觀、透明度與中繼資料控制。
前置條件與環境設定
必要的相依套件
將 GroupDocs.Annotation 加入您的 Maven 專案。請保持以下程式碼片段的原樣:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/annotation/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-annotation</artifactId>
<version>25.2</version>
</dependency>
</dependencies>
開發環境清單
- Java 8+(建議使用 Java 11+)。
- Maven 3.6+(或相等的 Gradle)。
- 具備 Maven 支援的 IDE(IntelliJ IDEA、Eclipse、VS Code)。
- 測試用 PDF,內含真實的敏感資料,以便進行實際驗證。
授權考量
開發與測試階段可取得 免費臨時授權。正式上線則必須購買完整授權,試用版已提供全部功能供評估使用。
如何使用 GroupDocs.Annotation 於 Java 進行 PDF 塗抹
步驟 1:初始化 PDF 註解器
建立指向欲保護 PDF 的 Annotator 實例。
import com.groupdocs.annotation.Annotator;
// Initialize annotator object
dual Annotator annotator = new Annotator("YOUR_DOCUMENT_DIRECTORY/input.pdf");
專業提示: 使用 try‑with‑resources 或手動釋放資源,以避免記憶體泄漏。稍後會再次說明正確的清理方式。
步驟 2:建立稽核回覆的註解
透過加入回覆物件,說明每筆塗抹的原因。
import com.groupdocs.annotation.models.Reply;
import java.util.ArrayList;
import java.util.Calendar;
// Create reply objects with comments and timestamps
dual Reply reply1 = new Reply();
reply1.setComment("First comment");
reply1.setRepliedOn(Calendar.getInstance().getTime());
dual Reply reply2 = new Reply();
reply2.setComment("Second comment");
reply2.setRepliedOn(Calendar.getInstance().getTime());
List<Reply> replies = new ArrayList<>();
replies.add(reply1);
replies.add(reply2);
這些回覆會成為文件稽核日誌的一部份,滿足多數合規需求。
步驟 3:定義精確的塗抹邊界
正確的座標確保正確的文字被移除。原點 (0,0) 為頁面的左上角。
import com.groupdocs.annotation.models.Point;
import java.util.ArrayList;
// Define points for annotation boundaries
dual Point point1 = new Point(80, 730);
dual Point point2 = new Point(240, 730);
dual Point point3 = new Point(80, 650);
dual Point point4 = new Point(240, 650);
List<Point> points = new ArrayList<>();
points.add(point1);
points.add(point2);
points.add(point3);
points.add(point4);
小技巧: 使用能顯示座標的 PDF 檢視器,或自行開發 UI 讓使用者點選以自動取得座標。
步驟 4:建立文字塗抹註解
將座標、稽核回覆與說明訊息綁定在一起。
import com.groupdocs.annotation.models.annotationmodels.TextRedactionAnnotation;
// Create text redaction annotation with properties
dual TextRedactionAnnotation textRedaction = new TextRedactionAnnotation();
textRedaction.setCreatedOn(Calendar.getInstance().getTime());
textRedaction.setMessage("This is a text redaction annotation");
textRedaction.setPageNumber(0);
textRedaction.setPoints(points);
textRedaction.setReplies(replies);
// Add the annotation to the document
annotator.add(textRedaction);
setMessage() 欄位會記錄塗抹原因,同時不會暴露被隱藏的內容。
步驟 5:儲存塗抹後的文件並清理資源
將變更寫入檔案,並釋放相關資源。
// Save the annotated document
dual annotator.save("YOUR_OUTPUT_DIRECTORY/annotated_output.pdf");
// Release resources
dual annotator.dispose();
關鍵: 必須呼叫
dispose()(或使用 try‑with‑resources)以釋放檔案句柄與記憶體。
常見問題與解決方案
座標與預期區域不符
- 原因: PDF 產生工具可能使用不同的座標原點。
- 解決方式: 使用與正式環境相同的檢視器驗證座標,或實作預覽工具讓使用者微調點位。
大量處理時的記憶體泄漏
- 原因: Annotator 實例會持有檔案串流。
- 解決方式: 使用 try‑with‑resources 以保證釋放:
try (Annotator annotator = new Annotator("input.pdf")) {
// annotation logic
annotator.save("output.pdf");
} // automatically disposed
儲存後註解未顯示
- 原因: 在
save()之後才呼叫add(),或座標超出頁面範圍。 - 解決方式: 確保
add()先於save(),並再次確認所有點位皆在頁面尺寸內。
效能優化建議
批次處理策略
在需要處理多個檔案時,重複使用同一個 annotator 實例。
// Less efficient - creates new instances
for (String file : files) {
try (Annotator annotator = new Annotator(file)) {
// process
}
}
// More efficient - batch processing
try (Annotator annotator = new Annotator()) {
for (String file : files) {
annotator.load(file);
// process annotations
annotator.save(outputFile);
annotator.clear(); // Prepare for next file
}
}
記憶體管理最佳實踐
- 盡可能將大型 PDF 分段處理。
- 依文件大小設定 JVM 堆積上限(
-Xmx)。 - 在負載測試期間監控堆積使用情形,以決定最佳批次大小。
- 對大量文件集合使用串流 API。
敏感資料的安全考量
真正的塗抹 vs. 僅視覺隱藏
GroupDocs.Annotation 會從 PDF 的內容串流中移除文字,確保即使使用文字抽取工具也無法恢復——這是 HIPAA、GDPR 等法規的必要條件。
臨時檔案清理
程式庫在處理過程中可能會寫入臨時檔案。請將這些檔案存放於安全、非公開的目錄,並確保在作業完成後已被刪除。
真實案例
| 行業 | 典型情境 |
|---|---|
| 法律 | 在電子發現前移除客戶的特權資訊。 |
| 醫療 | 從研究用 PDF 中剔除患者身分識別資訊。 |
| 金融 | 在公開季報前清理機密內容。 |
| 人力資源 | 在內部備忘錄中塗抹員工個人資料。 |
進階客製化
自訂塗抹外觀
控制最終 PDF 中塗抹的顯示方式。
textRedaction.setBackgroundColor(Color.BLACK); // Solid black block
textRedaction.setOpacity(1.0); // Fully opaque
結合多種註解類型
您可以同時加入高亮、評論或箭頭,打造完整的審閱工作流程。
生產環境錯誤處理
try (Annotator annotator = new Annotator(inputPath)) {
// annotation code
annotator.save(outputPath);
} catch (Exception e) {
logger.error("Redaction failed for {}: {}", inputPath, e.getMessage());
// optional retry or fallback logic
}
將每筆塗抹事件(包括文件名稱、時間戳記與使用者 ID)記錄下來,可建立堅實的稽核追蹤。
常見問答
Q: 塗抹的文字會永久移除嗎?
A: 會。GroupDocs.Annotation 會從 PDF 的內部結構中刪除文字,無法透過一般抽取工具復原。
Q: 儲存後可以復原塗抹嗎?
A: 不行。塗抹設計上是不可逆的,以符合合規需求。如需保留原始內容,請保留未塗抹的副本。
Q: 函式庫支援掃描版 PDF 嗎?
A: 掃描版 PDF 為影像,需要先使用 OCR 取得文字位置後才能進行塗抹。GroupDocs 提供可無縫整合的 OCR 附加元件。
Q: 大文件的效能表現如何?
A: 處理時間大致與頁數與註解數量線性成長。對於超過 100 頁的文件,建議使用非同步處理並加入進度回報機制。
Q: 可以將 PDF 存放於雲端儲存(如 AWS S3)並仍使用 API 嗎?
A: 可以。只要 Java 執行環境能取得檔案串流——無論是掛載 bucket 還是先下載至臨時位置——API 的使用方式皆相同。
最後更新: 2026-02-18
測試版本: GroupDocs.Annotation 25.2
作者: GroupDocs