Extract Group Image

Dear Team,

We need to extract grouped shape images from word document using Aspose.words. Here I’ve attached sample document for your reference also attached expected output.

Input : ReferenceDoclet_withSingleCell.zip (22 Bytes)

Expected OP : Expected OP.zip (734.0 KB)

Please give us solution for this scenario.

Thank you

@ssvel

Thanks for your inquiry.

The ZIP file does not contain the document. Please ZIP and attach your input document. We will then provide you code example according to your requirement.

@tahir.manzoor

Please find the updated input and output.

Input : Chapter-25_left&right_figs.zip (571.1 KB)

Expected OP : Expected OP.zip (734.0 KB)

@ssvel

Thanks for sharing the document. Following code example shows how to extract the images from the document for “Fig” caption. Hope this helps you.

Document doc = new Document(MyDir + "Chapter-25_left&right_figs.docx");
int i = 1;
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs) {
    if (paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig")) {

        Node PreviousNode = paragraph.getPreviousSibling();

        if (PreviousNode != null && PreviousNode.getNodeType() == NodeType.PARAGRAPH &&
                ((Paragraph) PreviousNode).getChildNodes(NodeType.SHAPE, true).getCount() == 0
                && PreviousNode.toString(SaveFormat.TEXT).trim().contains("a)")
                && PreviousNode.toString(SaveFormat.TEXT).trim().contains("b)"))
        {
            PreviousNode = PreviousNode.getPreviousSibling();
        }


        if (PreviousNode != null && PreviousNode.getNodeType() == NodeType.PARAGRAPH &&
                ((Paragraph) PreviousNode).getChildNodes(NodeType.SHAPE, true).getCount() > 0)

        {
            Document dstDoc = new Document();
            NodeImporter importer = new NodeImporter(doc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            Node newNode = importer.importNode(PreviousNode, true);
            dstDoc.getFirstSection().getBody().appendChild(newNode);

            for (Run run : (Iterable<Run>) dstDoc.getChildNodes(NodeType.RUN, true)) {
              run.setText("");
            }
            dstDoc.save(MyDir + "output"+i+".docx");
            i++;
        }
    }
}

@tahir.manzoor

Thanks for your valuable support.

@ssvel

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.