1

I am using PdfStamper getOverContent() so I can add an image to the output PDF file using an AffineTransform of Identity type.

    PdfContentByte content = stamper.getOverContent(1);
    data.image.setAbsolutePosition(desc.X,desc.Y);
    content.addImage(data.image,desc.transform);
    //content.addImage(data.image);

if I use the commented line without the transform it works perfectly adding the image to the PDF generated but with the AffineTransform (setToIdentity()) it does not show up.

could someone help me out with that ? I intend to use more sophisticated transform but the Identity should work first...


EDIT (copied from the invalid answer)

I removed the call to setAbsolutePosition and used a setToIdentity() as the only transformation and the image is not show... Then added the setToTranslation(X,Y) where X and Y are the same values used in the successful case where I do NOT give the transformation as the second parameter and still it does NOT show the image. Is there an example with the AffineTransform as a parameter to PdfContentByte addImage() call using an AffineTransform as a parameter ? I have bought your book but could not fins any.

mkl
  • 90,588
  • 15
  • 125
  • 265
Mário de Sá Vera
  • 380
  • 1
  • 4
  • 12
  • What does your `transform` look like? Maybe it rotates the image outside the visible area of the page. Also: AFAIK you can't combine `setAbsolutePosition()` (a convenience method so that you don't need to define a translation) with adding the document defining your own transformation. – Bruno Lowagie Dec 06 '15 at 12:57
  • I removed the call to setAbsolutePosition and used a setToIdentity() as the only transformation and the image is not shown... Then added the setToTranslation(X,Y) where X and Y are the same values used in the successful case where I do NOT give the transformation as the second parameter and still it does NOT show the image. Is there an example with the AffineTransform as a parameter to PdfContentByte addImage() call using an AffineTransform as a parameter ? I have bought your book but could not find any. – Mário de Sá Vera Dec 07 '15 at 20:57

1 Answers1

2

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Dear Bruno, I appreciate your help. It works fine now that I have followed your directions. Honestly, it does not make sense to me to the iText framework to require a scaling in order to render the image properly but once again I thank you for your time and wish I could collaborate more with your efforts if that sounds like proper to you. take care. – Mário de Sá Vera Dec 08 '15 at 20:31