We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Issues with extracting image from a word document with Aspose Java


I have been trying to extract image out of a word document and use it to paste in some other document at a particular position.The only help I found with Aspose for accomplishing this was with the following code but apparently it has not worked with me.

I would really appreciate if you could please provide me any sample code/help/pointers for accomplishing this.

For your reference please find the file which hold the image attached to this email.

(I assume this a c# code.I have still not been able to find any help with the java code)

public void ExtractImagesToFiles()
Document doc = new Document(MyDir + “Image.SampleImages.doc”);
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, <span class="code-keyword">true</span>);
<span class="code-object">int</span> imageIndex = 0;            
foreach (Shape shape in shapes)
    <span class="code-keyword">if</span> (shape.HasImage)
        string imageFileName = string.Format(
            <span class="code-quote">"Image.ExportImages.{0} Out{1}"</span>, imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType));
        shape.ImageData.Save(MyDir + imageFileName);


Hi there,

Thanks for your inquiry.

Images in Word documents are either represented by Shape nodes or by DrawingML nodes when loading into Aspose.Words’ DOM. The image found in your document is actually represented by DrawingML node. Please read the members of DrawingML and Shape class from here:

Please use the following code example to achieve your requirements. Hope this helps you.

Document doc = new
Document(MyDir + “graph.docx”);<o:p></o:p>

int i = 0;

for (Shape shape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {

if (shape.hasImage()) {

shape.getImageData().save(MyDir + "Image.ExportImages.Out_" + i + FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));




for (DrawingML dml : (Iterable<DrawingML>) doc.getChildNodes(NodeType.DRAWING_ML, true)) {

if (dml.hasImage()) {

dml.getImageData().save(MyDir + "Image.ExportImages.Out_" + i + FileFormatUtil.imageTypeToExtension(dml.getImageData().getImageType()));




Awesome Tahir.You are the best !! It works fine at my end :slight_smile: :slight_smile:

I understand that once I have the image on the local machine I can convert it into byteStreamArray.But I wanted to know if there is way I can have it converted into bytestream or anything that is compatible with ‘insertImage’ of Aspose word library without having to save the image on the local machine.I was trying to cast dml object but I have had no success so far.

All I want to accomplish eventually is insert just the 2nd image in another document.

Any help will be appreciated.

Hi there,

Thanks for your inquiry. Please use the following code snippet to extract image from one document and insert into another document without saving it into disk. Please check the overloaded methods of DocumentBuilder.insertImage method from here:


Hope this helps you. Please let us know if you have any more queries.

Document doc = new
Document(MyDir + “graph.docx”);<o:p></o:p>

//Save Shape node to stream

ByteArrayOutputStream dstStream = new ByteArrayOutputStream();

Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);


//Insert image into new document

Document newDoc = new Document();

DocumentBuilder builder = new DocumentBuilder(newDoc);

ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());


newDoc.save(MyDir + "Out.docx");