Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Manage Sections

    • 10 minutes to read

    A section is a part of a document with its own page layout settings: page size and orientation, margins, text columns, page and line numbering, page borders, headers, and footers. Split a document into sections to apply different page layouts in the same document (for example, to display a wide table on a landscape page).

    The WordDocument.Sections collection contains Section objects in document order. A new document contains a single section.

    A section break ends each section except the last section. The Paragraph.Mark.Type property specifies whether a paragraph ends with a paragraph mark or a section break. The last section ends at the end of the document body.

    Add Sections

    To start a new section after a paragraph, set the Paragraph.Mark.Type property to one of the following section break types:

    A section break splits a section into two sections. The source section specifies layout settings of both sections.

    If you set the Mark.Type property for the last paragraph in the document body, the section break takes effect when you add a block after this paragraph.

    To insert a section at a specific position, set the Mark.Type property of a new Paragraph object to a section break type and pass the paragraph to the DocumentFragment.Insert(index, block) method.

    The following code snippet adds two paragraphs separated by a next-page section break, applies landscape orientation to the second section, and saves the document to a DOCX file:

    using System.IO;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument();
    
    var summary = document.Body.AddParagraph("Quarterly Report Summary");
    summary.Mark.Type = ParagraphMarkType.SectionBreakNextPage;
    document.Body.AddParagraph("Sales by Region");
    
    document.Sections[1].Page.Orientation = PageOrientation.Landscape;
    
    Directory.CreateDirectory("output");
    using var stream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(stream, DocumentFormat.Docx);
    

    Access Sections

    Use the SectionCollection[index] indexer or iterate the WordDocument.Sections collection to access sections.

    The Section.Range property returns the section range in the document body. To get section text, pass this range to the DocumentFragment.GetText(range) method.

    The following code snippet writes the page orientation and text length of each section to the console:

    var body = document.Body;
    for (var i = 0; i < document.Sections.Count; i++) {
        var section = document.Sections[i];
        var text = body.GetText(section.Range);
        Console.WriteLine($"Section {i + 1}: {section.Page.Orientation}, {text.Length} characters");
    }
    

    Remove Sections

    To remove a section break, set the Paragraph.Mark.Type property of the paragraph that ends the section to ParagraphMarkType.ParagraphMark. If you remove the paragraph that ends a section, the API also removes the section break.

    When you remove a section break, content before the break moves to the next section and gets the next section’s settings.

    The following code snippet removes all section breaks from the document body:

    foreach (var block in document.Body) {
        if (block is Paragraph paragraph && paragraph.Mark.Type != ParagraphMarkType.ParagraphMark)
            paragraph.Mark.Type = ParagraphMarkType.ParagraphMark;
    }
    

    Specify Page Size and Orientation

    Use the Section.Page property to access page settings for a section.

    To apply a predefined paper size, set the SectionPage.PaperKind property to a DXPaperKind value (for example, DXPaperKind.A4). The page keeps the current orientation.

    To specify a custom page size, use SectionPage.Width and SectionPage.Height properties. If the size matches a predefined paper size, the PaperKind property gets the corresponding DXPaperKind value.

    To switch between portrait and landscape orientation, set the SectionPage.Orientation property. The API swaps the page width and height.

    SectionPage.FirstPageTray and SectionPage.OtherPagesTray properties specify printer paper trays for the first page and other pages in the section.

    Page sizes, margins, column widths, border thickness, and border offsets are in document units (1/300 of an inch). The page width and height range is from 30 to 6600 (from 0.1 to 22 inches).

    Specify Page Margins

    Use the Section.Margins property to access margin settings for a section:

    Properties Description
    SectionMargins.Left, SectionMargins.Right, SectionMargins.Top, SectionMargins.Bottom Specify page margins.
    SectionMargins.Gutter, SectionMargins.GutterPosition Specify extra space for binding.
    SectionMargins.HeaderOffset, SectionMargins.FooterOffset Specify the distance from the page edge to the header and footer.

    To mirror margins on facing pages or place the gutter at the top of pages, use WordDocument.MirrorMargins and WordDocument.GutterAtTop properties. These settings apply to all sections in the document.

    Margins and the gutter must leave space for text on the page. Otherwise, the API throws an ArgumentException. The API also checks margins when you modify page size or orientation.

    The following code snippet applies A4 paper in landscape orientation to the first section and specifies 0.75-inch margins and a 0.5-inch gutter:

    using DevExpress.Docs.Word;
    using DevExpress.Drawing.Printing;
    // ...
    
    var section = document.Sections[0];
    
    // Specify A4 paper in landscape orientation.
    section.Page.PaperKind = DXPaperKind.A4;
    section.Page.Orientation = PageOrientation.Landscape;
    
    // Specify 0.75-inch margins and a 0.5-inch gutter.
    section.Margins.Left = 225;
    section.Margins.Right = 225;
    section.Margins.Top = 225;
    section.Margins.Bottom = 225;
    section.Margins.Gutter = 150;
    

    Create Text Columns

    Use the Section.Columns property to access the section’s column layout. Call one of the following methods to create columns:

    Method Description
    SectionColumns.SetUniformColumns(count, spacing) Creates columns of equal width with the specified spacing. The API calculates column widths from the text width on the page. If you modify the page size or margins, the API recalculates column widths.
    SectionColumns.SetUniformColumns(count) Creates columns of equal width and keeps the current spacing between columns.
    SectionColumns.SetColumns(columns) Creates columns with the specified widths. Each SectionColumn value specifies the column width and spacing after the column. Column widths do not change if you modify the page size or margins.

    A section can contain up to 45 columns. The total width of columns and spacing must not exceed the text width on the page.

    Set the SectionColumns.ShowSeparator property to true to display a vertical line between columns. To get the current layout, use the SectionColumns.IsUniform property and the SectionColumns[index] indexer.

    To move text to the next column, add a ColumnBreak object to a paragraph.

    The following code snippet creates three columns of equal width with a separator line, and then replaces them with two columns of different widths:

    var columns = document.Sections[0].Columns;
    
    // Create three columns of equal width with 0.25-inch spacing.
    columns.SetUniformColumns(3, 75);
    columns.ShowSeparator = true;
    
    // Create a 4-inch column and a 2-inch column with 0.5-inch spacing.
    columns.SetColumns(new SectionColumn(1200, 150), new SectionColumn(600));
    

    Refer to the following help topic for additional information: Manage Text and Breaks.

    Number Pages

    Use the Section.PageNumbering property to access page numbering settings for a section:

    Property Description
    SectionPageNumbering.StartNumber Specifies the first page number in the section. If the StartNumber property is null, the section continues page numbering from the previous section.
    SectionPageNumbering.Format Specifies the page number format (for example, Arabic or Roman numerals).

    To display page numbers, add a PAGE field to a section header or footer.

    The following code snippet restarts page numbering in the second section, applies the lowercase Roman numeral format, and displays the page number in the section footer:

    var section = document.Sections[1];
    
    // Restart page numbering at 1 and display lowercase Roman numerals.
    section.PageNumbering.StartNumber = 1;
    section.PageNumbering.Format = NumberingFormat.LowerRoman;
    
    // Display the page number in the footer.
    var footer = new Footer();
    footer.Add(new Field("PAGE"));
    section.Footers[HeaderFooterType.Odd] = footer;
    

    Refer to the following help topics for additional information:

    Number Lines

    Use the Section.LineNumbering property to access line numbering settings for a section:

    Property Description
    CountBy Specifies the line number interval (for example, if the CountBy property is 5, the section displays a number for every fifth line). If the CountBy property is 0, the section does not display line numbers.
    Start Specifies the first line number.
    Distance Specifies the distance between line numbers and text. If the Distance property is null, the API calculates the distance automatically.
    Restart Specifies when line numbering restarts.

    To exclude a paragraph from line numbering, set the ParagraphProperties.SuppressLineNumbers property to true.

    The following code snippet numbers every fifth line in the first section and restarts line numbering in each section:

    var lineNumbering = document.Sections[0].LineNumbering;
    lineNumbering.CountBy = 5;
    lineNumbering.Restart = LineNumberingRestart.NewSection;
    

    Add Page Borders

    Use the Section.PageBorders property to access page border settings for a section.

    To add a border, assign a PageBorder object to the SectionPageBorders.Left, SectionPageBorders.Top, SectionPageBorders.Right, or SectionPageBorders.Bottom property. Pass the line style, thickness, color, and offset to the PageBorder constructor. To remove a border, set the corresponding property to null. The SectionPageBorders.Reset() method removes all page borders in the section.

    Use the following SectionPageBorders properties to specify border layout:

    Property Description
    SectionPageBorders.AppliesTo Specifies section pages that display borders.
    SectionPageBorders.OffsetFrom Specifies whether the API measures the border offset from text or from the page edge.
    SectionPageBorders.InFrontOfText Specifies whether page borders appear in front of text.

    Note

    Art page borders are not currently supported. If a loaded document contains an art border, the corresponding SectionPageBorders property gets null. If you set this property to null, the API removes the art border.

    The following code snippet adds a double blue border to the first page of the first section:

    using System.Drawing;
    using DevExpress.Docs;
    using DevExpress.Docs.Word;
    // ...
    
    var borders = document.Sections[0].PageBorders;
    var border = new PageBorder(BorderLineStyle.Double, 6, new OfficeColor(Color.SteelBlue), 100);
    borders.Left = border;
    borders.Top = border;
    borders.Right = border;
    borders.Bottom = border;
    borders.AppliesTo = PageBorderAppliesTo.FirstPage;
    borders.OffsetFrom = PageBorderOffsetFrom.Page;
    

    Add Headers and Footers

    Each section has separate headers and footers. Use Section.Headers and Section.Footers properties to access section headers and footers.

    Note

    To display a different header and footer on the first page of a section, set the Section.DifferentFirstPage property to true. To display different headers and footers on odd and even pages, set the WordDocument.DifferentOddEvenPages property to true.

    Refer to the following help topic for additional information: Manage Headers and Footers.