DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Split PDF Files into Separate Documents Using Java
  • How to Get Plain Text From Common Documents in Java
  • How to Change PDF Paper Sizes With an API in Java
  • How To Convert Common Documents to PNG Image Arrays in Java

Trending

  • Is Big Data Dying?
  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  1. DZone
  2. Coding
  3. Java
  4. How to Add a Watermark to a PDF Document in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Apr. 09, 21 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.3K Views

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:

Java
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Then add a reference to the dependency:

Java
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.90</version>
6
</dependency>
7
</dependencies>



Once the installation is complete, we are ready to call the function with the following code:

Java
 




xxxxxxxxxx
1
30


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.EditPdfApi;
7
 
          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
 
          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
 
          
16
EditPdfApi apiInstance = new EditPdfApi();
17
String watermarkText = "watermarkText_example"; // String | Watermark text to add to the PDF (required)
18
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
19
String fontName = "fontName_example"; // String | Font Family Name for the watermark text; default is Times New Roman
20
BigDecimal fontSize = new BigDecimal(); // BigDecimal | Font Size in points of the text; default is 150
21
String fontColor = "fontColor_example"; // String | Font color in hexadecimal or HTML color name; default is Red
22
BigDecimal fontTransparency = new BigDecimal(); // BigDecimal | Font transparency between 0.0 (completely transparent) to 1.0 (fully opaque); default is 0.5
23
try {
24
    byte[] result = apiInstance.editPdfWatermarkText(watermarkText, inputFile, fontName, fontSize, fontColor, fontTransparency);
25
    System.out.println(result);
26
} catch (ApiException e) {
27
    System.err.println("Exception when calling EditPdfApi#editPdfWatermarkText");
28
    e.printStackTrace();
29
}



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.

PDF Document Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Split PDF Files into Separate Documents Using Java
  • How to Get Plain Text From Common Documents in Java
  • How to Change PDF Paper Sizes With an API in Java
  • How To Convert Common Documents to PNG Image Arrays in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!