|
||||||||||||||||
TUTORIALSTemplate: Accessing and replacing imagesAs easy as 1, 2, 3...
|
||||||||||||||||
| 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:
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();
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. |