Hi
Thanks for your request. I modified InsertDocument method a little. Please see the following code.
public void InsertDocumentAtBookmark(string bookmarkName, Document dstDoc, Document srcDoc)
{
// 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
Paragraph insertAfterParagraph = (Paragraph)insertAfterNode;
// Content of last paragraph of srcDoc will be apended to this parafraph
Paragraph insertBeforeParagraph = builder.CurrentParagraph;
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// Loop through all sections in the source document.
foreach (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.
Paragraph para = srcNode as Paragraph;
if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
break;
// If current paragraph is first paragraph of srcDoc
// then append its content to insertAfterParagraph
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 append 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;
}
}
}
}
I hope this could help you.
Best regards.