How to Work with Sections in .NET
This guide shows how to create section breaks, configure page setup, and manage headers, footers, and multi-column layout in Aspose.Words FOSS for .NET, using the Section and PageSetup classes. It covers reading the sections already in a document, starting a new section with DocumentBuilder, configuring page dimensions and margins, adding header and footer variants, and laying out body text in multiple columns.
Step-by-Step Guide
Step 1: Install the Package
Aspose.Words FOSS for .NET does not yet publish a NuGet package – build the library from source instead. Clone https://github.com/aspose-words-foss/Aspose.Words-FOSS-for-.NET.git, then build Aspose.Words.sln in Release configuration with the .NET SDK and reference the resulting Aspose.Words assembly from your project. The library targets .NET Standard 2.0, so it runs on .NET Framework 4.6.2+ and .NET 6/8/10.
Step 2: Import Required Classes
Add a using Aspose.Words; directive – Section, PageSetup, HeaderFooter, and the related enums used in this guide all live directly in the Aspose.Words namespace alongside Document and DocumentBuilder.
Step 3: Read the Sections Already in a Document
Document.Sections returns a SectionCollection holding every Section in the document, each a CompositeNode that is also part of the overall node tree. Document.FirstSection and Document.LastSection give direct access to the boundary sections without iterating the collection. Each Section contains one Body – reached the same way as any other Story – and, when present, header and footer content reached through Section.HeadersFooters.
Step 4: Start a New Section with a Section Break
Call DocumentBuilder.InsertBreak(BreakType) at the cursor to start a new section. Pass BreakType.SectionBreakNewPage when the new section should always begin on a fresh page, BreakType.SectionBreakContinuous when only a page-setup change is needed (for example, switching to multi-column text) without forcing a page break, or BreakType.SectionBreakNewColumn, SectionBreakEvenPage, and SectionBreakOddPage for the other section-start behaviors Word supports. A section break carries the formatting for the section that follows it, so set PageSetup properties on the new Section – not the one before it – once the break is inserted. DocumentBuilder.MoveToSection(sectionIndex) moves the cursor to the start of a specific section’s body, and PageSetup.SectionStart reads or sets the SectionStart break type already recorded on an existing section.
Step 5: Configure Page Dimensions and Margins
Each Section.PageSetup is a PageSetup instance. Set PageWidth, PageHeight, LeftMargin, RightMargin, TopMargin, BottomMargin, and Gutter for page dimensions and margins, or assign a Margins preset directly to PageSetup.Margins. Orientation and PaperSize control page orientation and paper size. Page borders are configured with Borders (a BorderCollection), BorderAppliesTo (PageBorderAppliesTo), BorderDistanceFrom (PageBorderDistanceFrom), BorderAlwaysInFront, BorderSurroundsHeader, and BorderSurroundsFooter. Call PageSetup.ClearFormatting() to reset a section’s page setup back to defaults before configuring it from scratch.
Step 6: Add and Link Headers and Footers
Section.HeadersFooters is a HeaderFooterCollection with typed accessors – HeaderPrimary, HeaderFirst, HeaderEven, FooterPrimary, FooterFirst, FooterEven – alongside GetByHeaderFooterType() for lookup by HeaderFooterType and Add() to add a new one. Each HeaderFooter is a Story, flagged IsHeader and IsLinkedToPrevious. Enable PageSetup.DifferentFirstPageHeaderFooter or PageSetup.OddAndEvenPagesHeaderFooter before adding the corresponding HeaderFirst/FooterFirst or HeaderEven/FooterEven variant – the flag and the variant only take effect together. Call HeaderFooterCollection.LinkToPrevious() to reconnect a section’s header or footer to inherit the previous section’s content, or to unlink it so the new section starts with independent content.
Step 7: Lay Out Body Text in Multiple Columns
PageSetup.TextColumns returns a TextColumnCollection for the section. Call SetCount() with the number of columns the body text should flow into, then adjust EvenlySpaced, Spacing, LineBetween, and Width to control how the columns are laid out. Once the collection holds more than one column, individual TextColumn instances expose their own Width and SpaceAfter.
Step 8: Clear or Bulk-Edit Section Content
Section.AppendContent() and PrependContent() copy another section’s body content into this one; ClearContent() empties a section’s body in bulk. ClearHeadersFooters() and DeleteHeaderFooterShapes() clear header and footer content across the section without removing the section itself.
Common Issues and Fixes
A new section’s page setup doesn’t match what I set
PageSetup was set on the wrong Section instance. A section break carries formatting for the section that follows it – set PageSetup on the section that comes after InsertBreak(), not the one before it.
Header or footer content from the previous section unexpectedly appears in a new one
The new section’s header or footer is still linked to the previous section. Call HeaderFooterCollection.LinkToPrevious(false), or add distinct header/footer content to the new section, to unlink it.
First-page or even-page header/footer is ignored
DifferentFirstPageHeaderFooter or OddAndEvenPagesHeaderFooter is false, or the matching HeaderFooterType variant doesn’t exist in Section.HeadersFooters. Set the flag on PageSetup and add the corresponding header/footer variant together.
Multi-column layout doesn’t apply
TextColumns.SetCount() was called with a count of one, or on the wrong section’s PageSetup. Call SetCount() with more than one column on the target section’s PageSetup.TextColumns.
Section break inserted at the wrong document position
DocumentBuilder.InsertBreak(BreakType) inserts at the builder’s current cursor. Use DocumentBuilder.MoveToSection() or the builder’s other cursor-positioning members to position the cursor before calling InsertBreak().
Frequently Asked Questions
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 TopMargin, BottomMargin, LeftMargin, RightMargin, Orientation, and PaperSize on the new section’s PageSetup.
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, EvenlySpaced, or individual TextColumn.Width values as needed.
How do I access the first and last sections directly?
Read Document.FirstSection and Document.LastSection instead of indexing into Document.Sections.