# Sections and Page Layout

> Divide WPF documents into sections with landscape pages, margins and columns: SectionCollection.Add, SectionFormat, SectionChanged in XAML, 1/96 inch units.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Document Editor > Sections and Page Layout
- **2826 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/sections/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/sections/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/sections/llms-full.txt

---

When a report needs one landscape page for a wide table, or a heading that spans the page above two columns of text, the document is divided into sections. In the WPF `TextControl`, each section has its own page size, margins, orientation, columns and headers; users set them on the **Page Layout** tab and in the **Page Setup** dialog box, and your code inserts section breaks, formats sections and follows the input position from section to section.

The code goes into *MainWindow.xaml.cs* of the window from [Create Your First WPF Application](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt), with a `TextControl` named `textControl1` and `SampleApp` replaced by your namespace. [Headers and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/llms-full.txt) covers the headers and footers of a section, and [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt) sections in documents processed without a user interface.

### What a Section Is

A [Section](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/section/llms-full.txt) of the `TXTextControl` namespace is a part of the document with its own page settings and headers and footers. Every document has at least one, and they are numbered from 1. The text, tables and images of all sections are in the usual collections of the `TextControl`.

![Class diagram of the section and page classes: TextControl, WPF.TextControl and ServerTextControl with Sections, PageSize, PageMargins, PageUnit and SectionFormatDialog, SectionCollection, Section, SectionFormat, PageBorder, PageSize, PageMargins and the Page classes.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/sections-class-diagram.4b456f42.webp)

A section break either starts a new page or continues on the next line. A new section inherits every setting of the one before it. The page size and separate headers and footers need a section that begins on a new page, while a section that begins on a new line can change the number of columns in the middle of a page.

### Set Up Pages in the Ribbon

The **Page Layout** tab, a [RibbonPageLayoutTab](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribbonpagelayouttab/llms-full.txt) connected through the [RibbonPageLayoutTab](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/ribbonpagelayouttab-property/llms-full.txt) property of the `TextControl`, changes the section with the input position:

- **Page Setup:** **Margins** opens the page setup dialog box, **Orientation** switches between **Portrait** and **Landscape**, and **Size** sets the paper size of the current section.
- **Columns & Breaks:** **Columns** offers **One**, **Two**, **Three** and **More Columns...**. **Breaks** inserts page breaks (**Page**, **Column**, **Text Wrapping**) and section breaks: **Next Page** starts the new section on a new page, **Continuous** on the next line.
- **Page Background & Frames:** **Page Color**, and **Frame**, **Frame Color** and **Frame Width** for a border around the pages of the section.

The **Page Setup** dialog box has the tabs **Margins and Paper**, **Headers and Footers**, **Columns** and **Frames**. On the first tab, **Section start** chooses how the section begins, and **Apply to** applies the settings to the **Whole document** or to the current **Section**:

![The Page Setup dialog box on its Margins and Paper tab, with margins, paper format A4, width and height, portrait and landscape orientation, the Section start list set to New page and the Apply to list open with Whole document and Section.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/page-setup-margins-paper.81fbf900.webp)

[SectionFormatDialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/sectionformatdialog-method/llms-full.txt) opens the dialog box. Its tab index is 0 for margins, page size and orientation, 1 for headers and footers, 2 for page columns and 3 for page borders.

### Insert a Section and Follow the Input Position

Declare the `SectionChanged` event and a `TextBlock` for the section number in *MainWindow.xaml*, where `WPF` is the prefix of `TXTextControl.WPF`:

```xml
<DockPanel>
    <TextBlock x:Name="sectionText" DockPanel.Dock="Bottom" />
    <WPF:TextControl x:Name="textControl1"
                     SectionChanged="TextControl1_SectionChanged" />
</DockPanel>
```

The handlers insert a landscape section at the input position, open the page setup, show the current section and set the page format of the document:

```csharp
using System;
using System.Windows;
using TXTextControl;

namespace SampleApp;

public partial class MainWindow : Window
{
    private void InsertLandscapeSection_Click(object sender, RoutedEventArgs e)
    {
        // A section break at the input position; the new section begins on a new page
        if (textControl1.Sections.Add(SectionBreakKind.BeginAtNewPage))
        {
            // The input position is now in the new section
            Section section = textControl1.Sections.GetItem();
            section.Format.Landscape = true;
        }
    }

    private void PageSetup_Click(object sender, RoutedEventArgs e)
    {
        // 0 opens the tab with margins, page size and orientation
        textControl1.SectionFormatDialog(0);
    }

    private void TextControl1_SectionChanged(object sender, EventArgs e)
    {
        // Sections are numbered from 1
        sectionText.Text = $"Section {textControl1.InputPosition.Section} of {textControl1.Sections.Count}";
    }

    private void SetDocumentPageFormat()
    {
        // A4 in 1/96 inch, the default unit of PageUnit
        textControl1.PageSize = new PageSize(793.7, 1122.5);
        textControl1.PageMargins = new PageMargins(96, 96, 96, 96);
    }
}
```

The code works as follows:

- [TextControl.Sections](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/sections-property/llms-full.txt) returns the [SectionCollection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectioncollection/llms-full.txt). [Add](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectioncollection/add-method/llms-full.txt) inserts a section break at the input position with `BeginAtNewPage` or `BeginAtNewLine` from [SectionBreakKind](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionbreakkind/enumeration/llms-full.txt) and returns `true` on success.
- [SectionCollection.GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectioncollection/getitem-method/llms-full.txt) without a parameter returns the section with the input position. [Section.Format](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/section/format-property/llms-full.txt) is its [SectionFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/llms-full.txt), and [Landscape](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/landscape-property/llms-full.txt) turns its pages; later sections inherit the orientation.
- [SectionChanged](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/sectionchanged-event/llms-full.txt) occurs when the input position moves to another section. Its handler takes a plain `EventArgs`, not a `RoutedEventArgs`. [InputPosition.Section](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/inputposition/section-property/llms-full.txt) is the section number of the input position.
- [TextControl.PageSize](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/pagesize-property/llms-full.txt) takes a [PageSize](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/pagesize/llms-full.txt) and [PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/pagemargins-property/llms-full.txt) a [PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/pagemargins/llms-full.txt) with left, top, right and bottom. In WPF, both are measured in device-independent units of 1/96 inch by default, the unit of [TextControl.PageUnit](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/pageunit-property/llms-full.txt); A4 is 793.7 × 1122.5 units, and 96 units are 1 inch.

[PageFormatChanged](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/pageformatchanged-event/llms-full.txt) occurs when the page settings change, and [TextControl.Landscape](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/landscape-property/llms-full.txt) sets the orientation of the document.

### Format a Section

The properties of `SectionFormat`:

| Property | Sets |
|---|---|
| [Landscape](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/landscape-property/llms-full.txt) | Landscape or portrait orientation |
| [PageSize](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/pagesize-property/llms-full.txt), [PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/pagemargins-property/llms-full.txt) | Paper size, always as the portrait values, and margins, in the unit of `PageUnit` |
| [BreakKind](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/breakkind-property/llms-full.txt) | A new page, the default, or a new line at the start of the section |
| [Columns](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/columns-property/llms-full.txt), [ColumnLineWidth](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/columnlinewidth-property/llms-full.txt) | The number of page columns, 1 by default, and a line between them in twips |
| [PageBorder](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/pageborder-property/llms-full.txt) | A [PageBorder](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/pageborder/llms-full.txt) around the pages |
| [RestartPageNumbering](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/sectionformat/restartpagenumbering-property/llms-full.txt) | Page numbers that start again in this section |

[Selection.SectionFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/sectionformat-property/llms-full.txt) gets and sets the page settings of the selected text; `Columns` returns 0 when the selection covers sections with different settings. Columns, margins and page borders in code are shown in [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt), and removing all section breaks in the blog article [How to Delete All Section Breaks in a DOCX Document using C#](https://www.textcontrol.com/blog/2023/11/21/how-to-delete-all-section-breaks-in-a-docx-document-using-csharp/).

### Next Steps

- [Headers and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/llms-full.txt): headers per section and restarted page numbers.
- [Tables and Table Formulas](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/tables/llms-full.txt): a wide table for the landscape section.
- [Printing and Print Preview](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/llms-full.txt): print the pages.
- [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt): sections with `ServerTextControl`.

---

## Nearby

- **Previous:** [Headers and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/llms-full.txt)
- **Next:** [Footnotes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/footnotes/llms-full.txt)
