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
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");
}
}
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");
}
}
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");
}
}
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
How can I add a custom watermark to a PDF in Java?
To add a custom watermark to a PDF in Java, you can use IronPDF to utilize HTML strings and CSS for creating and styling your watermark. IronPDF allows for inserting text or image watermarks with customizable options.
What are the types of watermarks that can be applied using IronPDF?
With IronPDF, you can apply both text and image watermarks to PDFs. Text watermarks can be styled with HTML and CSS, while image watermarks support formats like PNG and JPEG.
How do I adjust the position of a watermark on a PDF page?
You can adjust the position of a watermark using IronPDF by setting parameters for vertical and horizontal alignment. Options include TOP, MIDDLE, BOTTOM for vertical, and LEFT, CENTER, RIGHT for horizontal alignment.
Can I change the opacity and rotation of a watermark in a PDF?
Yes, IronPDF allows you to customize the opacity and rotation of watermarks. This enables you to adjust the transparency and orientation to fit your document's design requirements.
Is it possible to apply watermarks to specific pages of a PDF?
Yes, IronPDF provides the functionality to apply watermarks to specific pages. You can specify the page numbers when using the watermark application method.
How do I add a text watermark with specific font and color to a PDF?
To add a text watermark with a specific font and color, you can use HTML and CSS styling within IronPDF. Define the desired font and color in your HTML string, then apply it as a watermark.
Can I use CSS to style my watermarks in PDFs?
Yes, IronPDF supports CSS for styling watermarks, allowing you to customize the appearance of both text and image watermarks extensively.
What is the method to save a PDF after applying watermarks using IronPDF?
After applying watermarks to a PDF using IronPDF, you can save the modified document using the saveAs
method, specifying the file path and name.
How do I apply an image watermark to a PDF document?
To apply an image watermark, load your image in a supported format like PNG or JPEG, and use IronPDF to position and style it on the PDF pages using CSS.
What are the steps to get started with adding watermarks using IronPDF in Java?
To get started, download the IronPDF library, set up your Java environment, and use the library's API methods to render or load PDFs and apply custom watermarks with your chosen settings.