Thanks for the reply but it’s not doing what I would like it to do. It appears this code will only work if the source bookmark is a paragraph. If the bookmark contains a Table it doesn’t cast to a Paragraph and if it only contains formatted text then it won’t import anything at all.
The following code (from the Aspose web site) works fine on all paragraph types but only when I insert it at the start or end of a paragraph in the destination document. I want to insert it in the middle of a paragraph at the point where this is a bookmark. Effectively how can I replace the destination bookmark contents with the source bookmark contents?
I can do this manually in Word o.k - I can select the source bookmark, copy and paste it over the destination bookmark. The destination bookmark is replaced in it’s entirety, including keeping it as a bookmark with the source bookmark name.
Thanks for your help.
David
/**
* Inserts content of the external document after the specified node.
* Section breaks and section formatting of the inserted document are ignored.
*
* @param insertAfterNode Node in the destination document after which the content
* should be inserted. This node should be a block level node (paragraph or table).
* @param srcDoc The document to insert.
*/
public static void insertDocument(Node insertAfterNode, Document srcDoc) throws Exception
{
// Make sure that the node is either a paragraph or table.
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
(insertAfterNode.getNodeType() != NodeType.TABLE))
throw new IllegalArgumentException(“The destination node should be either a paragraph or table.”);
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.getParentNode();
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Loop through all sections in the source document.
for (Section srcSection : srcDoc.getSections())
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
for (Node srcNode : (Iterable) srcSection.getBody())
{
// Let’s skip the node if it is a last empty paragraph in a section.
if (srcNode.getNodeType() == (NodeType.PARAGRAPH))
{
Paragraph para = (Paragraph)srcNode;
if (para.isEndOfSection() && !para.hasChildNodes())
continue;
}
// 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;
}
}
}