Hi,
I need to insert several word documents into a single document at specific positions. I have used the following code, and everything works well apart from the Continuous Section Breaks seem to be behaving like NewPage Section Breaks as they force a new page to start where each document is inserted. Any advice would be greatly appreciated:
public void InsertDocument(Node node, Document doc)
{
Document dstDoc = node.Document;
Section insertedSection;
int index;
if(node.GetAncestor(typeof(Cell))!=null || node.GetAncestor(typeof(Shape))!=null)
{
// Insertion point is tracked to Cell or Shape level:
// - insert appended document on node by node basis.
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(node is Paragraph || node is Table)
{
node.ParentNode.ChildNodes.Insert(index++, insertedNode);
}
}
}
}
else
{
// Insertion point is tracked to Section.Body level:
// - insert appended document on section by section basis.
Section dstSection;
Body body = (Body)node.GetAncestor(typeof(Body));
if(body.LastChild!=node)
{
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.MoveTo(node);
builder.InsertBreak(BreakType.SectionBreakContinuous);
dstSection = builder.CurrentParagraph.ParentSection;
}
else
{
dstSection = (Section)node.GetAncestor(typeof(Section));
}
index = dstDoc.Sections.IndexOf(dstSection);
int index0 = index;
foreach(Section section in doc.Sections)
{
insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
dstDoc.Sections.Insert(index++, insertedSection);
}
dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;
}
}