DocumentSections Property

Returns a collection that represents all sections in the document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.9.0

Syntax

public SectionCollection Sections { get; }

Property Value

Type: SectionCollection

Examples

Shows how to add/remove sections in a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Section 1");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Write("Section 2");

// This shows what is in the document originally. The document has two sections
Assert.AreEqual("Section 1\x000cSection 2", doc.GetText().Trim());

// Delete the first section from the document
doc.Sections.RemoveAt(0);

// Duplicate the last section and append the copy to the end of the document
int lastSectionIdx = doc.Sections.Count - 1;
Section newSection = doc.Sections[lastSectionIdx].Clone();
doc.Sections.Add(newSection);

// Check what the document contains after we changed it
Assert.AreEqual("Section 2\x000cSection 2", doc.GetText().Trim());
Shows how to specify how the section starts, from a new page, on the same page or other.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Add text to the first section and that comes with a blank document,
// then add a new section that starts a new page and give it text as well
builder.Writeln("This text is in section 1.");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln("This text is in section 2.");

// Section break types determine how a new section gets split from the previous section
// By inserting a "SectionBreakNewPage" type section break, we have set this section's SectionStart value to "NewPage" 
Assert.AreEqual(SectionStart.NewPage, doc.Sections[1].PageSetup.SectionStart);

// Insert a new column section the same way
builder.InsertBreak(BreakType.SectionBreakNewColumn);
builder.Writeln("This text is in section 3.");

Assert.AreEqual(SectionStart.NewColumn, doc.Sections[2].PageSetup.SectionStart);

// We can change the types of section breaks by assigning different values to each section's SectionStart
// Setting their values to "Continuous" will put no visible breaks between sections
// and will leave all the content of this document on one page
doc.Sections[1].PageSetup.SectionStart = SectionStart.Continuous;
doc.Sections[2].PageSetup.SectionStart = SectionStart.Continuous;

doc.Save(ArtifactsDir + "PageSetup.SetSectionStart.docx");

See Also