Skip to main content

Sections in Word Documents

  • 4 minutes to read

Divide a Document into Sections

The Document.Sections property provides access to the document’s section collection. Use the following methods to insert a section in code:

Specify the Section.StartType property to define the section break’s type. The RichEditDocumentServer supports the following section breaks:

  • Next Page - starts the new section on the following page.
  • Continuous - starts the new section on the same page.
  • Even Page - starts a new section on the next even-numbered page.
  • Odd Page - starts a new section on the next odd-numbered page.
  • Column - starts a new section on the next column on the page.

The code sample below shows how to insert continuous section break after the specific paragraph:

Document document = wordProcessor.Document;
document.LoadDocument("Documents\\Grimm.docx");
document.InsertSection(document.Paragraphs[4].Range.End);
Section insertedSection = document.GetSection(targetPosition);
insertedSection.StartType = SectionStartType.Continuous;

Use the RichEditBehaviorOptions.PageBreakInsertMode property to specify whether a page break is inserted next to the specified position or in the new line.

Define Page Formatting Options

Each section has page formatting options. You can use properties available in the SectionPage class to specify the page’s paper type, orientation, margins, etc. Use the Section.Page property to access the SectionPage object.

The code sample below specifies the first section’s page formatting options: sets the portrait orientation and the A3 Extra paper size.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.Drawing.Printing;

Section firstSection =  wordProcessor.Document.Sections[0];
firstSection.Page.Landscape = false;
firstSection.Page.PaperKind = DXPaperKind.A3Extra;

Line Numbering

Set the SectionLineNumbering.CountBy property to a non-zero value to enable line numbering. Refer to the following topic for more information: How to: Add Line Numbering

Columns

Call the SectionColumns.CreateUniformColumns method to create a multi-column layout. Refer to the following topic for more information: How to: Create a Three-Column Layout with Uniform Columns

Headers and Footers

Headers and footers in the document belong to a particular section. You can use the Section.BeginUpdateHeader - Section.EndUpdateHeader and the Section.BeginUpdateFooter - Section.EndUpdateHeader method pairs to edit header or footer content. Refer to the following topic for more information: Headers and Footers

When you create a new section, it’s automatically linked to the previous section and the content from the first section header is automatically inserted in the second section header. Remove this content before inserting a new content to the header. Call the SubDocument.Delete() method to clear the header.

Page Numbering

Each document section has its own page numbering settings (page number format, initial number, etc.). Use the Section.PageNumbering property to access the SectionPageNumbering class properties and specify section’s page numbering options. You should reset the numbering options by setting the SectionPageNumbering.ContinueNumbering property to false before you apply new options.

The PAGE field represents a document’s page number. To apply new settings to existing page numbers, call the Document.UpdateAllFields() method to update all fields.

The code sample below specifies the initial number and the NumberingFormat.CardinalText numbering format, and inserts the PAGE field to the section footer.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.CreateNewDocument();
    Section section = wordProcessor.Document.Sections[0];
    section.PageNumbering.ContinueNumbering = false;
    section.PageNumbering.FirstPageNumber = 3;
    section.PageNumbering.NumberingFormat = NumberingFormat.CardinalText;

    var footer = section.BeginUpdateFooter();
    footer.Fields.Create(footer.Range.End, "PAGE");
    section.EndUpdateFooter(footer);

    wordProcessor.Document.UpdateAllFields();
}

Footnote and Endnote Options

You can specify footnote and endnote options (format, position and layout) for each document section. The Section.FootnoteOptions provides access to the footnote options, the Section.EndnoteOptions property retrieves the endnote options. Refer to the following topic for more information: Footnotes and Endnotes

Section Protection

You can protect certain sections in your document. To do this, use the ProtectedForForms property.

The code snippet below loads a document with two sections, turns on form protection for the first section in the document, unprotects the second section, and saves the updated document.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

RichEditDocumentServer wordProcessor = new RichEditDocumentServer();
wordProcessor.LoadDocument(@"C:\DocumentWithTwoSections.docx", DocumentFormat.OpenXml);
wordProcessor.Document.Sections[0].ProtectedForForms = true;
wordProcessor.Document.Sections[1].ProtectedForForms = false;
wordProcessor.Document.Protect("", DocumentProtectionType.FillInForms);
wordProcessor.SaveDocument("DocumentWithTwoSectionsProtected.docx", DocumentFormat.OpenXml);