Legacy notice!

iText 5 is the previous major version of iText’s leading PDF SDK. iText 5 is EOL, and is no longer developed, although we still provide support and security fixes. Switch your project to iText 8, our latest version which supports the latest PDF standards and technologies.
Check related iText 8 content!

Why does it work If I use the commented line without the transform, but with the AffineTransform, it does not show up?

I am using PdfStamper's getOverContent() method to add an image to a PDF file using an AffineTransform instance. When I open the resulting PDF, I can't see the image. This is my code:

PdfContentByte content = stamper.getOverContent(1);
content.addImage(data.image,desc.transform);
//content.addImage(data.image);
If I use the commented line without the transform it works perfectly: the image is added to the PDF, but with the AffineTransform, it does not show up. I intend to use a sophisticated transform, but I tried with the "Identity" transformation first...

Posted on StackOverflow on Dec 7, 2015 by Mário de Sá Vera

I have examined your problem and I am pretty sure that your image gets added. However: you can't see it because the dimension of the image is 1 user unit by 1 user unit.

I've made an example to show you how to fix this problem: AddImageAffineTransform

public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(
        reader, new FileOutputStream(dest));
    Image image = Image.getInstance(IMG);
    AffineTransform at = AffineTransform.getTranslateInstance(36, 300);
    at.concatenate(AffineTransform.getScaleInstance(
        image.getScaledWidth(), image.getScaledHeight()));
    PdfContentByte canvas = stamper.getOverContent(1);
    canvas.addImage(image, at);
    stamper.close();
    reader.close();
}

In this example, I start with a translation: 36 user units from the left border and 300 user units from the bottom. If I would add the image using this transformation, I'd add the image at those coordinates, but it would be too small to see with the naked eye.

To make sure the image is visible, I concatenate a scale transformation scaling the image to its width in X direction and to its height in Y direction.