How to Add a Text Watermark to a PDF using Java
Watermarks are transparent labels placed on top of various documents for legal purposes. The idea started originally to prevent criminals from counterfeiting financial documents, but has since expanded into the world of photography and content creation (when you look through Google Images, you’re likely to find dozens of watermarked photos). A document of any kind with a watermark sends a clear message to the viewer: this content cannot be copied or used in any way without the permission of the copyright owner.
Adding a watermark to any PDF containing sensitive copyright information is a smart move, and with the help of our PDF Watermark API, you can include that operation as a cloud service into your application with ease. The below instructions are intended to help you structure your API call with Java, and provide insight into the API’s required & optional parameters.
To begin installing Maven, first include the below reference in the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, include the next reference in the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now include the import classes:
// 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;
Lastly, call the API. You’re required to include your PDF file and Watermark Text, and you may then specify the font name, size, color, and transparency desired by customizing the parameters below:
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();
}
And that’s it — give it a test to see how it turns out. This API is perfect for use in conjunction with our suite of PDF editing APIs.