I have broken a template into 5 documents. These five documents are each have a paragraph (First Section) in them that I want to extract and add to the main document in a different order without line return. I am using the insertdocument and mail merge methods that were given as examples in the documentation. Each of the these methods keep putting a new line break in before the inserted document. Does anyone have any helpful examples working with sections and documents?
Thanks,
Bryan
Hi Yuri,
Thanks for your inquiry.
Please see the code below which demonstrates how to append the content of documents at the paragraph level.
public static void AppendDocumentAtParagraphLevel(Document dstDoc, Document srcDoc)
{
Paragraph lastPara = dstDoc.FirstSection.Body.LastParagraph;
foreach (Section section in srcDoc.Sections)
{
foreach (Node blockNode in section.Body)
{
CompositeNode newNode = (CompositeNode)lastPara.Document.ImportNode(blockNode, true, ImportFormatMode.KeepSourceFormatting);
Node insertNode = lastPara.LastChild;
if (newNode.NodeType == NodeType.Paragraph || newNode.NodeType == NodeType.StructuredDocumentTag || newNode.NodeType == NodeType.CustomXmlMarkup)
{
// Use GetChildNodes to find runs that are deeply nested even if the paragraph is wrapped within a SDT or CustomXml markup
foreach (Node inlineNode in newNode.GetChildNodes(NodeType.Run, true))
{
lastPara.InsertAfter(inlineNode, insertNode);
insertNode = inlineNode;
}
}
else if (newNode.NodeType == NodeType.Table)
{
lastPara.ParentNode.InsertAfter(newNode, lastPara);
}
}
}
}
If you have any further queries, please feel free to ask.
Thanks,
That this works great. I have not tried it yet but will this method keep the formating and spacing of tables that are merged in?
Hi Yuri,
Thanks for your inquiry.
Yes it should as the tables are imported as whole nodes. The method itself may take a bit of tweaking depending upon how you want the documents to be appended.
If you run into any issues I will be glad to help you.
Thanks,