1

I have a program that convert Word docs to PDFs. After conversion, I want to find all images in the new PDF. If the image is of a specific dimension (200x100), then I want to draw another image on top of it. Better yet, if I can create a signature field and draw on top of it.

Any help please?

Haoest
  • 13,610
  • 29
  • 89
  • 105

1 Answers1

2

First try this example: ExtractImages

I know this example doesn't do what you need, but take a look at the MyImageRenderListener class that is used. This is an implementation of the RenderListener interface.

To meet your requirement, you should write your own RenderListener implementation, more specifically one that gives you the coordinates of the images. iText will give you these coordinates through the ImageRenderInfo object, more specifically as a Matrix object returned by the getCTM() method. This matrix can be interpreted using ordinary high-school algebra. I31 and I32 give you the X and Y position. In most cases I11 and I22 will give you the width and the height (unless the image is rotated).

Once you have X, Y, width and height, drawing whatever you want is easy. If you want to add a signature field, please read the digital signatures manual I wrote.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi Bruno. I was able to get very close to XYHW. In fact, I can obtain these values from getImageCTM by getting index 6, 7, 0, and 4. The width and height values obtained, however, are smaller than the actually image. Both are shrank by the ratio of ~= 1.367. If I write the image to disk as a single file,the dimension is correct; also if I use debugger to Watch image info, its private fields also shows the correct dimensions. Do you recommend me using reflection to read from private fields, or is there somewhere I can get the ratio to multiply the shrank values with? – Haoest Mar 26 '13 at 16:56
  • I'm puzzled about this problem. We'll have to make an example to test this and to find out what goes wrong. Note that the dimension of the extracted image can be different than the dimension of the image on the page. For instance: an image that measures 300 x 300 pixels, can be rendered as an image of 72 x 72 points (resolution 300 dpi), regardless of the resolution stored in the image. – Bruno Lowagie Mar 27 '13 at 07:32
  • I ended up using renderInfo.getBufferedImage().getWidth()/Height. This gives me the original dimension of the image. What getImageCTM() give are the coordinates and size of the image in the context of the PDF page. So if I want to draw something of a certain size directly on top of the placeholder, I must use the exact numbers given by getImageCTM(). – Haoest Mar 27 '13 at 14:01
  • @BrunoLowagie,I tried to follow your link to read more about getCTM() method from https://itextpdf.com/reference/com.itextpdf.text.pdf.parser.ImageRenderInfo#2nq520mht3bg but that link is dead.I understand long time has passed since the original article and your Products Website has been updated many times.So I went to your new website and did a manual search in the knowledge base with https://kb.itextpdf.com/home/search?q=getCTM&max=500&s=IT5KB&s=IT7KB The reason I am writing this comment is because there are still 0 results.Where can I read about getCTM in the context of itext5? Thank you – Hakan Usakli Jun 12 '20 at 11:09