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.
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.
The following screenshot shows the document before and after section break removal.