DocumentFirstSection Property

Gets the first section in the document.

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

Syntax

public Section FirstSection { get; }

Property Value

Type: Section

Remarks

Returns null if there are no sections.

Examples

Shows how to replace text in the document footer.
// Open the template document, containing obsolete copyright information in the footer
Document doc = new Document(MyDir + "Footer.docx");

HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
HeaderFooter footer = headersFooters[HeaderFooterType.FooterPrimary];

FindReplaceOptions options = new FindReplaceOptions
{
    MatchCase = false,
    FindWholeWordsOnly = false
};

int currentYear = System.DateTime.Now.Year;
footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", $"Copyright (C) {currentYear} by Aspose Pty Ltd.", options);

doc.Save(ArtifactsDir + "HeaderFooter.ReplaceText.docx");
Shows how to create a new section with a document builder.
Document doc = new Document();

// A blank document contains one section by default,
// in order for us to be able to edit it straight away.
Assert.AreEqual(1, doc.Sections.Count);

// Use a document builder to add text, and then to create a new section by inserting a section break.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");
builder.InsertBreak(BreakType.SectionBreakNewPage);

Assert.AreEqual(2, doc.Sections.Count);

// Each section is a subdivision of the document that has its own page setup settings.
// We can split up the text in the second section into two columns without affecting the first section in any way.
doc.LastSection.PageSetup.TextColumns.SetCount(2);
builder.Writeln("Column 1.");
builder.InsertBreak(BreakType.ColumnBreak);
builder.Writeln("Column 2.");

Assert.AreEqual(1, doc.FirstSection.PageSetup.TextColumns.Count);
Assert.AreEqual(2, doc.LastSection.PageSetup.TextColumns.Count);

doc.Save(ArtifactsDir + "Section.Create.docx");
Shows how you can enumerate through children of a composite node and detect types of the children nodes.
// Open a document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Section 1");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Primary header");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Primary footer");

// Get the first section in the document
Section section = doc.FirstSection;

// A Section is a composite node and therefore can contain child nodes
// Section can contain only Body and HeaderFooter nodes
foreach (Node node in section)
{
    // Every node has the NodeType property
    switch (node.NodeType)
    {
        case NodeType.Body:
        {
            // If the node type is Body, we can cast the node to the Body class
            Body body = (Body) node;

            // Write the content of the main story of the section to the console
            Console.WriteLine("*** Body ***");
            Console.WriteLine(body.GetText());
            break;
        }
        case NodeType.HeaderFooter:
        {
            // If the node type is HeaderFooter, we can cast the node to the HeaderFooter class
            HeaderFooter headerFooter = (HeaderFooter) node;

            // Write the content of the header footer to the console
            Console.WriteLine("*** HeaderFooter ***");
            Console.WriteLine(headerFooter.HeaderFooterType);
            Console.WriteLine(headerFooter.GetText());
            break;
        }
        default:
        {
            // Other types of nodes never occur inside a Section node
            throw new Exception("Unexpected node type in a section.");
        }
    }
}

See Also