Sections of the document are represented by the Section and SectionCollection classes. Section objects are immediate children of the Document node and can be accessed via the Document.Sections property.
Each section is represented by a Section object that can be obtained from the Document.Sections collection by the index.
Example
Shows how to access a section at the specified index.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
Section section = doc.getSections().get(0);
The Document object provides the section collection that can be accessed by using Document.Sections. This returns a SectionCollection object containing the document’s sections. You can then use the SectionCollection.Add method on this object to add a section to the end of the document.
Example
Shows how to add a section to the end of the document.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
Section sectionToAdd = new Section(doc);
doc.getSections().add(sectionToAdd);
The section that is to be added or inserted must not belong to an existing document. It must be either cloned from another section or removed from a document before it can be added.
In the same way as discussed above, the document’s sections are retrieved by using Document.Sections. You can then use SectionCollection.Remove(Node) to remove a specified section or SectionCollection.RemoveAt to remove a section at the specified index.
Example
Shows how to remove a section at the specified index.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
doc.getSections().removeAt(0);
In addition, you can use SectionCollection.Clear to remove all the sections from the document.
Example
Shows how to remove all sections from a document.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
doc.getSections().clear();
If you want to copy and insert just the main text of a section excluding the section separator and section properties, use Section.PrependContent or Section.AppendContent passing a Section object for the content being copied. No new section is created, headers and footers are not copied. The former method inserts a copy of the content at the beginning of the section, while the latter inserts a copy of the content at the end of the section.
Example
Shows how to append content of an existing section. The number of sections in the document remains the same.
[Java]
Document doc = new Document(getMyDir() + "Section.AppendContent.doc");
// This is the section that we will append and prepend to.
Section section = doc.getSections().get(2);
// This copies content of the 1st section and inserts it at the beginning of the specified section.
Section sectionToPrepend = doc.getSections().get(0);
section.prependContent(sectionToPrepend);
// This copies content of the 2nd section and inserts it at the end of the specified section.
Section sectionToAppend = doc.getSections().get(1);
section.appendContent(sectionToAppend);
To delete the main text of a section, use Section.ClearContent.
Example
Shows how to delete main content of a section.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
Section section = doc.getSections().get(0);
section.clearContent();
To delete the headers and footers in a section, call Section.ClearHeadersFooters.
Example
Clears content of all headers and footers in a section.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
Section section = doc.getSections().get(0);
section.clearHeadersFooters();
Use the Section.Clone method to create a duplicate of a particular section.
Example
Shows how to create a duplicate of a particular section.
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
Section cloneSection = doc.getSections().get(0).deepClone();
Fully or partially copying one document into another is a very popular task. Here is a "pattern" to implement this.
Before any node from another document can be inserted, it must be imported using Document.ImportNode. The Document.ImportNode method makes a copy of the original node and updates all internal document-specific attributes such as lists and styles to make them valid in the destination document.
Example
Shows how to copy sections between documents.
[Java]
Document srcDoc = new Document(getMyDir() + "Document.doc");
Document dstDoc = new Document();
Section sourceSection = srcDoc.getSections().get(0);
Section newSection = (Section)dstDoc.importNode(sourceSection, true);
dstDoc.getSections().add(newSection);
Sometimes it is necessary to avoid section breaks in the destination document. In this case, you can use Section.AppendContent instead of SectionCollection.Add.