Crionics LOGO

TUTORIALS

 

 Template: Accessing and replacing images

As easy as 1, 2, 3...

 

Working with images

In this tutorial we will see how we can dynamically replace an image in a PDF document. When jPDF performs this action, the graphic is resized to fit the exact same boundaries as the original.

Please note that adding a bigger image can be achieved by using jPDF Creator which knows how to append new content onto an existing document.

Let's see how to instantiate an image using Crionics API.

Java has several different API to instantiate an image file. If you run a search on Google you will certainly find various source codes talking about, for example, the AWT way, the Swing way, the ImageIO way, or the fact that you must use the MediaTracker, etc., ...

At Crionics we like to make things simple and easy. So we came out with our own AbstractImage implementation which hides all the complexity under a nice clean interface.

The class comes with some handy methods such as:

flip() Flips the image along the X and/or Y axis.
getWidth(), getHeight() Recovers the dimension of the image.
scale(int,int,int) Scales the image to different dimensions using the specified scaling algorithm (quality dependent).

This class is in fact inherited by two main objects:

ImageGeneric Container for a generic type of image (gif, png, awt, ...)
ImageJPEG Container for a jpeg image.

While the first one does not add any new features apart from those inherited by the AbstractImage Class, the second one adds some specific features related to JPEG types of images:

changeResolution() Changes the resolution of the image.
getXResolution(), getYResolution() Recovers the resolutions (for example in DPI) of the image along any axis (X and Y)
SetQuality() Allows the changing of the quality factor used by the underlying compression engine.

Instantiating an AbstractImage is then accomplished through the use of a singleton - see the code below:

AbstractImage img = ImageConvertor.getInstance().loadImage("myFile.jpg");

 

Identifying images

Now that we know how to instantiate an image, we would first like to open a document, next get the list of images it contains, and then replace one of them.

Images in a PDF document have internal names. Unfortunately, Acrobat Reader/Writer does not provide an easy way to obtain them. Images are usually manipulated visually, and their names are hidden for ease of use.

Fortunatly, jPDF Viewer has the tool you need. Just launch Crionics jPDF Viewer and pick the menu item: Document / Document Processing / Show Images
The window below displays the images present in a document along with their internal names.

You could also retrieve this list using the following code:

// Open the template document:
PdfDocument doc = new PdfDocument("documentWithImages.pdf");

// Get the list of images:
List imgList = doc.getTemplateModule().getGraphicList();

for (int idx = 0; idx < imgList.size(); idx++)
{
PdfGraphic graphic = (PdfGraphic) imgList.get(idx);

String imagename=graphic.getName();
int width=graphic.getWidth();
int height=graphic.getHeight();

System.out.println("Image " + imagename + " is " + width + " by " + height + " pixels");
}

doc.close();

 

Replacing images

Once you have the PdfGraphic instance, you can replace the associated image by using the setImage() method - as in the example below.

// Open the template document:
PdfDocument doc = new PdfDocument("documentWithImages.pdf");

// Load the image and flip it around the Y axis:
AbstractImage img = ImageConvertor.getInstance().loadImage("myimage.jpg");
img.flip(AbstractImage.FLIP_Y);

// Now update the PDF Document:
doc.getTemplateModule().getGraphic("Im18_35 0").setImage(img);

// Last step ... save and close:
doc.saveAs("modified.pdf");
doc.close();


© 2000-2007 Crionics Inc. Report issues with this page