In order to add page stamp into PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for 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 Page Stamp to PDF Document Java
You need Aspose.PDF for Java to try the code in your environment.
- Load the PDF with an instance of Document.
- Open a PDF document using Document object.
- Create a Page Stamp and define its properties.
- Add the Stamp to Page using AddStamp method
Add Page Stamp to PDF - Java
String inputFileName = "AddPageStamp.pdf";
String pageStampFileName = "PageStamp.pdf";
String outputFileName = "AddPageStamp_out.pdf";
Document pdfDocument = new Document(DATA_DIR.resolve(inputFileName).toString());
PdfPageStamp bluePageStamp = new PdfPageStamp(DATA_DIR.resolve(pageStampFileName).toString(), 1);
bluePageStamp.setHeight(800);
bluePageStamp.setBackground(true);
PdfPageStamp plumPageStamp = new PdfPageStamp(DATA_DIR.resolve(pageStampFileName).toString(), 2);
plumPageStamp.setHeight(800);
plumPageStamp.setBackground(true);
for (int i = 1; i < 5; i++) {
if (i % 2 == 0)
pdfDocument.getPages().get_Item(i).addStamp(bluePageStamp);
else
pdfDocument.getPages().get_Item(i).addStamp(plumPageStamp);
}
pdfDocument.save(DATA_DIR.resolve(outputFileName).toString());
pdfDocument.close();