Stamp Class
Contents
[
Hide
]
The Java StampExamples class demonstrates the main stamp-building workflows available through the Facades API.
Add an image stamp
Use this workflow when an image file should be placed on the PDF as a stamp.
Steps
- Create a
PdfFileStampinstance and bind the source PDF. - Create a
Stampobject and bind it to the image file. - Set the stamp identifier and placement origin.
- Add the stamp to the document.
- Save the result and close the facade object.
Java example
public static void addImageStamp(Path inputFile, Path imageFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
Stamp stamp = new Stamp();
stamp.bindImage(imageFile.toString());
stamp.setStampId(1);
stamp.setOrigin(36, 520);
pdfStamper.addStamp(stamp);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
Add a PDF page as a stamp
Use this workflow when content from another PDF page should be reused as stamp content.
Steps
- Create a
PdfFileStampinstance and bind the target PDF. - Create a
Stampobject. - Bind the stamp to a specific page from another PDF file.
- Set the target page number and origin for placement.
- Add the stamp, save the output, and close the facade object.
Java example
public static void addPdfPageAsStamp(Path inputFile, Path stampPdf, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
Stamp stamp = new Stamp();
stamp.bindPdf(stampPdf.toString(), 1);
stamp.setPageNumber(1);
stamp.setOrigin(36, 250);
pdfStamper.addStamp(stamp);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
Add a text stamp with TextState
Use this workflow when the stamp should contain styled text rather than an image.
Steps
- Create a
PdfFileStampinstance and bind the source PDF. - Create a
Stampobject. - Bind a
FormattedTextlogo and a customTextStateto the stamp. - Set the stamp origin and rotation.
- Add the stamp, save the output, and close the facade object.
Java example
public static void addTextStampWithTextState(Path inputFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
Stamp stamp = new Stamp();
stamp.bindLogo(createTextLogo("Approved by signing workflow"));
stamp.bindTextState(createTextState());
stamp.setOrigin(36, 700);
stamp.setRotation(15.0f);
pdfStamper.addStamp(stamp);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
Add a stamp to specific pages
Use this workflow when the stamp should appear only on selected pages instead of the whole document.
Steps
- Create a
PdfFileStampinstance and bind the source PDF. - Create a
Stampobject and bind it to an image file. - Set the target page list, origin, and image size.
- Add the stamp to the document.
- Save the result and close the facade object.
Java example
public static void addStampToSpecificPages(Path inputFile, Path imageFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
Stamp stamp = new Stamp();
stamp.bindImage(imageFile.toString());
stamp.setPages(new int[] {1});
stamp.setOrigin(400, 40);
stamp.setImageSize(120, 60);
pdfStamper.addStamp(stamp);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}
Add a background image stamp
Use this workflow when the stamp should appear behind page content with controlled opacity and rotation.
Steps
- Create a
PdfFileStampinstance and bind the source PDF. - Create a
Stampobject and bind it to the image file. - Mark the stamp as background content.
- Configure opacity, quality, rotation, size, and origin.
- Add the stamp, save the output, and close the facade object.
Java example
public static void addBackgroundImageStamp(Path inputFile, Path imageFile, Path outputFile) {
PdfFileStamp pdfStamper = new PdfFileStamp();
try {
pdfStamper.bindPdf(inputFile.toString());
Stamp stamp = new Stamp();
stamp.bindImage(imageFile.toString());
stamp.setBackground(true);
stamp.setOpacity(0.35f);
stamp.setQuality(90);
stamp.setRotation(45.0f);
stamp.setImageSize(160, 80);
stamp.setOrigin(200, 300);
pdfStamper.addStamp(stamp);
pdfStamper.save(outputFile.toString());
} finally {
pdfStamper.close();
}
}