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 FreeThroughout 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:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
Once the installation is complete, we are ready to call the function with the following code:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditPdfApi apiInstance = new EditPdfApi();
String watermarkText = "watermarkText_example"; // String | Watermark text to add to the PDF (required)
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String fontName = "fontName_example"; // String | Font Family Name for the watermark text; default is Times New Roman
BigDecimal fontSize = new BigDecimal(); // BigDecimal | Font Size in points of the text; default is 150
String fontColor = "fontColor_example"; // String | Font color in hexadecimal or HTML color name; default is Red
BigDecimal fontTransparency = new BigDecimal(); // BigDecimal | Font transparency between 0.0 (completely transparent) to 1.0 (fully opaque); default is 0.5
try {
byte[] result = apiInstance.editPdfWatermarkText(watermarkText, inputFile, fontName, fontSize, fontColor, fontTransparency);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfWatermarkText");
e.printStackTrace();
}
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.
Comments