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.
How to Apply Watermarks in Java
- Download the Java library to apply watermarks to PDFs
- Render a new PDF or load an existing one
- Configure the HTML string or image to be used as a watermark
- Apply the watermark using the appropriate method
- Adjust parameters for opacity, rotation, and location as needed
Start using IronPDF in your project today with a free trial.
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 {
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// HTML string for watermark
String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>";
// Apply the watermark
pdf.applyWatermark(watermarkHtml);
// Save the PDF
pdf.saveAs("text_watermark.pdf");
}
}
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 {
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
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
pdf.saveAs("image_watermark.pdf");
}
}
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 {
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
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
pdf.saveAs("watermark_opacity_rotation.pdf");
}
}
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.