Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting image size from XWPF document (Apache POI)

anyone had experience parsing docx file with Apache poi? while extracting image from CharacterRun using

paragraph.getRun().getEmbeddedPictures();

I was not able to find a way to get scaling or/and width/hight data of picture.

Is there functionality for that?

Thank you.

like image 447
Avetis Zakharyan Avatar asked Jan 14 '23 14:01

Avetis Zakharyan


1 Answers

Figured that out!

I am not sure if that's the official way, but, here is how you can do it.

XWPFRun doesnot specifically give you any info, but XWPFPicture, has method called, getCTPicture which will return an XML part of picture in docx file, using your favorite XML parser, all you need is to find this "/xml-fragment/pic:spPr/a:xfrm/a:ext" (xpath) in XML structure, that tag will have cx and cy attributes, these are basically the display width and height of image in EMU's English Metric Units. If you google, you can find that EMUS_PER_INCH = 914400 so you can convert this to inches (or then later convert inches to pixels, if I am not mistaken should be 96 pixels per inch) so doing some calculation you can figure out the new width and height of picture.

Kinda complicated, but it is what it is. And it works.

Hope that will help to someone.

like image 86
Avetis Zakharyan Avatar answered Jan 21 '23 13:01

Avetis Zakharyan