Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 2, 2021 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/7af5b641d0ab658dbddce3292649c227 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7af5b641d0ab658dbddce3292649c227 to your computer and use it in GitHub Desktop.
Extract Images from Word Documents in Java
// 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