不需要的圖像水印會讓 PDF 文件顯得不專業,並妨礙後續處理。 GroupDocs.Watermark Cloud SDK for Java 提供強大的 API,讓 Java 開發人員能以程式方式從 PDF 檔案中去除這些水印。本指南說明如何使用 SDK 的移除端點 在 Java 中移除 PDF 圖像水印。完成後,您將能將自動水印移除整合到任何 Java 應用程式中。

Java 開發環境 - 前置條件與設定

在開始之前,請確保您具備以下條件:

  • 已安裝 Java 17 或更高版本。
  • 用於相依性管理的 Maven 或 Gradle。
  • 具有 client IDclient secret 的 GroupDocs Cloud 帳戶。

將 SDK 添加到您的專案中,使用以下 Maven 依賴項(您也可以使用等效的 Gradle)。最新的二進位檔可在下載頁面上取得。

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-watermark-cloud</artifactId>
    <version>23.8</version>
</dependency>

使用您的憑證初始化 API 客戶端。此程式碼片段直接取自範例程式碼。

ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
WatermarkApi watermarkApi = new WatermarkApi(apiClient);

當客戶端已就緒時,您可以繼續實際的移除工作流程。

逐步指南:在 Java 中從 PDF 移除圖像水印

步驟 1:載入來源文件

首先,指定您要清理的 PDF。FileInfo 物件告訴 API 檔案所在的位置,並可選地提供密碼。

FileInfo inputFile = new FileInfo();
inputFile.setFilePath("input.pdf");
// inputFile.setPassword("pdfPassword"); // Uncomment if needed

步驟 2:定義圖像水印標準

建立一個指向您想要刪除的浮水印圖像的 ImageSearchCriteria。這是 在 Java 中從 PDF 移除圖像浮水印 操作的核心。

ImageSearchCriteria imageCriteria = new ImageSearchCriteria();
imageCriteria.setImagePath("watermark.png");

第 3 步:設定移除選項

將檔案資訊和搜尋條件組合成 RemoveOptions 物件。您還可以定義清理後的 PDF 將儲存的位置。

RemoveOptions removeOptions = new RemoveOptions();
removeOptions.setFileInfo(inputFile);
removeOptions.setOutputPath("output.pdf");
removeOptions.setSearchCriteria(Collections.singletonList(imageCriteria));

步驟 4:執行刪除呼叫

WatermarkApi 上調用 remove 方法。此呼叫會返回一個包含已處理檔案路徑的 RemoveResult

RemoveResult result = watermarkApi.remove(removeOptions);
System.out.println("Watermark removal completed. Output file: " + result.getPath());

第5步:處理結果

處理可能發生的任何例外情況。SDK 會拋出 ApiException 以表示服務端錯誤,並拋出通用的 Exception 以表示意外問題。

try {
    // removal code above
} catch (ApiException e) {
    System.err.println("API exception occurred: " + e.getMessage());
} catch (Exception e) {
    System.err.println("Unexpected error: " + e.getMessage());
}

完整程式碼範例:使用 GroupDocs 移除影像浮水印

以下範例展示了從客戶端初始化到結果處理的完整工作流程。

import com.groupdocs.watermark.cloud.api.WatermarkApi;
import com.groupdocs.watermark.cloud.client.ApiClient;
import com.groupdocs.watermark.cloud.client.ApiException;
import com.groupdocs.watermark.cloud.model.FileInfo;
import com.groupdocs.watermark.cloud.model.ImageSearchCriteria;
import com.groupdocs.watermark.cloud.model.RemoveOptions;
import com.groupdocs.watermark.cloud.model.RemoveResult;
import java.util.Collections;

public class RemoveImageWatermarkFromPdf {
    public static void main(String[] args) {
        // Initialize API client with your credentials
        ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
        WatermarkApi watermarkApi = new WatermarkApi(apiClient);

// Prepare input file information
        FileInfo inputFile = new FileInfo();
        inputFile.setFilePath("input.pdf");
        // If the PDF is password protected, uncomment the following line and set the password
        // inputFile.setPassword("pdfPassword");

// Define the image watermark to be removed
        ImageSearchCriteria imageCriteria = new ImageSearchCriteria();
        imageCriteria.setImagePath("watermark.png");

// Set up removal options
        RemoveOptions removeOptions = new RemoveOptions();
        removeOptions.setFileInfo(inputFile);
        removeOptions.setOutputPath("output.pdf");
        removeOptions.setSearchCriteria(Collections.singletonList(imageCriteria));

try {
            // Execute removal
            RemoveResult result = watermarkApi.remove(removeOptions);
            System.out.println("Watermark removal completed. Output file: " + result.getPath());
        } catch (ApiException e) {
            System.err.println("API exception occurred: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("Unexpected error: " + e.getMessage());
        }
    }
}

注意: 此程式碼範例展示了核心功能。在您的專案中使用之前,請確保更新所有檔案路徑和設定值以符合實際環境,驗證所有必要的相依項目已正確安裝,並在開發環境中徹底測試。若遇到任何問題,請參閱官方文件或聯繫支援團隊以獲得協助。

使用 cURL 透過 REST API 進行水印移除操作

如果您偏好純粹的 REST 方法,則可以使用 cURL 命令執行相同的操作。

  1. 驗證並取得存取權杖
curl -X POST "https://api.groupdocs.cloud/v2.0/auth/token" \
     -H "Content-Type: application/json" \
     -d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'
  1. 上傳源 PDF
curl -X POST "https://api.groupdocs.cloud/v2.0/storage/upload?path=input.pdf" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -F "file=@/local/path/input.pdf"
  1. 執行水印移除
curl -X POST "https://api.groupdocs.cloud/v2.0/watermark/remove" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "fileInfo": {"filePath":"input.pdf"},
           "outputPath":"output.pdf",
           "searchCriteria":[{"type":"Image","imagePath":"watermark.png"}]
         }'
  1. 下載已清理的 PDF
curl -X GET "https://api.groupdocs.cloud/v2.0/storage/download?path=output.pdf" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o output.pdf

如需完整參考,請參閱 API 文檔

微調移除選項

SDK 提供了多個屬性,您可以根據不同情況進行調整。

  • 密碼保護 - 如果來源 PDF 已加密,請在 FileInfo 上設定密碼。
  inputFile.setPassword("mySecret");
  • 多重搜尋條件 - 透過在清單中加入更多 ImageSearchCriteria 物件,一次呼叫即可移除多個浮水印。
  ImageSearchCriteria logoCriteria = new ImageSearchCriteria();
  logoCriteria.setImagePath("logo.png");
  removeOptions.setSearchCriteria(Arrays.asList(imageCriteria, logoCriteria));
  • 輸出格式 - 雖然預設是 PDF,但您可以透過設定 outputPath 為不同的副檔名(例如 output.docx)來更改輸出格式。
  removeOptions.setOutputPath("output.docx");

這些選項讓您能夠根據具體需求調整 在 Java 中從 PDF 移除圖像水印 流程。

結論

以程式方式從 PDF 檔案中移除圖像浮水印相當簡單,只需使用 GroupDocs.Watermark Cloud SDK for Java。按照上述步驟,您可以將浮水印刪除功能整合到任何 Java 後端,處理加密的 PDF,並透過彈性的選項微調操作。請記得取得正式授權以供生產環境使用;產品頁面提供價格細節,亦可透過臨時授權頁面 申請臨時授權。立即開始清理您的 PDF,為使用者提供精緻的文件。

常見問題

  • 如何在 Java 中移除 PDF 的圖片水印?
    使用 WatermarkApiremove 方法,搭配指向水印圖片的 ImageSearchCriteria。本文中的完整程式碼範例展示了具體步驟。

  • 如果我的 PDF 受密碼保護該怎麼辦?
    在呼叫移除 API 之前,於 FileInfo 物件上設定密碼 (inputFile.setPassword("yourPassword"))。SDK 會自動解密、處理,並重新加密檔案。

  • 我可以從其他格式(例如 DOCX)中移除水印嗎?
    是的。相同的 API 可用於 GroupDocs.Watermark 支援的任何格式。只需相應更改 filePathoutputPath 的擴展名即可。

  • 我可以處理的 PDF 大小是否有限制?
    雲服務每個請求可處理的檔案上限為 500 MB。對於較大的文件,請考慮在處理前將 PDF 拆分為較小的部分。

閱讀更多