Skip to content

Instantly share code, notes, and snippets.

Code to Extract Images from Word File in Java. For more information: https://kb.aspose.com/words/java/how-to-extract-images-from-word-file-in-java/
import com.aspose.words.Document;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Shape;
import com.aspose.words.FileFormatUtil;
public class AsposeTest {
public static void main(String[] args) throws Exception {//Main function to extract images from a Word file
// Instantiate the license
com.aspose.words.License licPage = new com.aspose.words.License();
licPage.setLicense("Aspose.Total.lic");
//
Document imagesWordFile = new Document("WordFileWithImages.docx");
NodeCollection<Shape> docShapes = (NodeCollection<Shape>) imagesWordFile.getChildNodes(NodeType.SHAPE, true);
int indexForImages = 0;
for (Shape shape : docShapes
) {
if (shape.hasImage()) {
String outputImage = String.format("Image" + indexForImages + FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
shape.getImageData().save(outputImage);
indexForImages++;
}
}
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment