Add Watermark to PDF Java Logo

Watermarks are used to protect PDF documents or claim their ownership. Also, in various cases, watermarks are added to display the status of a PDF document such as draft, manuscript, etc. In order to automate watermarking, this article covers how to add a text or image watermark to the PDF using Java.

Java API to Add Watermark to PDF

In order to add a watermark to PDF documents, we will use Aspose.PDF for Java. It is a feature-rich API to create, process, and convert PDF files from within the Java applications. You can either download the API’s JAR or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>21.6</version>
</dependency>

Add Text Watermark to PDF in Java

The following are the steps to add a text watermark to PDF using Java.

The following code sample shows how to add a text watermark to a PDF document.

// Load PDF document
Document doc = new Document("input.pdf");
// Create a formatted text
FormattedText formattedText = new FormattedText("Confidential Document", java.awt.Color.RED, FontStyle.Courier, EncodingType.Identity_h, true, 40.0F);
// Create watermark artifact and set its properties
WatermarkArtifact artifact = new WatermarkArtifact();
artifact.setText(formattedText);
artifact.setArtifactHorizontalAlignment (HorizontalAlignment.Center);
artifact.setArtifactVerticalAlignment (VerticalAlignment.Center);
artifact.setRotation (25);
artifact.setOpacity (0.5);
artifact.setBackground (false);
// Add watermark to the first page of PDF
doc.getPages().get_Item(1).getArtifacts().add(artifact);
// Save watermarked PDF document
doc.save("watermark.pdf");

Output

The following is the screenshot of the watermarked PDF.

adding watermark to pdf in java

Add Image Watermark to PDF in Java

The following are the steps to add an image watermark to PDF using Java.

The following code sample shows how to add an image watermark to a PDF document.

// Load PDF document
Document doc = new Document("input.pdf");
// Create a background artifact
BackgroundArtifact background = new BackgroundArtifact();
// Specify the image for background artifact object
background.setBackgroundImage(new FileInputStream("logo.png"));
background.setOpacity(0.5);
background.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
background.setArtifactVerticalAlignment(VerticalAlignment.Center);
// Add watermark to the first page of PDF
doc.getPages().get_Item(1).getArtifacts().add(background);
// Save watermarked PDF document
doc.save("watermark.pdf");

Output

The following is the screenshot of the watermarked PDF.

Image watermark in PDF using Java

Get a Free API License

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

Conclusion

In this article, you have learned how to add a text or image watermark to a PDF using Java. Also, you have seen how to customize the appearance of the watermark using different properties. You can learn more about the Java PDF API using the documentation. In case you would have any questions, let us know via our forum.

See Also