I was able to get this working but found another issue.
Part of the text that is inserted is not formatted correctly.
I attached three documents: one.doc, two.doc, completed.doc.
As you can see I inserted two.doc into one.doc and the text ‘Item Details’ is formatted in Times New Roman and the rest of the document is in Arial, which is correct.
Here is the code I used:
public void InsertDocumentAtBookmark(String afterNode, String doc, String bookmarkName, String NewDoc)
{
// Create Document
Document dstDoc = new Document(afterNode);
Document srcDoc = new Document(doc);
//Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(dstDoc);
//Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName);
builder.Writeln();
//Content of srcdoc will be inserted after this node
Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
//Content of first paragraph of srcDoc will be apended to this parafraph
Aspose.Words.Paragraph insertAfterParagraph = insertAfterNode as Aspose.Words.Paragraph;
//Content of last paragraph of srcDoc will be apended to this parafraph
Aspose.Words.Paragraph insertBeforeParagraph = builder.CurrentParagraph;
//We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
//Remove empty paragraphs from the end of document
//while (!srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
//{
// srcDoc.LastSection.Body.LastParagraph.Remove();
//}
//Loop through all sections in the source document.
foreach (Aspose.Words.Section srcSection in srcDoc.Sections)
{
//Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
//Do not insert node if it is a last empty paragarph in the section.
Aspose.Words.Paragraph para = srcNode as Aspose.Words.Paragraph;
if ((para != null) && para.IsEndOfSection && (!para.HasChildNodes))
{
break;
}
//If current paragraph is first paragraph of srcDoc
//then appent its content to insertAfterParagraph
if (para != null)
{
if (para.Equals(srcDoc.FirstSection.Body.FirstParagraph))
{
foreach (Node node in para.ChildNodes)
{
Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
insertAfterParagraph.AppendChild(dstNode);
}
}
//If current paragraph is last paragraph of srcDoc
//then appent its content to insertBeforeParagraph
else if (para.Equals(srcDoc.LastSection.Body.LastParagraph))
{
Node previouseNode = null;
foreach (Node node in para.ChildNodes)
{
Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
if (previouseNode == null)
{
insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
}
else
{
insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
}
previouseNode = dstNode;
}
}
else
{
//This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
//Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
else
{
//This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
//Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
dstDoc.Save(NewDoc);
}