I am trying to cross through a word document and save all the images found in the word document. I tried to upload the sample word document and found that the images are listed as follows:
/word/media/image1.png rId5 image / png / word / media /image2.png rId5 Image / Png / word / media / image3.jpg rId5 image / jpeg
How do I save these images while crossing the document?
Current I get all the text from the document like this:
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load (New java.io.File (filepath)) MainDocumentPart documentPart = wordMLPackage GetMainDocumentPart () document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement () body body = wmlDocumentEl.getBody (); Document tracer travers = new document tracer (); Class document spreads out to Traversal Youth. CallbackImpl {@Override public list & lt; Object & gt; Applicable (object o) {if (o instanceof org.docx4j.wml.Text) {....} return tap; For the images embedded (as opposed to external), the simplest way is to:
Import java.io.FileOutputStream; Import java.util.Map.Entry; Import org.docx4j.openpackaging.packages.WordprocessingMLPackage; Import org.docx4j.openpackaging.parts.Part; Import org.docx4j.openpackaging.parts.PartName; Import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; Import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; Public class SaveImages {public static void (String [] args) {Exception WordProcessingMLPackage wordMLPackage = WordprocessingMLPackage.load (New java.io.File (inputfilepath) throws; (; PartName, part & gt; Sign in & lt entry: WordMLPackage.getParts () getParts () entrySet (.) For {if (entry.getValue) BinaryPartAbstractImage instanceof} {FileOutputStream FOS = New FileOutputStream (Yourfile) ; // TODO: You can get file extension from PartName, or fractional class. (BinaryPart) entry.getValue ()) writeDataToOutputStream (FOS). Fos.close (); }}}}
If you care about the context of the images, then you have to search for them in the relevant parts (for example MainDocumentPart, and your header / footer parts etc. As required).
You have to give an indication of how to do this. Note that there are two different XML structures for images, new drawing XML, and legacy VML
Comments
Post a Comment