Last active
September 23, 2022 16:09
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/
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
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