Learn how to extract images from Word documents using Java: https://blog.aspose.com/2021/11/02/extract-images-from-word-in-java/
Last active
December 2, 2021 14:13
-
-
Save aspose-com-gists/7af5b641d0ab658dbddce3292649c227 to your computer and use it in GitHub Desktop.
Extract Images from Word Documents in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load Word document | |
Document doc = new Document("Document.docx"); | |
// Get all the shapes | |
NodeCollection<Shape> shapes = (NodeCollection<Shape>) doc.getChildNodes(NodeType.SHAPE, true); | |
int imageIndex = 0; | |
// Loop through the shape collection | |
for (Shape shape : shapes) { | |
// Check if shape has image | |
if (shape.hasImage()) { | |
// Extract and save the image | |
String imageFileName = String.format( | |
"Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType())); | |
shape.getImageData().save(dataDir + imageFileName); | |
imageIndex++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment