Back to Basics: Stamping Images using the Datalogics PDF Java Toolkit

Back to Basics: Stamping Images using the Datalogics PDF Java Toolkit

Sample of the Week:

Back to Basics Month continues here at Sample of the Week. This week we’ll use the Datalogics PDF Java Toolkit to replicate the Acrobat (Windows-only) behavior where an image on the clipboard can be pasted onto a PDF page resulting in a Stamp Annotation.

Adding an image to PDF Page content is very easy when using the Datalogics PDF Java Toolkit but what we’re doing here is a little more complicated. Adding an image to the PDF page content results in a static image. However, the stamp feature in Acrobat allows you “stamp” an image or other PDF content onto a PDF page, then scale, position and rotate it, add a note to it or even copy it to another page. If there’s a specific image that you want to stamp, you can also use Acrobat to create a custom stamp from any PDF or image content and keep it in a permanent library for use at any time. But one of the lesser known ways of adding an image to a PDF page is to simply copy the image to the clipboard and then use the ctrl-v keyboard shortcut to paste the image onto the PDF page. The result is a stamp annotation of your image. You can think of it as a sort of “temporary” custom stamp. It behaves just like any other custom stamp, it just isn’t persistent across Acrobat sessions.

Now, obviously, with a server application, we aren’t going to want to use the clipboard to hold the image so in the Gist referenced below, we just read the image from a file.

After reading in both the PDF file and the image, we use the ImageManager class to convert the BufferedImage into a PDF XObject that can be used as the appearance of the Stamp.

We begin by creating a basic PDFAnnotationStamp object and then assign it’s properties. Unlike Buttons, Stamps only have one appearance state so we only need to set the “Normal”, default appearance.

Optionally, you can also add a Title and some Content for the Stamp Pop-Up. These strings will appear in the Acrobat Comments panel and when you roll over the Stamp.

After that it’s just a simple matter of positioning the stamp and adding it to the page.

After running the sample, you end up with a file that is functionally equivalent to what you would get out of Acrobat if you pasted an image onto the page.

The entire Gist is listed below.

Leave a Reply

Your email address will not be published. Required fields are marked *