如何在 Java 中使用 GroupDocs 為 PDF 加上圖片註釋

如果您需要 annotate PDF with image——例如在合約或培訓手冊上直接插入標誌、圖表或照片——GroupDocs.Annotation for Java 讓這變得輕鬆。於本教學中,您將學會如何新增圖片註釋、控制其不透明度與旋轉角度,並處理常見的問題,例如受密碼保護的 PDF 或大型檔案。完成後,您即可以程式方式將圖片嵌入 PDF,並自信地在生產環境中部署此解決方案。

快速解答

  • 我可以使用 Java 為 PDF 添加圖片嗎? Yes – use GroupDocs.Annotation’s ImageAnnotation class.
  • 哪個方法控制圖片不透明度? Call setOpacity(float) on the annotation object.
  • 我需要生產環境的授權嗎? A trial works for testing; a full license is required for commercial use.
  • 我可以為受密碼保護的 PDF 加上註釋嗎? Yes – provide the password when creating the Annotator.
  • 需要哪個 Java 版本? Java 8 以上,建議使用 Java 11 以上以獲得最佳效能。

什麼是向 PDF 添加圖片?

將圖片載入 PDF 頁面會建立一個 image annotation,它成為文件內容流的一部分。ImageAnnotation 是用來儲存圖片資料、位置、尺寸、旋轉角度與視覺樣式的物件,讓您可以像處理其他註釋類型一樣操作圖片。

為何使用 GroupDocs Annotation for Java?

載入 PDF,附加 ImageAnnotation,然後儲存——不需要外部檢視器。GroupDocs Annotation 支援 50+ 種輸入與輸出格式,可在不將整個檔案載入記憶體的情況下處理高達 500 MB 的 PDF,且可在 Windows、Linux 與 macOS 上執行。其 API 提供對位置、不透明度(0‑1 範圍)與旋轉角度(0‑360°)的精細控制,十分適合企業級文件工作流程。

前置條件

  • Java 8 或以上(建議使用 Java 11+)。
  • IDE – IntelliJ IDEA、Eclipse 或任何相容 Java 的編輯器。
  • Build tool – Maven 或 Gradle(範例使用 Maven)。

設定 GroupDocs.Annotation

在您的 pom.xml 中加入 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>

小技巧: 請務必在 GroupDocs 發佈頁面確認最新版本。Version 25.2 為 2025 年初的最新版本,但較新版本可能加入新功能。

授權(千萬別跳過!)

您有三個選項:

  1. Free trial – 完美的測試版 – 從 GroupDocs trial page 取得。
  2. Temporary license – 需要更長的評估時間?請從 temporary license page 取得。
  3. Full license – 生產環境使用 – 可於 purchase page 取得。

開始使用 – 您的第一個圖片註釋

步驟 1:初始化 annotator

Annotator 是開啟 PDF 並為修改做準備的入口點。Annotator 為核心類別,負責載入 PDF 文件、提供註釋集合,並將變更寫回磁碟。

try (final Annotator annotator = new Annotator("YOUR_DOCUMENT_DIRECTORY/input.pdf")) {
    // Your annotation magic happens here
}

為何使用 try‑with‑resources? 它可確保 annotator 關閉並釋放檔案句柄,防止記憶體洩漏。

步驟 2:建立並設定圖片註釋

以下為最小化的 ImageAnnotation 設定;ImageAnnotation 代表可放置於 PDF 頁面的圖片註釋。您需要定義矩形、透明度、頁碼、圖片來源與旋轉角度。

Rectangle 定義註釋在頁面上的位置與大小。Rectangle(100, 100, 100, 100) 表示「從左上角 (100, 100) 開始,建立 100 × 100 像素的方框」。請依需求調整這些數值以符合版面配置。

// Initialize the image annotation
class ImageAnnotation {
    public void setBox(Rectangle rectangle) { /* Implementation */ }
    public void setOpacity(double opacity) { /* Implementation */ }
    public void setPageNumber(int pageNumber) { /* Implementation */ }
    public void setImagePath(String imagePath) { /* Implementation */ }
    public void setAngle(double angle) { /* Implementation */ }
}

// Create your image annotation
ImageAnnotation imageAnnotation = new ImageAnnotation();

// Position and size (x, y, width, height in pixels)
imageAnnotation.setBox(new Rectangle(100, 100, 100, 100));

// Make it 70% opaque (0.0 = transparent, 1.0 = fully opaque)
imageAnnotation.setOpacity(0.7);

// Place it on the first page (0‑indexed)
imageAnnotation.setPageNumber(0);

// Your image source (can be local file or URL)
imageAnnotation.setImagePath("www.google.com.ua/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png");

// Rotate it 100 degrees (because why not?)
imageAnnotation.setAngle(100.);

了解 setOpacity – setOpacity(float) 方法將註釋的透明度設定在 0(完全透明)到 1(完全不透明)的範圍內。

步驟 3:套用註釋並儲存

現在將註釋附加至文件,並將結果寫入磁碟。

// Add the annotation to your document
annotator.add(imageAnnotation);

// Save the annotated PDF
annotator.save("YOUR_OUTPUT_DIRECTORY/result_image_annotation.pdf");

完成!您已成功 annotate PDF with image。

常見問題與解決方案

檔案路徑問題

  • 症狀: FileNotFoundException 或空白圖片。
  • 解決方法: 使用絕對路徑或確認 URL 可存取。
// Bad: relative path that may fail
imageAnnotation.setImagePath("images/logo.png");

// Good: absolute path
imageAnnotation.setImagePath("/full/path/to/your/images/logo.png");

圖片尺寸與品質

  • 症狀: 圖片像素化或過大。
  • 解決方法: 讓圖片尺寸與註釋矩形相匹配。
// Rectangle is 200 × 200, so use an image at least that size
imageAnnotation.setBox(new Rectangle(50, 50, 200, 200));

大型 PDF 的記憶體問題

  • 症狀: OutOfMemoryError。
  • 解決方法: 分批處理文件,並保持圖片檔案輕量。

何時使用圖片註釋於 PDF

當視覺資訊能提供純文字無法傳達的價值時,您應該在 PDF 中加入圖片註釋——例如在檢查報告中附上現場照片、在培訓工作表中嵌入圖表,或在合約上蓋上標誌。使用圖片註釋可保留原始 PDF 版面,同時即時向讀者傳遞額外的視覺資訊。

效能最佳實踐

最佳化圖片來源

// Avoid huge files
imageAnnotation.setImagePath("massive_10mb_image.png");

// Resize to match annotation box (e.g., 100 × 100)

批次處理策略

List<String> pdfFiles = Arrays.asList("doc1.pdf", "doc2.pdf", "doc3.pdf");

for (String pdfFile : pdfFiles) {
    try (final Annotator annotator = new Annotator(pdfFile)) {
        ImageAnnotation annotation = createImageAnnotation();
        annotator.add(annotation);
        annotator.save("annotated_" + pdfFile);
    }
}

資源管理

// Good – automatically closes resources
try (final Annotator annotator = new Annotator("input.pdf")) {
    // Your code here
}

// Bad – might cause memory leaks
Annotator annotator = new Annotator("input.pdf");
// ... do stuff ...
// Forgot to close!

進階設定技巧

動態定位

// Bottom‑right corner placement (assuming standard Letter size)
int pageWidth = 612;   // points
int pageHeight = 792;  // points
int imageSize = 50;

Rectangle dynamicPosition = new Rectangle(
    pageWidth - imageSize - 10,   // 10 px margin from right
    pageHeight - imageSize - 10,  // 10 px margin from bottom
    imageSize,
    imageSize
);

imageAnnotation.setBox(dynamicPosition);

單頁多圖

// Add a logo
ImageAnnotation logo = new ImageAnnotation();
logo.setBox(new Rectangle(50, 50, 100, 50));
logo.setImagePath("company_logo.png");
logo.setPageNumber(0);

// Add an approval stamp
ImageAnnotation stamp = new ImageAnnotation();
stamp.setBox(new Rectangle(400, 700, 100, 50));
stamp.setImagePath("approved_stamp.png");
stamp.setPageNumber(0);

annotator.add(logo);
annotator.add(stamp);

常見問答

Q: 我可以使用的圖片最大尺寸是多少?
A: 沒有硬性限制,但為了最佳效能,建議將圖片大小控制在 2 MB 以下。

Q: 我可以使用動畫 GIF 嗎?
A: GroupDocs 只會渲染動畫 GIF 的第一幀。

Q: 我如何精確定位圖片?
A: GroupDocs 以左上角為原點;Rectangle 座標以像素為單位,從該點測量。

Q: 我可以為受密碼保護的 PDF 加上註釋嗎?
A: 可以——在建立 Annotator 時提供密碼。

Q: 這適用於所有 PDF 版本嗎?
A: 支援的 PDF 版本範圍為 1.4 到 2.0,幾乎涵蓋您會遇到的所有 PDF。

結語

您現在已具備使用 GroupDocs.Annotation for Java annotate PDF with image 的堅實基礎。請記得:

  • 使用 try‑with‑resources 以確保資源正確釋放。
  • 最佳化圖片尺寸,使 PDF 保持輕量。
  • 使用絕對路徑測試,以避免路徑相關錯誤。
  • 選擇適合的透明度與旋轉角度以符合視覺設計。

下一步: 探索其他註釋類型(文字、形狀、標註)或將此邏輯整合至 Spring Boot 服務,以即時處理 PDF。

docs.groupdocs.com 的文件提供更多進階範例與 API 參考,供您深入了解。


Last Updated: 2026-09-15
Tested with: GroupDocs.Annotation 25.2 (Java)
Author: GroupDocs

資源與支援

相關教學