Unwanted image watermarks can make PDF documents look unprofessional and hinder downstream processing. GroupDocs.Watermark Cloud SDK for Java offers a robust API that lets Java developers programmatically strip those watermarks from PDF files. This guide shows how to remove image watermark from PDF in Java using the SDK’s removal endpoint. By the end, you’ll be able to integrate automatic watermark removal into any Java application.
Java Development Environment - Prerequisites and Setup
Before you start, make sure you have the following:
- Java 17 or higher installed.
- Maven or Gradle for dependency management.
- A GroupDocs Cloud account with client ID and client secret.
Add the SDK to your project with the Maven dependency below (you can also use the Gradle equivalent). The latest binaries are available on the download page.
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-watermark-cloud</artifactId>
<version>23.8</version>
</dependency>
Initialize the API client using your credentials. This snippet is taken directly from the sample code.
ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
WatermarkApi watermarkApi = new WatermarkApi(apiClient);
With the client ready, you can move on to the actual removal workflow.
Step-by-Step Guide to Remove Image Watermark from PDF in Java
Step 1: Load the Source Document
First, specify the PDF you want to clean. The FileInfo object tells the API where the file lives and optionally supplies a password.
FileInfo inputFile = new FileInfo();
inputFile.setFilePath("input.pdf");
// inputFile.setPassword("pdfPassword"); // Uncomment if needed
Step 2: Define the Image Watermark Criteria
Create an ImageSearchCriteria that points to the watermark image you wish to delete. This is the core of the remove image watermark from PDF in Java operation.
ImageSearchCriteria imageCriteria = new ImageSearchCriteria();
imageCriteria.setImagePath("watermark.png");
Step 3: Set Up Removal Options
Combine the file information and search criteria into a RemoveOptions object. You also define where the cleaned PDF will be saved.
RemoveOptions removeOptions = new RemoveOptions();
removeOptions.setFileInfo(inputFile);
removeOptions.setOutputPath("output.pdf");
removeOptions.setSearchCriteria(Collections.singletonList(imageCriteria));
Step 4: Execute the Removal Call
Invoke the remove method on the WatermarkApi. The call returns a RemoveResult that contains the path to the processed file.
RemoveResult result = watermarkApi.remove(removeOptions);
System.out.println("Watermark removal completed. Output file: " + result.getPath());
Step 5: Process the Result
Handle any exceptions that may arise. The SDK throws ApiException for service‑side errors and generic Exception for unexpected issues.
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());
}
Complete Code Example: Remove Image Watermark Using GroupDocs
The following example demonstrates the full workflow from client initialization to result handling.
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());
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Watermark Removal Operation via REST API using cURL
If you prefer a pure REST approach, the same operation can be performed with cURL commands.
Authenticate and obtain an access token
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"}'Upload the source 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"Execute the watermark removal
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"}] }'Download the cleaned PDF
curl -X GET "https://api.groupdocs.cloud/v2.0/storage/download?path=output.pdf" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -o output.pdf
For a full reference, see the API documentation.
Fine-Tuning Removal Options
The SDK provides several properties you can adjust to suit different scenarios.
Password Protection - If the source PDF is encrypted, set the password on
FileInfo.inputFile.setPassword("mySecret");Multiple Search Criteria - Remove several watermarks in one call by adding more
ImageSearchCriteriaobjects to the list.ImageSearchCriteria logoCriteria = new ImageSearchCriteria(); logoCriteria.setImagePath("logo.png"); removeOptions.setSearchCriteria(Arrays.asList(imageCriteria, logoCriteria));Output Format - Although the default is PDF, you can change the output format by setting
outputPathwith a different extension (e.g.,output.docx).removeOptions.setOutputPath("output.docx");
These options let you tailor the remove image watermark from PDF in Java process to your exact needs.
Conclusion
Programmatically removing an image watermark from PDF files is straightforward with the GroupDocs.Watermark Cloud SDK for Java. By following the steps above, you can integrate watermark deletion into any Java backend, handle encrypted PDFs, and fine‑tune the operation with flexible options. Remember to obtain a proper license for production use; pricing details are available on the product page, and a temporary license can be requested via the temporary license page. Start cleaning your PDFs today and deliver polished documents to your users.
FAQs
How do I remove image watermark from PDF in Java?
Use theremovemethod ofWatermarkApiwith anImageSearchCriteriathat points to the watermark image. The full code example in this article shows the exact steps.What if my PDF is password protected?
Set the password on theFileInfoobject (inputFile.setPassword("yourPassword")) before calling the removal API. The SDK will decrypt, process, and re‑encrypt the file automatically.Can I remove watermarks from other formats, such as DOCX?
Yes. The same API works with any format supported by GroupDocs.Watermark. Just change thefilePathandoutputPathextensions accordingly.Are there limits on the size of the PDF I can process?
The cloud service handles files up to 500 MB per request. For larger documents, consider splitting the PDF into smaller parts before processing.
