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
PageSetupproperties on aSectionyou just created (afterInsertBreak) affects the content that comes after the break, not before it. - Use
SectionBreakContinuouswhen you only need a page-setup change (for example, switching to multi-column text) without starting a new page; useSectionBreakNewPagewhen 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.DifferentFirstPageHeaderFooterandOddAndEvenPagesHeaderFooteronly take effect if the correspondingHeaderFooterTypevariants (HeaderFirst,HeaderEven, and so on) actually exist inSection.HeadersFooters– enable the flag and add the corresponding header/footer together.Section.Bodyand eachHeaderFooterare separateStoryinstances –Range.Fields, bookmarks, and other range-scoped operations on the section’sBodydo not automatically reach into its headers and footers.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| New section’s page setup doesn’t match what I set | PageSetup was set on the wrong Section instance | Set 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 section | The new section’s headers/footers are still linked to the previous section | Call HeaderFooterCollection.LinkToPrevious(false) (or add distinct header/footer content) to unlink them |
| First-page or even-page header/footer is ignored | DifferentFirstPageHeaderFooter / OddAndEvenPagesHeaderFooter is false, or the matching HeaderFooterType variant doesn’t exist | Set the flag on PageSetup and ensure the corresponding header/footer variant is present |
| Multi-column layout doesn’t apply | TextColumns.SetCount() was called with a count of 1, or on the wrong section’s PageSetup | Call 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 PageSetup –
TopMargin, 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 / Enum | Description |
|---|---|
Section | A section node; Body, HeadersFooters, PageSetup |
SectionCollection | Every Section in the document, via Document.Sections |
Body / Story | Main content container for a section (Body) or header/footer (HeaderFooter) |
PageSetup | Page dimensions, margins, orientation, borders, and columns for a section |
HeaderFooter / HeaderFooterCollection | Header/footer content and the typed accessors for a section |
HeaderFooterType | Identifies which header/footer variant (primary, first, even) a HeaderFooter is |
TextColumn / TextColumnCollection | Multi-column text layout for a section |
PaperSize / Orientation / Margins | Page size, orientation, and preset margin values |
PageBorderAppliesTo / PageBorderDistanceFrom | Which pages a page border prints on, and its reference edge |
SectionStart / BreakType | Section-break type recorded on a section, and the break kinds DocumentBuilder.InsertBreak() accepts |
LineNumberRestartMode | When automatic line numbering restarts within a section |