Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Add an Image to a PDF Page as a Stamp Annotation
* Copyright Datalogics, Inc. 2015
package pdfjt.cookbook.imaging;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationStamp;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAppearance;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import pdfjt.util.SampleFileServices;
* This sample reproduces the Acrobat (Windows-only) behavior where an image on
* the clip board can be pasted on a PDF page resulting in a Stamp Annotation.
* In this case, the image is read from a file.
public class StampedImage {
private static final String inputPDF = "";
private static final String inputImage = "";
private static final String outputDir = "cookbook/Imaging/output/";
static public void main(String[] args) throws Exception {
try {
// First read in the PDF file and get the first page so we can add a
// Stamp to it.
InputStream fis = new URL(inputPDF).openStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
PDFPage pdfPage = pdfDocument.requirePages().getPage(0);
// Read in the image file as a BufferedImage.
ImageReader reader = null;
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByFormatName("PNG");
if (imageReaders.hasNext()) {
reader =;
} else {
System.out.println("A PNG Reader isn't available");
reader.setInput(ImageIO.createImageInputStream(new URL(inputImage).openStream()));
BufferedImage bufferedImage =;
* Use the ImageManager class to convert the BufferedImage into a
* PDF XObject that can be used as the appearance of the Stamp.
* Unlike Buttons, Stamps only have one appearance state so we only
* need to set the "Normal" or default appearance.
PDFXObjectForm pdfXObjectForm = ImageManager.getXObjPDFImage(pdfDocument, bufferedImage, new XObjectUseOptions());
// Create the Stamp Annotation Object.
PDFAnnotationStamp pdfAnnotationStamp = PDFAnnotationStamp.newInstance(pdfDocument);
PDFAppearance pdfAppearance = PDFAppearance.newInstance(pdfDocument);
* Add a Title and Content for the Stamp Pop-Up. These strings will
* appear in the Acrobat Comments panel and when you roll over the
* Stamp.
pdfAnnotationStamp.setContents("Pike at the park");
// Set the location of the Stamp to be centered on the page.
double stampWidth = pdfXObjectForm.getBBox().width();
double stampHeight = pdfXObjectForm.getBBox().height();
double llx = (pdfPage.getCropBox().width() - stampWidth);
double lly = (pdfPage.getCropBox().height() - stampHeight);
pdfAnnotationStamp.setRect(llx, lly, stampWidth, stampHeight);
// Add the Stamp Annotation to the page
// Save the file.
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + "StampedImageOutput.pdf");, PDFSaveFullOptions.newInstance());
} finally {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment