Getting image size from an XWPF document (Apache POI)

Has anyone had experience parsing a docx file using Apache poi? when retrieving an image from CharacterRun using

 paragraph.getRun().getEmbeddedPictures(); 

I could not find a way to get the scaling and / or width / height of the image data.

Are there any features for this?

Thanks.

+4
source share
1 answer

Got it about that!

I'm not sure this is the official way, but here's how to do it.

XWPFRun does not give you any information, but XWPFPicture has a method called getCTPicture which will return the XML part of the image to the docx file using your favorite XML parser, all you need to do is find "/ xml-fragment / pic: spPr / a: xfrm / a: ext "(xpath) in the XML structure, this tag will have cx and cy attributes, this is mainly the display width and image height in EMU English metric units. If you are google, you can find EMUS_PER_INCH = 914400 so that you can convert this to inches (or later convert inches to pixels, if I'm not mistaken, it should be 96 pixels per inch) so when calculating you can determine the new width and height of the image.

Complexity, but that's what it is. And it works.

Hope this helps someone.

+7
source

All Articles