How to Remove Page breaks in C#

In Word document, users can add new page break or remove existing page breaks. This sample shows how to remove page breaks from the word document by using Spire.Doc. Spire.Doc supports to remove the page breaks from the word document from the format of .docx, .doc, and RTF etc.

Firstly make sure Spire.Doc for .NET has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: ".. \Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to remove page breaks in C#.

//Create a new word document and load from the file.
Document document = new Document();
document.LoadFromFile("sample.docx");

// Traverse every paragraph of the first section of the document
for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
  {
   Paragraph p = document.Sections[0].Paragraphs[j];

// Traverse every child object of a paragraph
for (int i = 0; i < p.ChildObjects.Count; i++)
    {
     DocumentObject obj = p.ChildObjects[i];

//Find the page break object
 if (obj.DocumentObjectType == DocumentObjectType.Break)
    {
     Break b = obj as Break;

// Remove the page break object from paragraph
p.ChildObjects.Remove(b);

//save the document to file.
document.SaveToFile("result.docx");

Please check the effective screenshot:

How to Remove Page breaks in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
namespace RemovePageBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("sample.docx", FileFormat.Docx);
            for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
            {
                Paragraph p = document.Sections[0].Paragraphs[j];
                for (int i = 0; i < p.ChildObjects.Count; i++)
                {
                    DocumentObject obj = p.ChildObjects[i];
                    if (obj.DocumentObjectType == DocumentObjectType.Break)
                    {
                        Break b = obj as Break;
                        p.ChildObjects.Remove(b);
                    }
                }
            }
            document.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}