Page 1 of 1

How to read/get Image from docx file and save to local drive

PostPosted: Sat Jan 03, 2015 10:21 pm
by sdhramveer
I have to read specific Image from .docx file and save on specific location.
but i'm unable to read it.
and getting "java.lang.IllegalArgumentException"
here is code...
Code: Select all

                  Drawing drw = (Drawing) XmlUtils.unwrap(drawingObjList.get(0));
                  if(drw != null)
                  {
                      List<Object> grp = ((Drawing) XmlUtils.unwrap(drw)).getAnchorOrInline();
                      if( grp != null)
                           {
                         Graphic grpdata = ((Inline) XmlUtils.unwrap(grp.get(0))).getGraphic();
                                                byte[] imgBytes = BinaryPartAbstractImage.getImage(wProcessorPackage, grpdata);

                                         RenderedImage renderImage = ImageIO.read(new ByteArrayInputStream(imgBytes));
                                         ImageIO.write(renderImage, "jpg", new File("/Users/gwuser164/Desktop/Images/image.jpg"));
                      }
                  }


here i'm getting bytes array but unable to use.
hope it's enough to understand.
any help.....

thanks

Re: How to read/get Image from docx file and save to local d

PostPosted: Thu Jan 08, 2015 7:25 am
by jason
The method BinaryPartAbstractImage.getImage assumes your image is in the main document part (as opposed to a header/footer).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                org.docx4j.relationships.Relationship rel =
                        wmlPkg.getMainDocumentPart().getRelationshipsPart().getRelationshipByID(rId);
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


And note it won't necessarily be a jpeg.