How to get image from * .docx with all changes using POI?

Check out the link between docx and pictures:

As I understand it, it *.docxstores original pictures (photos at the moment when you copy / paste them into Word). And every time you use this image, Word makes a link to the original image.

But if you make some changes to this image (for example, change the size, circle or change the color), Word remembers your changes by changing the “link” (add some special tags). This is great because you will never lose the quality of your picture!

Let me get an image from our * .docx file. For this I use this piece of code:

XWPFDocument wordDoc = new XWPFDocument( pathToFile );
for (XWPFParagraph p : wordDoc.getParagraphs()) {
    for (XWPFRun run : p.getRuns()) {
        for (XWPFPicture pic : run.getEmbeddedPictures()) {
            byte [] img = pic.getPictureData().getData()

            File  outputfile = new File ( pathToOutputFile );                
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(img));
            ImageIO.write(image , "png", outputfile);
        }
    }
}

*.docx. , , , outputfile. .

- , , - Word?

+4

Source: https://habr.com/ru/post/1533218/


All Articles