Cannot insert document at bookmark

hello, I keep getting this error:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.
at com.aspose.words.CompositeNode.a(Unknown Source)
at com.aspose.words.CompositeNode.insertAfter(Unknown Source)
at com.rwd.ASPOSE.Merge.InsertDocument(Merge.java:91)
at com.rwd.ASPOSE.Merge.bookmark(Merge.java:112)
at com.rwd.ASPOSE.Merge.main(Merge.java:20)

I have included my java file as well as test documents.
Thank you.

Hi

Thanks for your inquiry. There is an mistake in your code. You should use the following code:

Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);

Instead of

Node srcNode = srcSection.getChildNodes().get(nodeIndex);

Hope this helps.
Best regards.

Ah, thanks so much, that worked.
I have a follow up question. I’m trying to delete the content of the bookmark before I insert the document. I attempt to do bookmark.settext("");, but what happens is that the entire bookmark is gone, and the document is inserted at the next bookmark.
Is there an easy way to delete the content of the bookmark, but not the bookmark?
Thanks.

Hi

Thanks for your inquiry. bookmark.setText(“”) will not remove entire bookmark, this code will remove only bookmark content.
Maybe you should try using the code provided here, to insert document at bookmark
https://forum.aspose.com/t/100067
Hope this helps.
Best regards.