In Aspose.Words, use the SectionBreak field of the ControlChar class to find all section breaks.
The following code example shows how to remove page breaks from a document:
Document doc = new Document(MyDir + "Remove section breaks.docx");
// Loop through all sections starting from the section that precedes the last one
for (int i = doc.Sections.Count - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc.LastSection.PrependContent(doc.Sections[i]);
// Remove the copied section.
doc.Sections[i].Remove();
}
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
Following are the namespaces we need to add:
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using NUnit.Framework;
The following code example shows how to remove section breaks from a document:
public void RemoveSectionBreaksFeature()
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(MyDir + "Remove section breaks.docx", true))
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
List<ParagraphProperties> paraProps = mainPart.Document.Descendants<ParagraphProperties>()
.Where(IsSectionProps).ToList();
foreach (ParagraphProperties pPr in paraProps)
pPr.RemoveChild(pPr.GetFirstChild<SectionProperties>());
using (Stream stream = File.Create(ArtifactsDir + "Remove section breaks - OpenXML.docx"))
{
mainPart.Document.Save(stream);
}
}
}
private static bool IsSectionProps(ParagraphProperties pPr)
{
SectionProperties sectPr = pPr.GetFirstChild<SectionProperties>();
if (sectPr == null)
return false;
return true;
}