Free Support Forum - aspose.com

Unable to read embedded object in doc file

Hi,

I've a doc file which contains another doc file which contains an image I want to extract.

However, I don't get any Shape object as child node type but get Document-Section-Body-Paragraph-Run.

Below statment returns 0.

NodeCollection shapess = doc.getChildNodes(NodeType.SHAPE, true);

System.out.println("shapes.getCount() " + shapes.getCount());

Regards,

Pramod Talekar

Hi Pramod,


Thanks for your inquiry. Please use the following code to extract images from your embedded document:
Document doc = new Document(“C:\Temp\testing3.doc”);
for (Shape shape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {
if (shape.getOleFormat() != null) {
if (shape.getOleFormat().getProgId().equals(“Word.Document.8”)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
shape.getOleFormat().save(baos);
        InputStream inStream <font color="BLUE">=</font> <font color="RED"><b>new</b></font> ByteArrayInputStream<font color="BLUE"><b>(</b></font>baos<font color="BLUE"><b>.</b></font>toByteArray<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        Document newDoc <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Document<font color="BLUE"><b>(</b></font>inStream<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        <font color="RED"><b>int</b></font> i <font color="BLUE">=</font> <font color="BROWN">0</font><font color="BLUE"><b>;</b></font>
        <font color="RED"><b>for</b></font> <font color="BLUE"><b>(</b></font>Shape s <font color="BLUE">:</font> <font color="BLUE"><b>(</b></font>Iterable<font color="BLUE"><</font>Shape<font color="BLUE">></font><font color="BLUE"><b>)</b></font> newDoc<font color="BLUE"><b>.</b></font>getChildNodes<font color="BLUE"><b>(</b></font>NodeType<font color="BLUE"><b>.</b></font>SHAPE<font color="BLUE"><b>,</b></font> true<font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE"><b>{</b></font>
            ShapeRenderer r <font color="BLUE">=</font> s<font color="BLUE"><b>.</b></font>getShapeRenderer<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

            ImageSaveOptions imageOptions <font color="BLUE">=</font> <font color="RED"><b>new</b></font> ImageSaveOptions<font color="BLUE"><b>(</b></font>SaveFormat<font color="BLUE"><b>.</b></font>PNG<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
            r<font color="BLUE"><b>.</b></font>save<font color="BLUE"><b>(</b></font><font color="PURPLE">"C:\\temp\\img\\img"</font> <font color="BLUE">+</font> i <font color="BLUE">+</font> <font color="PURPLE">".png"</font><font color="BLUE"><b>,</b></font> imageOptions<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
            i<font color="BLUE"><font color="BLUE">+</font><font color="BLUE">+</font></font><font color="BLUE"><b>;</b></font>
        <font color="BLUE"><b>}</b></font>
    <font color="BLUE"><b>}</b></font>
<font color="BLUE"><b>}</b></font>

}

I hope, this helps.

Best Regards,