Hello,
We have a function that copies a certain number of sections from multiple documents into a main document . When copying the sections the final section breaks are also copied. The sections are copied by the following function:
private static void CopyAndInsertNodes(SectionMergeSettings smSettings, ref Document doc, List sourceNodes, List anzahlProFile)
{
var sectionsCount = smSettings.InsertAfterSection - 1;
var targetSection = doc.Sections[sectionsCount];
Section contentSection = null;
var cnt = 0;
var sourceNodesCount = sourceNodes.Count;
var fileIndex = 0;
for (var sourceNodeIndex = 0; sourceNodeIndex < sourceNodesCount; sourceNodeIndex++)
{
var node = sourceNodes[sourceNodeIndex];
if (!(node is StructuredDocumentTag) && !(node is Section)) continue;
var newBookMarkName = mergeSection + Guid.NewGuid();
var newNode = doc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
var oldcount = sectionsCount;
if (newNode is Section)
{
doc.InsertAfter(newNode, targetSection);
sectionsCount++;
targetSection = newNode as Section;
}
else if (newNode is StructuredDocumentTag)
{
if (targetSection == null)
continue;
if (contentSection == null)
{
// Neue Section erstellen - Content Controls können nur einer Section zugeordnet werden
contentSection = targetSection.Clone();
// Bestehende Section klonen und leeren (damit bleibt die Formatierung dieselbe)
contentSection.ClearHeadersFooters();
contentSection.ClearContent();
contentSection.Body.Paragraphs.Clear(); //Removes Empty Paragraph
sectionsCount++;
// Section einfügen
doc.InsertAfter(contentSection, targetSection);
targetSection = contentSection;
}
// Section ergänzen
var index = doc.Sections.IndexOf(contentSection);
if (index >= 0)
doc.Sections[index].Body.AppendChild(newNode);
}
else
throw new NotImplementedException(
string.Format("Node-Type ‘{0}’ is not implemented in this method", newNode.GetType()));
if (oldcount == sectionsCount) continue;
var builder = new DocumentBuilder(doc);
builder.MoveToSection(sectionsCount);
builder.StartBookmark(newBookMarkName);
builder.EndBookmark(newBookMarkName);
if (cnt == anzahlProFile[fileIndex])
{
cnt = 0;
if (fileIndex + 1 < anzahlProFile.Count)
fileIndex++;
}
// Here I want to add a page break before the section break.
if (cnt == 0)
{
var sectionIndex = doc.Sections.IndexOf(targetSection);
var docbuilder = new DocumentBuilder(doc);
docbuilder.MoveToSection(sectionIndex);
// LastParagraph is the end of a section. So i want to add the page break before. But i doesn’t work. Why? docbuilder.MoveToParagraph(docbuilder.CurrentSection.Body.IndexOf(docbuilder.CurrentSection.Body.LastParagraph)-1,-1);
if (smSettings.AddSeitenumbruch)
{
docbuilder.InsertBreak(BreakType.PageBreak);
}
}
cnt++;
}
}
Thank you for your help.
WGA