TX Text Control .NET for WPF

Sections and Page Layout

Topic

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, with a TextControl named textControl1 and SampleApp replaced by your namespace. Headers and Footers covers the headers and footers of a section, and Sections, Pages, Headers, and Footers sections in documents processed without a user interface.

What a Section Is

A Section 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.

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 connected through the RibbonPageLayoutTab 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.

SectionFormatDialog 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:

<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:

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:

PageFormatChanged occurs when the page settings change, and TextControl.Landscape sets the orientation of the document.

Format a Section

The properties of SectionFormat:

PropertySets
LandscapeLandscape or portrait orientation
PageSize, PageMarginsPaper size, always as the portrait values, and margins, in the unit of PageUnit
BreakKindA new page, the default, or a new line at the start of the section
Columns, ColumnLineWidthThe number of page columns, 1 by default, and a line between them in twips
PageBorderA PageBorder around the pages
RestartPageNumberingPage numbers that start again in this section

Selection.SectionFormat 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, and removing all section breaks in the blog article How to Delete All Section Breaks in a DOCX Document using C#.

Next Steps