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.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
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.graphics.xobject.PDFXObjectForm;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationStamp;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAppearance;
import com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.services.imageconversion.ImageManager;
import com.adobe.pdfjt.services.xobjhandler.XObjectUseOptions;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
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 = "http://dev.datalogics.com/cookbook/imaging/StampedImageInput.pdf";
private static final String inputImage = "http://dev.datalogics.com/cookbook/imaging/PikePark.png";
private static final String outputDir = "cookbook/Imaging/output/";
static public void main(String[] args) throws Exception {
SampleFileServices.createDir(outputDir);
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 = imageReaders.next();
} else {
System.out.println("A PNG Reader isn't available");
System.exit(0);
}
reader.setInput(ImageIO.createImageInputStream(new URL(inputImage).openStream()));
BufferedImage bufferedImage = reader.read(0);
/*
* 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);
pdfAppearance.setNormalAppearance(pdfXObjectForm);
pdfAnnotationStamp.setAppearance(pdfAppearance);
/*
* 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.setTitle("Datalogics");
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
pdfPage.addAnnotation(pdfAnnotationStamp);
// Save the file.
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + "StampedImageOutput.pdf");
pdfDocument.save(outputFile, PDFSaveFullOptions.newInstance());
System.out.println("Done!");
} finally {
//
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment