IRONSOFTWAREHOME
USING IRONPDF FOR JAVA

How to Watermark PDF Files in Java

Curtis Chau
Curtis Chau
Updated: August 1, 2026

Watermarking is a common technique used to protect the authenticity and ownership of digital documents. This article will explore how to add watermarks to PDF files using IronPDF, a powerful library for Java. With IronPDF, you can easily incorporate watermarks into your PDF documents programmatically, ensuring their security and branding. Let's dig deep into the step-by-step process of Java watermarking using IronPDF.

IronPDF - Java PDF Library

IronPDF Java Edition is a library for working with PDFs in Java. It offers fast and accurate operations, making it an excellent choice for many PDF file-related tasks like extracting text from PDFs, extracting images from PDFs, merging PDF files, and splitting PDFs. It is built on the capabilities of IronPDF for .NET, ensuring reliable functionality.

With the IronPDF library, you can convert HTML, URL, and strings into PDF documents using popular open standard document types such as HTML, CSS, JS, JPG, and PNG. The library generates PDFs with HTML to PDF conversion with precise pixel-perfect rendering and utilizes the latest technology.

Prerequisites

Before getting started, make sure you have the following prerequisites in place:

  1. Java Development Kit (JDK) installed on your machine.
  2. A Java IDE (Integrated Development Environment) such as Eclipse, NetBeans, or IntelliJ IDEA.
  3. IronPDF library added as a dependency in your Java project. You can include it by referencing the appropriate Maven artifact or by manually importing the JAR file. You can download it from the IronPDF home page and include it in your project.

Setting up the Project

  1. Create a new Java project in your chosen IDE and include the IronPDF library as a dependency. You can do this by either adding the Maven artifact to your project's pom.xml file or by importing the JAR file manually.

  2. Add the IronPDF library to your project using the dependency manager.

    # Install IronPDF through Maven or another package manager
    SHELL
  3. Add the following necessary imports to your Java source file(s):

    import com.ironsoftware.ironpdf.PdfDocument;
    import com.ironsoftware.ironpdf.stamp.*;
    
    import java.io.IOException;
    import java.nio.file.Paths;
    Java

Loading the PDF Document

To begin, load the existing PDF document on which you want to add the watermark, or create a new PDF file using the [renderHtmlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String) method. IronPDF provides convenient methods to open and manipulate PDF files. The following code example will load the example.pdf file as a PdfDocument object:

// Load an existing PDF document
PdfDocument pdf = PdfDocument.fromFile(Paths.get("example.pdf"));
Java

Adding Watermark to PDF File

IronPDF allows you to add various types of watermarks to your PDF documents, including text watermarks and image watermarks. Let's explore both options:

Add Text Watermark

To add a text confidential watermark, use the [applyWatermark](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#applyWatermark(java.lang.String) method. You can customize the text, font, color, and size, using an HTML string as the first parameter, set its opacity, and align the watermark vertically and horizontally. Then save it using the [saveAs](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#saveAs(java.lang.String) method. Here's an example:

// Apply a text watermark to the PDF
pdf.applyWatermark("<h1 style=\"color:red\">Confidential</h1>", 50, VerticalAlignment.MIDDLE, HorizontalAlignment.CENTER);
// Save the modified PDF document with the applied watermark
pdf.saveAs("textwatermarked.pdf");
Java

The text-watermarked PDF document will look like this:

How to Watermark PDF Files in Java, Figure 1: The watermarked PDF file The watermarked PDF file

Add Image Watermark

To add an image watermark, use the same applyWatermark method. Now, use the HTML string to set the img tag with the source image. Specify the path to the image file and adjust its position and opacity as needed. Here's an example:

// Apply an image watermark to the PDF
pdf.applyWatermark("<img src='assets/images/iron-pdf-logo.jpg'>", 50, VerticalAlignment.MIDDLE, HorizontalAlignment.CENTER);
// Save the modified PDF document with the applied watermark
pdf.saveAs("imagewatermarked.pdf");
Java

The image-watermarked PDF document looks like this:

How to Watermark PDF Files in Java, Figure 2: The watermarked PDF file The watermarked PDF file

Make Watermarked PDF using Stamper Class

IronPDF for Java provides a Stamper class, which can be used to add text and image watermarks using HtmlStamper, ImageStamper, and TextStamper classes. The Stamper class provides more flexibility over the applyWatermark method.

Text Watermark to PDF

Here this section will use the TextStamper class to apply the watermark to PDF. You can set the rotation angle of the watermark text or image watermark to a PDF document. It also allows you to set horizontal and vertical offsets along with stamping behind the PDF page content.

Here is the code to add a text watermark to an existing PDF document:

// Create a text stamper for watermarking
TextStamper stamper = new TextStamper("Confidential");
stamper.setFontColor("#FF0000");
stamper.setFontSize(60);
stamper.setFontFamily("Times New Roman");
stamper.setHorizontalAlignment(HorizontalAlignment.CENTER);
stamper.setVerticalAlignment(VerticalAlignment.MIDDLE);
stamper.setOpacity(30);
stamper.setRotation(45);
// Apply the stamper to the PDF
pdf.applyStamp(stamper);
// Save the modified PDF document with the watermark
pdf.saveAs("textwatermarked2.pdf");
Java

In the above code, the IronPDF library's TextStamper class is used to create a text watermark with the content "Confidential" and applies it to a PDF document. The watermark is customized with specific font properties, alignment, opacity, and rotation. Finally, the modified PDF document is saved as a new file with the watermark applied.

How to Watermark PDF Files in Java, Figure 3: The watermarked PDF file The watermarked PDF file

Image Watermark to PDF

Here this section is going to use the ImageStamper class to apply a background image watermark to a PDF document. The Java code is as follows:

// Create an image stamper for watermarking
ImageStamper stamper = new ImageStamper("assets/images/iron-pdf-logo.jpg");
stamper.setHorizontalAlignment(HorizontalAlignment.CENTER);
stamper.setVerticalAlignment(VerticalAlignment.MIDDLE);
stamper.setOpacity(30);
stamper.setStampBehindContent(true);
stamper.setRotation(45);
// Apply the stamper to the PDF
pdf.applyStamp(stamper);
// Save the modified PDF document with the watermark
pdf.saveAs("imagewatermarked.pdf");
Java

In the above complete code, the IronPDF library's ImageStamper class is used to create an image watermark and apply it to a PDF document. The watermark image is specified by its file path, and its properties such as alignment, opacity, stacking behind the content, and rotation are configured. Finally, the modified PDF document is saved as a new file with the watermark applied.

How to Watermark PDF Files in Java, Figure 4: The watermarked PDF file The watermarked PDF file

Conclusion

This article explored how to add watermarks to PDF documents using IronPDF. With IronPDF's intuitive APIs, you can easily incorporate text or image watermarks into your PDF files, enhancing their security and branding. Experiment with different customization options using the Stamper class to achieve the desired watermarking effects. Now, you can confidently protect and personalize your PDF documents in your Java applications.

For detailed guidance and examples on utilizing IronPDF for Java, you can refer to the code examples, which provide helpful resources and demonstrations.

IronPDF is free for development purposes and offers commercial licensing options for commercial use. To learn more about the licensing details, you can visit the IronPDF Licensing Guide.

You can also get a free API license in a free trial license for commercial use. To obtain the IronPDF software, you can download it from the official IronPDF for Java website.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Related Articles

Key in blue circle

Get your free 30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge related to IronPDF Product Demo

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required