We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert page break before section break continuous

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

Hi Gabriel,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, we would suggest you please upgrade to the latest version (v15.12.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document along with code example to reproduce this issue at our end. We will investigate the issue on our side and provide you more information.

We have tested the scenario using following code example and have not found the shared issue.

Document doc = new Document(MyDir + “in.docx”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentStart();

builder.MoveTo(builder.CurrentSection.Body.LastParagraph);

builder.InsertBreak(BreakType.PageBreak);

doc.Save(MyDir + "Out.docx");


Hi


Thank you for your answer. The updated brought no improvement. And your code does not work either.

In the “File Attachement” you can find my “in.docx” and my project. The “in” document has exactly one section. In my code, I want to copy this section incl. the Section Break into the “out” document . But before the Section Break comes i want to add a page break. This is not working. What am I doing wrong?

This is my code and the whole probject you can find in the attachements:
var doc = new Document(“D:\in.docx”);
var docbuilder = new DocumentBuilder();
docbuilder.MoveToDocumentStart();
docbuilder.MoveTo(docbuilder.CurrentSection.Body.LastParagraph);
docbuilder.InsertBreak(BreakType.PageBreak);
doc.Save(“D:\out.docx”);

Thank you for your help and kind regards
Gabriel Weibel
Hi Gabriel,

Thanks for your inquiry. Please pass Document object to DocumentBuilder's constructor. In your code the following highlighted code snippet is missing.

var doc = new Document(MyDir + "in.docx");

var docbuilder = new DocumentBuilder(doc);

docbuilder.MoveToDocumentStart();

docbuilder.MoveTo(docbuilder.CurrentSection.Body.LastParagraph);

docbuilder.InsertBreak(BreakType.PageBreak);

doc.Save(MyDir + "out.docx");