SectionCollection class

SectionCollection class

A collection of Section objects in the document. To learn more, visit the Working with Sections documentation article.

Remarks

A Microsoft Word document can contain multiple sections. To create a section in a Microsoft Word, select the Insert/Break command and select a break type. The break specifies whether section starts on a new page or on the same page.

Programmatically inserting and removing sections can be used to customize documents produced during mail merge. If a document needs to have different content or parts of the content depending on some criteria, then you can create a “master” document that contains multiple sections and delete some of the sections before or after mail merge.

Inheritance: SectionCollectionNodeCollection

Properties

NameDescription
countGets the number of nodes in the collection.
(Inherited from NodeCollection)
this[]
(Inherited from NodeCollection)

Methods

NameDescription
add(node)Adds a node to the end of the collection.
(Inherited from NodeCollection)
clear()Removes all nodes from this collection and from the document.
(Inherited from NodeCollection)
contains(node)Determines whether a node is in the collection.
(Inherited from NodeCollection)
indexOf(node)Returns the zero-based index of the specified node.
(Inherited from NodeCollection)
insert(index, node)Inserts a node into the collection at the specified index.
(Inherited from NodeCollection)
remove(node)Removes the node from the collection and from the document.
(Inherited from NodeCollection)
removeAt(index)Removes the node at the specified index from the collection and from the document.
(Inherited from NodeCollection)
toArray()Copies all sections from the collection to a new array of sections.

Examples

Shows how to add and remove sections in a document.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

builder.write("Section 1");
builder.insertBreak(aw.BreakType.SectionBreakNewPage);
builder.write("Section 2");

expect(doc.getText().trim()).toEqual("Section 1\u000cSection 2");

// Delete the first section from the document.
doc.sections.removeAt(0);

expect(doc.getText().trim()).toEqual("Section 2");

// Append a copy of what is now the first section to the end of the document.
let lastSectionIdx = doc.sections.count - 1;
let newSection = doc.sections.at(lastSectionIdx).clone();
doc.sections.add(newSection);

expect(doc.getText().trim()).toEqual("Section 2\u000cSection 2");

See Also