Hello,
I will really appreciate some tips about the best option to insert a whole doc at a specific location into another one.
I have a merge field in the dest doc and I use the MoveToMergeField function to go to the point when I need to insert the source doc.
My problem starts here : I can’t figure out how to do the job once I get the right position in the dest doc.
Could you help please ?
Regars,
Vincent
Please try the following method:
///
/// Inserts content of the external document after the specified node.
///
/// Node in the destination document where the external document content should be inserted.
/// Document to insert.
public void InsertDocument(Node node, Document doc)
{
Document dstDoc = node.Document;
Section insertedSection;
int index = node.ParentNode.ChildNodes.IndexOf(node);
foreach (Section section in doc.Sections)
{
insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
foreach (Node insertedNode in insertedSection.Body.ChildNodes)
{
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (insertedNode is Paragraph || insertedNode is Table)
{
// Do not insert node if it is a last empty paragarph in the section.
if (insertedNode is Paragraph && insertedNode == section.Body.LastChild && insertedNode.ToTxt().Equals(string.Empty))
break;
node.ParentNode.ParentNode.ChildNodes.Insert(++index, insertedNode.Clone(true));
}
}
}
}