public class SectionCollection extends NodeCollection
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.
Examples:
Shows how to add and remove sections in a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 2");
Assert.assertEquals("Section 1\fSection 2", doc.getText().trim());
// Delete the first section from the document.
doc.getSections().removeAt(0);
Assert.assertEquals("Section 2", doc.getText().trim());
// Append a copy of what is now the first section to the end of the document.
int lastSectionIdx = doc.getSections().getCount() - 1;
Section newSection = doc.getSections().get(lastSectionIdx).deepClone();
doc.getSections().add(newSection);
Assert.assertEquals("Section 2\fSection 2", doc.getText().trim());
| Modifier and Type | Method and Description |
|---|---|
Node |
get(int index)
Retrieves a section at the given index.
|
Node[] |
toArray()
Copies all sections from the collection to a new array of sections.
|
add, clear, contains, getContainer, getCount, getCurrentNode, getNextMatchingNode, indexOf, insert, iterator, remove, removeAtpublic Node get(int index)
Remarks:
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
Examples:
Shows when to recalculate the page layout of the document.
Document doc = new Document(getMyDir() + "Rendering.docx");
// Saving a document to PDF, to an image, or printing for the first time will automatically
// cache the layout of the document within its pages.
doc.save(getArtifactsDir() + "Document.UpdatePageLayout.1.pdf");
// Modify the document in some way.
doc.getStyles().get("Normal").getFont().setSize(6.0);
doc.getSections().get(0).getPageSetup().setOrientation(Orientation.LANDSCAPE);
doc.getSections().get(0).getPageSetup().setMargins(Margins.MIRRORED);
// In the current version of Aspose.Words, modifying the document does not automatically rebuild
// the cached page layout. If we wish for the cached layout
// to stay up to date, we will need to update it manually.
doc.updatePageLayout();
doc.save(getArtifactsDir() + "Document.UpdatePageLayout.2.pdf");
Shows how to prepare a new section node for editing.
Document doc = new Document();
// A blank document comes with a section, which has a body, which in turn has a paragraph.
// We can add contents to this document by adding elements such as text runs, shapes, or tables to that paragraph.
Assert.assertEquals(NodeType.SECTION, doc.getChild(NodeType.ANY, 0, true).getNodeType());
Assert.assertEquals(NodeType.BODY, doc.getSections().get(0).getChild(NodeType.ANY, 0, true).getNodeType());
Assert.assertEquals(NodeType.PARAGRAPH, doc.getSections().get(0).getBody().getChild(NodeType.ANY, 0, true).getNodeType());
// If we add a new section like this, it will not have a body, or any other child nodes.
doc.getSections().add(new Section(doc));
Assert.assertEquals(0, doc.getSections().get(1).getChildNodes(NodeType.ANY, true).getCount());
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it.
doc.getLastSection().ensureMinimum();
Assert.assertEquals(NodeType.BODY, doc.getSections().get(1).getChild(NodeType.ANY, 0, true).getNodeType());
Assert.assertEquals(NodeType.PARAGRAPH, doc.getSections().get(1).getBody().getChild(NodeType.ANY, 0, true).getNodeType());
doc.getSections().get(0).getBody().getFirstParagraph().appendChild(new Run(doc, "Hello world!"));
Assert.assertEquals("Hello world!", doc.getText().trim());
get in class NodeCollectionindex - An index into the list of sections.Section value.public Node[] toArray()
toArray in class NodeCollection