Working with Sections

Working with Sections

A Word document is divided into one or more sections, each with its own page setup, margins, headers, and footers. Aspose.Words FOSS for .NET represents a section as a Section node, containing a Body and, optionally, headers and footers, plus a PageSetup that controls its layout. This page covers the section object model, page setup, headers and footers, and multi-column text layout.


The Section Object Model

Document.Sections returns a SectionCollection – every Section in the document, each a CompositeNode and part of the node tree. A Section contains one Body (a Story holding the section’s main paragraphs and tables) and, when present, HeaderFooter nodes reachable through Section.HeadersFooters, a HeaderFooterCollection. Document.FirstSection and Document.LastSection give direct access to the boundary sections. Section.AppendContent(), PrependContent(), and ClearContent() manipulate a section’s body content in bulk, while ClearHeadersFooters() and DeleteHeaderFooterShapes() clear header/footer content.


Creating Sections and Section Breaks

DocumentBuilder.InsertBreak(BreakType) inserts a break at the cursor; BreakType includes SectionBreakNewPage, SectionBreakContinuous, SectionBreakNewColumn, SectionBreakEvenPage, and SectionBreakOddPage for starting a new section, alongside the non-section values PageBreak, ColumnBreak, LineBreak, and ParagraphBreak. DocumentBuilder.MoveToSection() moves the cursor to the beginning of a specific section’s body. PageSetup.SectionStart reads or sets the SectionStart break type recorded on an existing section.


Page Setup

Section.PageSetup is a PageSetup instance covering page dimensions and margins (PageWidth, PageHeight, TopMargin, BottomMargin, LeftMargin, RightMargin, Gutter), Orientation and PaperSize, and per-side page borders (Borders, BorderAppliesTo via PageBorderAppliesTo, BorderDistanceFrom via PageBorderDistanceFrom, BorderAlwaysInFront, BorderSurroundsHeader, BorderSurroundsFooter). Line-numbering is controlled by LineNumberRestartMode, LineNumberCountBy, LineNumberDistanceFromText, and LineStartingNumber. PageSetup.ClearFormatting() resets it to defaults.


Headers and Footers

Section.HeadersFooters is a HeaderFooterCollection with typed accessors – HeaderPrimary, HeaderFirst, HeaderEven, FooterPrimary, FooterFirst, FooterEven – alongside GetByHeaderFooterType(), Add(), and LinkToPrevious(). Each HeaderFooter is a Story (like Body) identified by its HeaderFooterType and flagged IsHeader / IsLinkedToPrevious. PageSetup.DifferentFirstPageHeaderFooter and PageSetup.OddAndEvenPagesHeaderFooter control whether Word expects the first-page and even-page header/footer variants to be used at all.


Multiple Text Columns

PageSetup.TextColumns returns a TextColumnCollection (Count, EvenlySpaced, LineBetween, Spacing, Width) with SetCount() to lay the section’s body out in multiple columns. Individual columns are TextColumn instances (Width, SpaceAfter) once the collection is populated with more than one column.


Tips and Best Practices

  • A section break carries the formatting for the section that follows it – setting PageSetup properties on a Section you just created (after InsertBreak) affects the content that comes after the break, not before it.
  • Use SectionBreakContinuous when you only need a page-setup change (for example, switching to multi-column text) without starting a new page; use SectionBreakNewPage when the new section should always start on a fresh page.
  • HeaderFooterCollection.LinkToPrevious() reconnects a section’s header/footer to inherit the previous section’s content – do this before writing new header/footer content if you want the new section to start independent from the one before it.
  • PageSetup.DifferentFirstPageHeaderFooter and OddAndEvenPagesHeaderFooter only take effect if the corresponding HeaderFooterType variants (HeaderFirst, HeaderEven, and so on) actually exist in Section.HeadersFooters – enable the flag and add the corresponding header/footer together.
  • Section.Body and each HeaderFooter are separate Story instances – Range.Fields, bookmarks, and other range-scoped operations on the section’s Body do not automatically reach into its headers and footers.

Common Issues

IssueCauseFix
New section’s page setup doesn’t match what I setPageSetup was set on the wrong Section instanceSet PageSetup on the section that follows the break, not the one before it
Header/footer content from the previous section unexpectedly appears in a new sectionThe new section’s headers/footers are still linked to the previous sectionCall HeaderFooterCollection.LinkToPrevious(false) (or add distinct header/footer content) to unlink them
First-page or even-page header/footer is ignoredDifferentFirstPageHeaderFooter / OddAndEvenPagesHeaderFooter is false, or the matching HeaderFooterType variant doesn’t existSet the flag on PageSetup and ensure the corresponding header/footer variant is present
Multi-column layout doesn’t applyTextColumns.SetCount() was called with a count of 1, or on the wrong section’s PageSetupCall SetCount() with more than one column on the target section’s PageSetup.TextColumns

FAQ

How do I start a new section without starting a new page?

Insert a SectionBreakContinuous break with DocumentBuilder.InsertBreak(BreakType.SectionBreakContinuous).

How do I give a section its own margins and orientation?

Insert a section break, then set the properties on the new section’s PageSetupTopMargin, BottomMargin, LeftMargin, RightMargin, Orientation, and PaperSize.

How do I make a section’s first page use a different header?

Set PageSetup.DifferentFirstPageHeaderFooter = true on that section, and add a HeaderFooter of type HeaderFirst (and FooterFirst, if needed) to its HeadersFooters collection.

How do I lay out a section’s body text in two columns?

Call PageSetup.TextColumns.SetCount(2) on the section, then adjust Spacing and EvenlySpaced / individual TextColumn.Width values as needed.


API Reference Summary

Class / EnumDescription
SectionA section node; Body, HeadersFooters, PageSetup
SectionCollectionEvery Section in the document, via Document.Sections
Body / StoryMain content container for a section (Body) or header/footer (HeaderFooter)
PageSetupPage dimensions, margins, orientation, borders, and columns for a section
HeaderFooter / HeaderFooterCollectionHeader/footer content and the typed accessors for a section
HeaderFooterTypeIdentifies which header/footer variant (primary, first, even) a HeaderFooter is
TextColumn / TextColumnCollectionMulti-column text layout for a section
PaperSize / Orientation / MarginsPage size, orientation, and preset margin values
PageBorderAppliesTo / PageBorderDistanceFromWhich pages a page border prints on, and its reference edge
SectionStart / BreakTypeSection-break type recorded on a section, and the break kinds DocumentBuilder.InsertBreak() accepts
LineNumberRestartModeWhen automatic line numbering restarts within a section

See Also