Add Watermark to PDF File, we’ll use Aspose.PDF for Java API, which is a feature-rich, powerful, and easy-to-use conversion API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
Add Watermark via Java
You need Aspose.PDF for Java to try the code in your environment.
- Load the PDF with an instance of Document.
- Create an instance of WatermarkArtifact.
- Set properties of WatermarkArtifact object.
- Add watermark using method Add of Aspose.Pdf.Page.Artifacts collection class.
- Save PDF file
Add Watermark to PDF - Java
Document pdfDocument = new Document(DATA_DIR.resolve("sample.pdf").toString());
com.aspose.pdf.WatermarkArtifact artifact = new com.aspose.pdf.WatermarkArtifact();
artifact.setImage(DATA_DIR.resolve("watermark.jpg").toString());
artifact.setArtifactHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
artifact.setArtifactVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);
artifact.setRotation(15);
artifact.setOpacity(1);
artifact.setBackground(true);
pdfDocument.getPages().get_Item(1).getArtifacts().add(artifact);
//save result pdf to file
pdfDocument.save("add_watermark.pdf", com.aspose.pdf.SaveFormat.Pdf);
pdfDocument.close();