The newChild was created from a different document than the one that created this node

Exception in thread “main” java.lang.IllegalArgumentException: The newChild was created from a different document than the one that created this node.
Trying to insert selected nodes from source document into destination document and getting above error.

@wrushu2004,

You need to import the selected nodes first. For example:

Document dst = new Document("D:\\temp\\table.docx");
Table tab = dst.getFirstSection().getBody().getTables().get(0);

Document src = new Document();
// src.getFirstSection().getBody().appendChild(tab); // This will throw
Table importedTab = (Table) src.importNode(tab, true);
src.getFirstSection().getBody().appendChild(importedTab);

src.save("D:\\temp\\awjava-18.9.docx");