Products Technologies Demo Docs Blog Support Company

How to Delete All Section Breaks in a DOCX Document using C#

Section breaks allow documents to have different page sizes, different margins, and other properties related to the sections. However, there are scenarios where existing section breaks should be removed to create a consistent document layout. This article shows how to delete all section breaks in a document.

How to Delete All Section Breaks in a DOCX Document using C#

Section breaks allow documents to have different page sizes, different margins, and other properties related to the sections. However, there are scenarios where existing section breaks should be removed to create a consistent document layout.

A section can also have its own header and footer collection with headers for the first page and footers for the first page. Removing all sections creates a consistent layout with one page orientation and one set of margins, headers, and footers.

Looping Sections

To achieve this, all section break characters must be removed. To find the indexes of all these break characters, you can iterate over the collection of sections. We need to go through all sections except the first to set the input position to the beginning of the section. A new paragraph will be added at the specified location. Then the section break character is selected and removed by passing an empty string.

private void RemoveAllSections(TextControl textControl)
{            
    // remove all sections
    for (int section = 2; section <= textControl.Sections.Count;)
    {
        // set the input position to the start of the section
        textControl.Select(textControl.Sections[section].Start - 2, 0);

        // add a new paragraph
        textControl.Selection.Text = "\r\n";

        // select the end of paragraph character
        textControl.Selection.Length = 1;

        // remove the paragraph
        textControl.Selection.Text = "";
    }
}

In this example, the RemoveAllSections method is called after loading an MS Word DOCX document that contains multiple sections.

textControl1.Load("demo.docx", TXTextControl.StreamType.WordprocessingML);
RemoveAllSections(textControl1);

The following screenshot shows the document before and after section break removal.

Removing Section Breaks

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETApplicationFieldsASP.NET Core

Find ApplicationFields within Sections and Paragraphs in .NET C#

This article shows how to find ApplicationFields within sections and paragraphs in a document using TX Text Control .NET. The implemented extension methods can be used to retrieve the sections and…


ASP.NETWindows FormsWPF

Diagnostics: When Calling ServerTextControl.Create() Failed

Under rare conditions, the creation of a new ServerTextControl instance can fail and the Dispose method returns false. This article explains the reasons and how what to do in these cases.


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…