How to Add a Watermark to a PDF Document in Java
Learn how to use an API to add a customizable text watermark to a PDF file.
Join the DZone community and get the full member experience.Join For Free
Throughout history, watermarks have been used to verify the authenticity and integrity of documents, currency, stamps, and more. They were originally developed for the paper-making process in thirteenth-century Italy to identify the manufacturer of the paper, and the practice spread rapidly across the rest of Europe. Fast forward several centuries to our current state and watermarks have bridged the gap between paper and digital mediums. Digital watermarks are used to verify authenticity and integrity as well, but in a different capacity than their paper counterparts. Some of the primary tasks they’re used for include copyright protection, source tracking, fraud protection, and online content management.
Since the PDF format is frequently used to share sensitive information, watermarks can be added to increase the security of the document and ensure that it isn’t employed in an incorrect or inappropriate manner. These markers can be used to denote draft documents, specify security levels, or indicate brand name/ownership. In the following tutorial, we will demonstrate how you can use an API in Java to instantly add a text watermark to your PDF documents; customizable features are available including font name, size, color, and transparency.
To kick off the process, we first need to install the Maven SDK by adding a reference to the repository in pom.xml:
Then add a reference to the dependency:
Once the installation is complete, we are ready to call the function with the following code:
There are a few required parameters you need to include for the operation to run successfully, as well as the optional custom parameters I mentioned earlier in the article:
- Watermark Text (required) - Watermark text to add to the PDF.
- Input File (required) – PDF file to perform the operation on.
- API Key (required) – your personal API key; this can be retrieved by registering for a free account on the Cloudmersive website.
- Font Name (optional) - font Family Name for the watermark text; default is Times New Roman.
- Font Size (optional) – font Size in points of the text; default is 150.
- Font Color (optional) – font color in hexadecimal or HTML color name; default is Red.
- Font Transparency (optional) - font transparency between 0.0 (completely transparent) to 1.0 (fully opaque); default is 0.5.
In no time at all, your updated PDF will be returned with the desired watermark text set behind the main text of your document.
Opinions expressed by DZone contributors are their own.