How to Delete All Section Breaks in Word using C#

In this short tutorial, we will learn how to delete all section breaks in Word using C#. It has all the details to set the development environment to develop the application and run the sample code to demonstrate how to remove all section breaks in Word using C#. You will also learn to filter different section breaks before deleting them if required.

Steps to Remove Section Break in Word using C#

  1. Set the environment to use Aspose.Words for .NET to remove the sections breaks
  2. Load the source Word file into the Document object having a few sections in it for removal
  3. Iterate through all the sections starting from the second last to the first one in the document
  4. Access the contents of the current section and append it to the section next to the current one
  5. Remove the current section after copying its contents
  6. Repeat the process in reverse order until the first sections in the documents reached
  7. Save the resultant Word file after removing the section breaks

These steps provide a quick look at how to remove section break from Word using C# where the process is commenced by loading the target Word file and accessing its second last section in the document. The contents of this section are appended to the next section followed by deleting the current section. This process is repeated until the first section of the document is processed and all the section breaks are deleted before saving the resultant Word file.

Code to Delete Section Break in Word using C#

This code manifests the process to remove a section break in Word using C#. The sections are iterated starting from the second last section however you may filter the process by starting it from any section index. On the other hand, you may also select the sections for deletion based on the Section.Body property or Section.PageSetup if required.

This article has taught us to remove sections from Word document using C#. If you want to extract images from the Word file, refer to the article on how to extract images from Word document in C#.

 English