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

Free Support Forum - aspose.com

How to insert new node before and after a shape

Hi team,

How to insert a empty node(most probably Paragraph) before and after a shape? Shape should include all graphical images,tables,group images inside text area.I had tried with the below code,

Document doc = new Document (“word_doc”);
for(Shape s :(Iterable)doc.getChildNodes(NodeType.SHAPE, true) ) {
s.getParentNode().insertBefore(new Paragraph(doc), s);
s.getParentNode().insertAfter(new Paragraph(doc), s);
}
doc.save(“filename”);

But, it resulted with the following error.

Error:
Invalid arguments
Cannot insert a node of this type at this location.

For time being please give a work around solution earlier as much possible.

Sample word doc: 22-LNCS11418_268.zip (930.0 KB)

Thanks in advance.

Regards,
Gomathi. N.

@Gomathi,

Please note that Shape is an inline level node. Please refer to the following article for more details:
Aspose.Words Document Object Model

The following code inserts paragraphs after and before the paragraphs the Shapes are anchored to:

for(Shape s :(Iterable<Shape>)doc.getChildNodes(NodeType.SHAPE, true) ) {
    Paragraph para = (Paragraph) s.getAncestor(NodeType.PARAGRAPH);
    if (para != null){
        para.getParentNode().insertBefore(new Paragraph(doc), para);
        para.getParentNode().insertAfter(new Paragraph(doc), para);
    }
}

Hope, this helps.

Dear Awais,

I have tried the below code but partial output only I got with. Please go through the attached sample for further analysis.

Sample: ch_1_ip.zip (724.8 KB)

Thanks in advance.

Regards,
Gomathi. N.

@Gomathi,

Please check if the following code is acceptable for you?

Document doc = new Document("E:\\ch_1_ip\\ch_1_ip.docm");

for(Shape s :(Iterable<Shape>)doc.getChildNodes(NodeType.SHAPE, true) ) {
    s.getParentNode().insertBefore(new Run(doc, ControlChar.LINE_BREAK), s);
    s.getParentNode().insertAfter(new Run(doc, ControlChar.LINE_BREAK), s);
}

doc.save("E:\\ch_1_ip\\awjava-19.3.docm");