TL;DR
Watermarking a PDF allows you to add branding, ownership information, or document status indicators onto your pages. While libraries like Apache PDFBox and iText can handle watermarking, they often require more manual handling and may struggle with complex PDFs or licensing limitations. JPedal provides a high-performance pure Java solution for reliably adding image, text, shape, and annotation based watermarks to PDFs with minimal code, making it especially suitable for enterprise workflows and batch processing.
What is a PDF watermark?
A watermark in a PDF file is a visual element placed behind or over the main content of the page. They are typically faint and translucent. The primary purpose of adding a watermark to a PDF is to convey document status (like “Confidential”) or company branding.
Why Add a Watermark to a PDF Using Java?
If you are building an automated document pipeline, you may want to watermark a PDF using Java to protect your intellectual property. You can programmatically stamp your name or logo so that you can be identified as the owner, or automatically mark unfinished documents with a “Draft” image before they are distributed.
Which Java PDF library should you choose?
When looking to add a watermark to a PDF in Java, there are several options to choose from, each with tradeoffs to consider:
- Apache PDFBox and iText can both add watermarks to PDFs, but they often require low-level handling of content streams and rendering edge cases to work reliably across complex documents. PDFBox is a free Java API, but can struggle with performance and inconsistent rendering. iText provides a more powerful API, but introduces AGPL/commercial licensing constraints
- JPedal is often preferred for enterprise Java PDF watermarking because its high-fidelity and high-performance engine handles complex PDFs with ease and consistency. It is the perfect tool for batch-processing large volumes of documents.
How to watermark a PDF in Java using JPedal
First, download the JPedal jar and then add it to your project.
To make edits to PDF files you can use JPedal’s PDFManipulator class. You can learn more about this powerful tool here. To get started we will create the basic structure for editing PDF files:
final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
// insert operations here…
pdf.apply();
pdf.reset();
pdf.writeDocument(new File("outputFile.pdf"));
pdf.closeDocument();
Now we can add different operations depending on what kind of watermarks we want to add.
Add a Watermark to All PDF Pages
You should rebuild the list of pages each time when loading a document, otherwise a document with more pages than the previous one will not have the watermark applied to the additional pages.
final PageRanges pages = new PageRanges(1, pdf.getPageCount());
Add an Image Watermark to a PDF
To add an image watermark to a PDF:
final BufferedImage image = JDeli.read(new File("watermark.png"));
final float[] rect = new float[] {0, 0, 100, 100}; // X1, Y1, X2, Y2
addImage(pages, image, rect)
Images may be transparent or in different colour spaces.
Add a Text Watermark to a PDF
To add a text watermark to a PDF:
final float x = 10;
final float y = 10;
final int fontSize = 12;
final float[] color = {1, 0.3f, 0.2f, 1.0f}; // RGBA
pdf.addText(pages, "Hello World", x, y, BaseFont.HelveticaBold, fontSize, color[0], color[1], color[2], color[3]);
You can also draw text at an angle.
Add a Shape Watermark
To draw a shape onto your PDF:
final Shape shape = new Rectangle2D.Float(56.7f, 596.64f, 131.53f, 139.25f);
final DrawParameters params = new DrawParameters();
params.setStrokeColor(new float[] {1, 0, 0});
params.setFillRule(DrawParameters.STROKE);
pdf.addShape(pages, shape, params);
Using Annotations with Watermarks
Annotations by themselves are not suitable for watermarks as users can easily remove them. However, you could use an annotation to create a clickable hyperlink over your PDF watermarks.
pdf.addAnnotation(pages, new Link(
rect,
Annotation.getFlagsValue(false, false, true, false, false, false, true, true, false, true),
new float[3], // annotation color
1.0f, // stroking opacity
1.0f, // fill opacity
"https://www.idrsolutions.com/"
));
Learn more
Looking for a pure Java PDF library to handle processing your documents? Check out JPedal.
Want to learn more about the PDF file format? We have been developing PDF software for over 20 years!