Sample of the Week:
Replacing images in a PDF file using the Datalogics PDF Java Toolkit isn’t exactly as easy as replacing a light bulb… but it’s close.
What You Need to Know First:
I’ve kept things simple in this example. There’s only one image in the single page file so we don’t need to go through the effort of identifying it as the one we want to replace; we can just replace it.
An image of the input file is on the left, the output file on the right. You can see that I just zoomed in a bit on the same photo and cropped the image a bit. Because I started with the same image I know that the original and the replacement images are in the same color space, have the same dimensions, and bits per pixel. Keeping these details the same means that all I really need to do to replace the image is replace the stream that represents the image data rather than recreate or update the other dictionary values in the XObject that gets painted by the viewer.
Replacing Images in PDF Files using the Datalogics PDF Java Toolkit
The first thing we want to do is convert the replacement JPG to a PDFXObjectImage; something we can use in a PDF.
Then we need to get the XObject that represents the original image. As I mentioned, there’s only one image so we don’t need to iterate over all of the XObjects, we just need to grab the first one.
After that, we just replace the stream data of the original with the stream data of the replacement image by accessing it via the XObjectImage that we created earlier.
I told you it was easy. In fact, replacing an image once you’ve found it in the PDF will always be pretty easy. However, finding the right image to replace… well… that’s the fun part. I’ll leave that for another article though.
To get started working with PDF, download this Gist and request an evaluation copy of The Datalogics PDF Java Toolkit.