How to Apply Custom Watermarks to PDFs

Watermarking is a popular technique for protecting PDF documents and asserting ownership or status, such as marking them as "Confidential" or branding them with a logo. IronPDF offers a highly flexible solution by allowing you to create custom watermarks using HTML strings with full CSS support. This enables complete customization, allowing you to style watermarks with all the possibilities that HTML and CSS offer.

This guide will demonstrate different types of watermarks—text, image, watermark location, opacity, and rotation adjustments, as well as advanced methods using TextStamper and ImageStamper.

Apply Text Watermark Example

To apply a simple text watermark to a PDF document, use the applyWatermark method. This method allows you to input text using HTML and CSS for advanced styling. For example, let's use this method to add the text 'Confidential' in red color to the PDF.

import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>";

        // Apply the watermark using HTML string
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("text_watermark.pdf");
    }
}
import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>";

        // Apply the watermark using HTML string
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("text_watermark.pdf");
    }
}
JAVA

Output

The resulting PDF file, 'text_watermark.pdf,' will have the specified watermark applied to all its pages, with the text 'Confidential' displayed in red at the center of the document.


Image Watermark Example

Using the same method, you can apply images as watermarks, supporting various formats such as PNG, JPEG, SVG, and more. The image can be styled and positioned using CSS within the HTML string.

import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // Image HTML watermark
        String watermarkHtml = "<img src='logo.png' style='width:100px;'/>";

        // Apply the image watermark
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("image_watermark.pdf");
    }
}
import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // Image HTML watermark
        String watermarkHtml = "<img src='logo.png' style='width:100px;'/>";

        // Apply the image watermark
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("image_watermark.pdf");
    }
}
JAVA

Output

The resulting PDF file, 'image_watermark.pdf,' will have the specified image 'logo.png' applied as a watermark on all pages. The image will be displayed with a width of 100 pixels.


Watermark Opacity and Rotation Example

You can customize the watermark's appearance by adjusting its opacity and applying rotation. The applyWatermark method allows you to specify both properties as parameters.

import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:blue;'>Confidential</h1>";

        // Apply the HTML watermark with 30% opacity, positioned at the top-left corner of each page
        pdf.applyWatermark(watermarkHtml, 30, VerticalAlignment.TOP, HorizontalAlignment.LEFT);

        // Save the PDF to a new file
        pdf.saveAs("watermark_opacity_rotation.pdf");
    }
}
import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:blue;'>Confidential</h1>";

        // Apply the HTML watermark with 30% opacity, positioned at the top-left corner of each page
        pdf.applyWatermark(watermarkHtml, 30, VerticalAlignment.TOP, HorizontalAlignment.LEFT);

        // Save the PDF to a new file
        pdf.saveAs("watermark_opacity_rotation.pdf");
    }
}
JAVA

Output

The resulting PDF file, 'watermark_opacity_rotation.pdf,' will have the specified watermark applied to all its pages with 30% opacity. The watermark text, 'Confidential,' in blue will be aligned to the top-left corner of each page.

Applying the Watermark: The applyWatermark method applies the HTML-based watermark to all pages of the PDF with additional options:

  • Opacity: The watermark is applied with 30% opacity, making it partially transparent.
  • Vertical Alignment: The watermark is aligned to the TOP of the page. Other vertical alignment options include:
    • TOP: Watermark appears at the top of the page.
    • MIDDLE: Watermark appears in the middle of the page.
    • BOTTOM: Watermark appears at the bottom of the page.
  • Horizontal Alignment: The watermark is aligned to the LEFT of the page. Other horizontal alignment options include:
    • LEFT: Watermark appears on the left side of the page.
    • CENTER: Watermark appears at the center of the page.
    • RIGHT: Watermark appears on the right side of the page.

Frequently Asked Questions

What is a watermark in the context of PDF documents?

A watermark in a PDF document is a text or image added to the pages to indicate ownership, confidentiality, or branding, often used to protect the document and assert its status. IronPDF provides tools for adding such watermarks easily.

How can I apply a text watermark to a PDF in Java?

To apply a text watermark, you can use a method that supports HTML strings with CSS styling to input the desired text. IronPDF allows for advanced styling options using HTML and CSS.

Can I use images as watermarks?

Yes, you can apply images as watermarks. Using IronPDF, the library supports various image formats such as PNG, JPEG, and SVG, and you can style and position the images using CSS within the HTML string.

How do I adjust the opacity and rotation of a watermark?

Use a method that allows you to specify the desired opacity and rotation parameters. IronPDF enables you to adjust these settings to customize the watermark's appearance on the PDF pages.

What are the alignment options available for watermarks?

You can align watermarks vertically (TOP, MIDDLE, BOTTOM) and horizontally (LEFT, CENTER, RIGHT) on the PDF pages using IronPDF's method for applying watermarks.

What is required to apply watermarks in Java?

To apply watermarks, you need a Java library like IronPDF, set up a license key, and use the provided API methods to load a PDF and apply watermarks.

Is it possible to apply a watermark to an existing PDF file?

Yes, you can load an existing PDF file and apply a watermark to it by following the steps outlined in the documentation of libraries like IronPDF.

Can I customize the appearance of a watermark using HTML and CSS?

Yes, you can fully customize the appearance of a watermark using HTML strings with CSS support, enabling advanced styling options through IronPDF.

How do I save a PDF after applying a watermark?

After applying a watermark, you can save the modified PDF document using a method that specifies the desired file path and name, such as IronPDF's 'saveAs' method.

Does the library support advanced watermarking options like TextStamper and ImageStamper?

Yes, IronPDF supports advanced watermarking options using TextStamper and ImageStamper for more complex watermarking needs.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity and problem-solving.

At Iron Software, Darrius enjoys creating new things and simplifying complex concepts to make them more understandable. As one of our resident developers, he has also volunteered to teach students, sharing his expertise with the next generation.

For Darrius, his work is fulfilling because it is valued and has a real impact.

Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?