Apply Watermark to PDF in Java

Whether you want to apply branding to your documents or you want to protect the files from any illegal use, the watermark does the job for you. In this article, you will learn to programmatically add the watermarks to your PDF files using Java.

The following topics are covered below:

Watermarking API for Java

GroupDocs.Watermark for Java is a watermarking API that allows working with text and image watermarks within the PDF files. Along with the PDF files, the API allows adding, removing, and extraction of watermarks for word-processing documents, spreadsheets, presentations, email messages, images, Visio drawings, and many other formats. From the documentation, you may further check the features and supported file formats.

Download and Configure

Get the PDF watermarking library from the downloads section. For Maven-based Java applications, add the following configuration within pom.xml. Later, you can try the examples of this article as well as many more from GitHub. For the details, you may also visit the API Reference.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-watermark</artifactId>
        <version>21.3</version> 
</dependency>

Add Text Watermark to PDF using Java

The text watermark can be applied to PDF files by adding the formatted text on either all the pages or any selective page on the set location.

The following steps show how to add text to PDF files as watermark.

  • Load the PDF document using Watermarker class.
  • Initialize the text watermark using TextWatermark class.
  • Set the appearance by changing rotation angle, x-y positions, opacity, foreground and background colors, etc.
  • Set the targetted page index (Optional). If you do not set the index, the watermark will be applied to all pages by default.
  • Add the text watermark to Watermarker.
  • Save the watermarked file using the appropriate save method.

The source code shows how to add text watermark to PDF files in Java.

The output of the above source code shows the text watermark on both the pages of the given PDF file.

Text Watermark to PDF

Add Image Watermark to PDF using Java

Similarly, you can add images to any PDF file at any location just like the text watermark options.

The following steps show how to add image to PDF files as watermark.

  • Load the PDF document using Watermarker class.
  • Initialize the image watermark using ImageWatermark class.
  • Set the appearance by adjusting the rotation angle, x-y positions, opacity, and other options.
  • Set the targetted page index. (Optional)
  • Add the image watermark to Watermarker.
  • Save the watermarked file using the appropriate save method.

The source code shows how to add image watermark to PDF files using Java.

The output of the above source code shows the image watermark on the second page of the given PDF file.

Image Watermark to PDF

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, you learned to apply watermarks to PDF files using Java. We discussed adding text as well as images on PDF files as watermarks. For more details or learning about the API, visit documentation. For queries, contact us via the forum.

See Also