DocumenBuilder.insertNode(..) method behavior

Hi,

I can’t find it in a documentation, but it seems like that if you take an existing node from the document and then inserts it at some other place in that same document, that node is actually moved from it’s original position to the new one. Here’s some sample code:

String wordFilePath = "Content Controls Test File 2007.docx";

Document wordDoc = new Document(wordFilePath);

StructuredDocumentTag childSdt = (StructuredDocumentTag) wordDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 1,
true);

Paragraph parentParagraph = (Paragraph) wordDoc.getChild(NodeType.PARAGRAPH, 6, true);

Run run = (Run) parentParagraph.getChild(NodeType.RUN, 0, true);

DocumentBuilder db = new DocumentBuilder(wordDoc);

db.moveTo(run);

db.insertNode(childSdt);

wordDoc.save("Content Controls Test File 2007_MOVE.docx");

Can you please check this with devs so I can get a confirmation from them if this is expected and desired behavior or not?

Regards,
Zeljko

Hi Zeljko,

Thanks for your inquiry. We have logged a ticket WORDSJAVA-1555 in our issue tracking to get more details of insertNode() method. We will keep you updated about the issue resolution progress in this forum thread.

Best Regards,

Hi Zeljko,

Thanks for your patience. We have further looked into the issue and would like to update you that it is expected behavior of insertNode() method. However, if you do not want to move your existing node to new location using insertNode() method then you can clone your node as following.

com.aspose.words.Document wordDoc = new com.aspose.words.Document(wordFilePath);
StructuredDocumentTag childSdt = (StructuredDocumentTag) wordDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 1,true);
Paragraph parentParagraph = (Paragraph) wordDoc.getChild(NodeType.PARAGRAPH, 6, true);
Run run = (Run) parentParagraph.getChild(NodeType.RUN, 0, true);
DocumentBuilder db = new DocumentBuilder(wordDoc);
db.moveTo(run);
db.insertNode(childSdt.deepClone(true));
wordDoc.save("E:/Data/Content Controls Test File 2007_MOVE.docx");

Best Regards,