watermark pdf java – Add a Text Watermark Using GroupDocs.Watermark for Java

Adding a text watermark to your PDF files is a reliable way to protect sensitive information and reinforce brand identity. In this guide you’ll learn how to watermark PDF Java documents using GroupDocs.Watermark for Java, from project setup to saving the final watermarked file.

Quick Answers

  • What library is recommended? GroupDocs.Watermark for Java
  • How many lines of code are needed? About 30 lines across 5 steps
  • Do I need a license? A trial works for testing; a full license is required for production
  • Can I process large PDFs? Yes—process pages in a loop and close resources promptly
  • Is the watermark visible on images? Yes, you can apply it to embedded image artifacts

What is watermark pdf java?

watermark pdf java refers to the process of programmatically embedding visible or semi‑transparent text or image marks into PDF files using Java code. This technique helps deter unauthorized distribution and clearly shows document ownership.

Why use GroupDocs.Watermark for Java?

  • Easy integration – Simple Maven dependency and a clean API.
  • Broad format support – Works with PDFs, Word, Excel, and images.
  • Fine‑grained control – Position, rotation, scaling, and opacity can be customized.
  • Performance‑focused – Handles large files efficiently when you close the Watermarker after saving.

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • GroupDocs.Watermark Library version 24.11 (or newer)
  • An IDE such as IntelliJ IDEA or Eclipse with Maven support
  • Basic knowledge of Java and PDF structure

Setting Up GroupDocs.Watermark for Java

Maven Setup

Add the repository and dependency to your pom.xml:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/watermark/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-watermark</artifactId>
      <version>24.11</version>
   </dependency>
</dependencies>

Direct Download

Alternatively, download the library directly from GroupDocs.Watermark for Java releases.

License Acquisition Steps

  • Free Trial – Test all features with a temporary license.
  • Purchase – Obtain a full license for unlimited production use.

Basic Initialization and Setup

Import the core classes you’ll need:

import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.PdfLoadOptions;

Implementation Guide – watermark pdf java

Below is a step‑by‑step walkthrough that uses the same seven code blocks from the original tutorial.

Step 1: Load PDF Document

First, load the PDF you want to protect:

String pdfPath = "YOUR_DOCUMENT_DIRECTORY/document.pdf";
PdfLoadOptions loadOptions = new PdfLoadOptions();
Watermarker watermarker = new Watermarker(pdfPath, loadOptions);

Why? This creates a Watermarker instance that gives you full access to the PDF’s content.

Step 2: Initialize Text Watermark (add text watermark pdf)

Create a text watermark and define its appearance:

import com.groupdocs.watermark.common.HorizontalAlignment;
import com.groupdocs.watermark.common.VerticalAlignment;
import com.groupdocs.watermark.watermarks.Font;
import com.groupdocs.watermark.watermarks.SizingType;
import com.groupdocs.watermark.watermarks.TextWatermark;

TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
watermark.setRotateAngle(45);
watermark.setSizingType(SizingType.ScaleToParentDimensions);
watermark.setScaleFactor(1);

Why? Adjusting alignment, rotation, and scaling makes the watermark both noticeable and aesthetically pleasing.

Step 3: Access PDF Content and Pages

Iterate through each page so you can target specific elements:

import com.groupdocs.watermark.contents.PdfContent;
import com.groupdocs.watermark.contents.PdfPage;

PdfContent pdfContent = watermarker.getContent(PdfContent.class);
for (PdfPage page : pdfContent.getPages()) {
    // Process each page as needed.
}

Why? Direct page access lets you apply the watermark only where it’s needed.

Step 4: Apply watermark to pdf images (apply watermark to pdf)

Add the watermark to every image artifact found on each page:

import com.groupdocs.watermark.contents.PdfArtifact;

for (PdfPage page : pdfContent.getPages()) {
    for (PdfArtifact artifact : page.getArtifacts()) {
        if (artifact.getImage() != null) {
            artifact.getImage().add(watermark);
        }
    }
}

Why? Watermarking embedded images prevents visual content from being reused without attribution.

Step 5: Save and Close Watermarked PDF Document (java add watermark code)

Finally, write the changes to a new file and release resources:

import java.io.File;

String outputPath = "YOUR_OUTPUT_DIRECTORY/output.pdf";
watermarker.save(outputPath);
watermarker.close();

Why? Saving persists the watermark, and closing frees memory—critical for large PDFs.

Practical Applications

  • Document Security – Guard confidential reports, contracts, or invoices.
  • Brand Reinforcement – Display company name or logo across all pages.
  • Copyright Protection – Deter unauthorized redistribution of proprietary material.

Performance Considerations

  • Use efficient loops (as shown) to avoid unnecessary overhead.
  • Close the Watermarker promptly to release file handles.
  • For bulk operations, process files in batches and reuse a single Watermarker instance when possible.

Common Issues and Solutions

IssueSolution
OutOfMemoryError on large PDFsProcess pages one at a time and call watermarker.close() after each file.
Watermark not visible on some pagesVerify that the page actually contains image artifacts; otherwise apply the watermark directly to the page background.
License not recognizedEnsure the temporary or full license file is placed in the application’s working directory or set via License.setLicense("license_file_path").

Frequently Asked Questions

Q: Can I watermark file types other than PDF?
A: Yes, GroupDocs.Watermark supports Word, Excel, PowerPoint, images, and more.

Q: How do I change the watermark color or opacity?
A: Use watermark.setColor(Color.RED); and watermark.setOpacity(0.5); before adding it to artifacts.

Q: Is it possible to add a watermark to password‑protected PDFs?
A: Absolutely. Provide the password in PdfLoadOptions when creating the Watermarker.

Q: Does the library work on Linux/macOS as well as Windows?
A: The Java library is platform‑independent; it runs wherever a compatible JDK is installed.

Q: What if I need a dynamic watermark (e.g., user name, date)?
A: Build the watermark text string at runtime—e.g., new TextWatermark("Confidential – " + LocalDate.now(), ...).

Conclusion

You now have a complete, production‑ready method to watermark PDF Java files using GroupDocs.Watermark. By following the steps above, you can protect sensitive documents, reinforce branding, and comply with copyright requirements. Explore additional API features such as image watermarks, PDF metadata editing, and batch processing to further extend your solution.


Last Updated: 2026-01-23
Tested With: GroupDocs.Watermark 24.11 for Java
Author: GroupDocs

Resources