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

Free Support Forum - aspose.com

Insert one document into another throws Exception using Java


I am trying to copy some contents from one document to another document.
While doing so i am getting following error.

Cannot insert a node of this type at this location.

code i am using to copy:

private void insertSubDocument(Node insertAfterNode, Document subDoc) throws Exception
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH)
& (insertAfterNode.getNodeType() != NodeType.TABLE))
//checkInFailReason = “The destination node should be either a paragraph or table.”;
checkInFailReason = “checkin.failed.destination.should.be.paragraph.or.table”;

throw new Exception(checkInFailReason);

CompositeNode dstStory = insertAfterNode.getParentNode();

NodeImporter importer = new NodeImporter(subDoc, insertAfterNode.getDocument(),

for (Section srcSection : subDoc.getSections())
// Loop through all block level nodes (paragraphs and tables) in the
// body of the section.
for (Node srcNode : (Iterable) srcSection.getChildNodes(NodeType.BODY, true))
// Let’s skip the node if it is a last empty paragarph in a
// section.
if (srcNode.getNodeType() == NodeType.PARAGRAPH)
Paragraph para = (Paragraph) srcNode;

if (para.isEndOfSection() && !para.hasChildNodes())

// This creates a clone of the node, suitable for insertion into
// the destination document.
Node newNode = importer.importNode(srcNode, true);

// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;


Thanks for your query. The first parameter of insertSubDocument should be of Node Type Paragraph or Table. Please pass Paragraph node type to insertSubDocument function. If you still face problem, please share your documents and function call for investigation purposes.