public class Body extends Story
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Remarks:
Body can contain Paragraph and Table child nodes.
Body is a section-level node and can only be a child of Section. There can only be one Body in a Section.
A minimal valid Body needs to contain at least one Paragraph.
Examples:
Shows how to construct an Aspose.Words document by hand.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// Set some page setup properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
body.appendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!", doc.getText().trim());
doc.save(getArtifactsDir() + "Section.CreateManually.docx");
| Constructor and Description |
|---|
Body(DocumentBase doc)
Initializes a new instance of the
Body class. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(DocumentVisitor visitor)
Accepts a visitor.
|
int |
acceptEnd(DocumentVisitor visitor)
Accepts a visitor for visiting the end of the document's body.
|
int |
acceptStart(DocumentVisitor visitor)
Accepts a visitor for visiting the start of the document's body.
|
void |
ensureMinimum()
If the last child is not a paragraph, creates and appends one empty paragraph.
|
int |
getNodeType()
Returns
NodeType.BODY. |
Section |
getParentSection()
Gets the parent section of this story.
|
appendParagraph, deleteShapes, getFirstParagraph, getLastParagraph, getParagraphs, getStoryType, getTablesacceptChildren, acceptCore, appendChild, coreRemoveSelfOnly, getChild, getChildNodes, getContainer, getCount, getCurrentNode, getFirstChild, getLastChild, getNextMatchingNode, getText, hasChildNodes, indexOf, insertAfter, insertBefore, isComposite, iterator, prependChild, removeAllChildren, removeChild, removeSmartTags, selectNodes, selectSingleNodedeepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getParentNode, getPreviousSibling, getRange, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic Body(DocumentBase doc)
Body class.
Remarks:
When Body is created, it belongs to the specified document, but is not yet part of the document and Node.getParentNode() is null.
To append Body to a Section use CompositeNode.appendChild(com.aspose.words.Node) CompositeNode.insertAfter(com.aspose.words.Node, com.aspose.words.Node) or CompositeNode.insertBefore(com.aspose.words.Node, com.aspose.words.Node) methods.
Examples:
Shows how to construct an Aspose.Words document by hand.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// Set some page setup properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
body.appendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!", doc.getText().trim());
doc.save(getArtifactsDir() + "Section.CreateManually.docx");
doc - The owner document.public int getNodeType()
NodeType.BODY.
Examples:
Shows how to iterate through the children of a composite node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Primary header");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("Primary footer");
Section section = doc.getFirstSection();
// A Section is a composite node and can contain child nodes,
// but only if those child nodes are of a "Body" or "HeaderFooter" node type.
for (Node node : section) {
switch (node.getNodeType()) {
case NodeType.BODY: {
Body body = (Body) node;
System.out.println("Body:");
System.out.println("\t\"{body.GetText().Trim()}\"");
break;
}
case NodeType.HEADER_FOOTER: {
HeaderFooter headerFooter = (HeaderFooter) node;
System.out.println("HeaderFooter type: {headerFooter.HeaderFooterType}:");
System.out.println("\t\"{headerFooter.GetText().Trim()}\"");
break;
}
default: {
throw new Exception("Unexpected node type in a section.");
}
}
}
getNodeType in class NodeNodeType.BODY. The returned value is one of NodeType constants.public Section getParentSection()
Remarks:
getParentSection() is equivalent to Node.getParentNode() casted to Section.
Examples:
Shows how to store endnotes at the end of each section, and modify their positions.
public void suppressEndnotes() throws Exception {
Document doc = new Document();
doc.removeAllChildren();
// By default, a document compiles all endnotes at its end.
Assert.assertEquals(EndnotePosition.END_OF_DOCUMENT, doc.getEndnoteOptions().getPosition());
// We use the "Position" property of the document's "EndnoteOptions" object
// to collect endnotes at the end of each section instead.
doc.getEndnoteOptions().setPosition(EndnotePosition.END_OF_SECTION);
insertSectionWithEndnote(doc, "Section 1", "Endnote 1, will stay in section 1");
insertSectionWithEndnote(doc, "Section 2", "Endnote 2, will be pushed down to section 3");
insertSectionWithEndnote(doc, "Section 3", "Endnote 3, will stay in section 3");
// While getting sections to display their respective endnotes, we can set the "SuppressEndnotes" flag
// of a section's "PageSetup" object to "true" to revert to the default behavior and pass its endnotes
// onto the next section.
PageSetup pageSetup = doc.getSections().get(1).getPageSetup();
pageSetup.setSuppressEndnotes(true);
doc.save(getArtifactsDir() + "PageSetup.SuppressEndnotes.docx");
}
/// <summary>
/// Append a section with text and an endnote to a document.
/// </summary>
private static void insertSectionWithEndnote(Document doc, String sectionBodyText, String endnoteText) {
Section section = new Section(doc);
doc.appendChild(section);
Body body = new Body(doc);
section.appendChild(body);
Assert.assertEquals(body.getParentNode(), section);
Paragraph para = new Paragraph(doc);
body.appendChild(para);
Assert.assertEquals(para.getParentNode(), body);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(para);
builder.write(sectionBodyText);
builder.insertFootnote(FootnoteType.ENDNOTE, endnoteText);
}
public boolean accept(DocumentVisitor visitor) throws java.lang.Exception
Remarks:
Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.
For more info see the Visitor design pattern.
Calls DocumentVisitor.visitBodyStart(com.aspose.words.Body), then calls Node.accept(com.aspose.words.DocumentVisitor) for all child nodes of the section and calls DocumentVisitor.visitBodyEnd(com.aspose.words.Body) at the end.
Examples:
Shows how to process absolute position tab characters with a document visitor.
public void documentToTxt() throws Exception {
Document doc = new Document(getMyDir() + "Absolute position tab.docx");
// Extract the text contents of our document by accepting this custom document visitor.
DocTextExtractor myDocTextExtractor = new DocTextExtractor();
Section fisrtSection = doc.getFirstSection();
fisrtSection.getBody().accept(myDocTextExtractor);
// Visit only start of the document body.
fisrtSection.getBody().acceptStart(myDocTextExtractor);
// Visit only end of the document body.
fisrtSection.getBody().acceptEnd(myDocTextExtractor);
// The absolute position tab, which has no equivalent in string form, has been explicitly converted to a tab character.
Assert.assertEquals("Before AbsolutePositionTab\tAfter AbsolutePositionTab", myDocTextExtractor.getText());
// An AbsolutePositionTab can accept a DocumentVisitor by itself too.
AbsolutePositionTab absPositionTab = (AbsolutePositionTab) doc.getFirstSection().getBody().getFirstParagraph().getChild(NodeType.SPECIAL_CHAR, 0, true);
myDocTextExtractor = new DocTextExtractor();
absPositionTab.accept(myDocTextExtractor);
Assert.assertEquals("\t", myDocTextExtractor.getText());
}
/// <summary>
/// Collects the text contents of all runs in the visited document. Replaces all absolute tab characters with ordinary tabs.
/// </summary>
public static class DocTextExtractor extends DocumentVisitor {
public DocTextExtractor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(final Run run) {
appendText(run.getText());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when an AbsolutePositionTab node is encountered in the document.
/// </summary>
public int visitAbsolutePositionTab(final AbsolutePositionTab tab) {
mBuilder.append("\t");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds text to the current output. Honors the enabled/disabled output flag.
/// </summary>
public void appendText(final String text) {
mBuilder.append(text);
}
/// <summary>
/// Plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private final StringBuilder mBuilder;
}
accept in class Nodevisitor - The visitor that will visit the nodes.DocumentVisitor stopped the operation before visiting all nodes.java.lang.Exceptionpublic int acceptStart(DocumentVisitor visitor) throws java.lang.Exception
Examples:
Shows how to process absolute position tab characters with a document visitor.
public void documentToTxt() throws Exception {
Document doc = new Document(getMyDir() + "Absolute position tab.docx");
// Extract the text contents of our document by accepting this custom document visitor.
DocTextExtractor myDocTextExtractor = new DocTextExtractor();
Section fisrtSection = doc.getFirstSection();
fisrtSection.getBody().accept(myDocTextExtractor);
// Visit only start of the document body.
fisrtSection.getBody().acceptStart(myDocTextExtractor);
// Visit only end of the document body.
fisrtSection.getBody().acceptEnd(myDocTextExtractor);
// The absolute position tab, which has no equivalent in string form, has been explicitly converted to a tab character.
Assert.assertEquals("Before AbsolutePositionTab\tAfter AbsolutePositionTab", myDocTextExtractor.getText());
// An AbsolutePositionTab can accept a DocumentVisitor by itself too.
AbsolutePositionTab absPositionTab = (AbsolutePositionTab) doc.getFirstSection().getBody().getFirstParagraph().getChild(NodeType.SPECIAL_CHAR, 0, true);
myDocTextExtractor = new DocTextExtractor();
absPositionTab.accept(myDocTextExtractor);
Assert.assertEquals("\t", myDocTextExtractor.getText());
}
/// <summary>
/// Collects the text contents of all runs in the visited document. Replaces all absolute tab characters with ordinary tabs.
/// </summary>
public static class DocTextExtractor extends DocumentVisitor {
public DocTextExtractor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(final Run run) {
appendText(run.getText());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when an AbsolutePositionTab node is encountered in the document.
/// </summary>
public int visitAbsolutePositionTab(final AbsolutePositionTab tab) {
mBuilder.append("\t");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds text to the current output. Honors the enabled/disabled output flag.
/// </summary>
public void appendText(final String text) {
mBuilder.append(text);
}
/// <summary>
/// Plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private final StringBuilder mBuilder;
}
acceptStart in class CompositeNodevisitor - The document visitor.VisitorAction constants.java.lang.Exceptionpublic int acceptEnd(DocumentVisitor visitor) throws java.lang.Exception
Examples:
Shows how to process absolute position tab characters with a document visitor.
public void documentToTxt() throws Exception {
Document doc = new Document(getMyDir() + "Absolute position tab.docx");
// Extract the text contents of our document by accepting this custom document visitor.
DocTextExtractor myDocTextExtractor = new DocTextExtractor();
Section fisrtSection = doc.getFirstSection();
fisrtSection.getBody().accept(myDocTextExtractor);
// Visit only start of the document body.
fisrtSection.getBody().acceptStart(myDocTextExtractor);
// Visit only end of the document body.
fisrtSection.getBody().acceptEnd(myDocTextExtractor);
// The absolute position tab, which has no equivalent in string form, has been explicitly converted to a tab character.
Assert.assertEquals("Before AbsolutePositionTab\tAfter AbsolutePositionTab", myDocTextExtractor.getText());
// An AbsolutePositionTab can accept a DocumentVisitor by itself too.
AbsolutePositionTab absPositionTab = (AbsolutePositionTab) doc.getFirstSection().getBody().getFirstParagraph().getChild(NodeType.SPECIAL_CHAR, 0, true);
myDocTextExtractor = new DocTextExtractor();
absPositionTab.accept(myDocTextExtractor);
Assert.assertEquals("\t", myDocTextExtractor.getText());
}
/// <summary>
/// Collects the text contents of all runs in the visited document. Replaces all absolute tab characters with ordinary tabs.
/// </summary>
public static class DocTextExtractor extends DocumentVisitor {
public DocTextExtractor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(final Run run) {
appendText(run.getText());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when an AbsolutePositionTab node is encountered in the document.
/// </summary>
public int visitAbsolutePositionTab(final AbsolutePositionTab tab) {
mBuilder.append("\t");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds text to the current output. Honors the enabled/disabled output flag.
/// </summary>
public void appendText(final String text) {
mBuilder.append(text);
}
/// <summary>
/// Plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private final StringBuilder mBuilder;
}
acceptEnd in class CompositeNodevisitor - The document visitor.VisitorAction constants.java.lang.Exceptionpublic void ensureMinimum()
Examples:
Clears main text from all sections from the document leaving the sections themselves.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// This body has no children, so we cannot add runs to it yet.
Assert.assertEquals(0, doc.getFirstSection().getBody().getChildNodes(NodeType.ANY, true).getCount());
// Call the "EnsureMinimum" to make sure that this body contains at least one empty paragraph.
body.ensureMinimum();
// Now, we can add runs to the body, and get the document to display them.
body.getFirstParagraph().appendChild(new Run(doc, "Hello world!"));
Assert.assertEquals("Hello world!", doc.getText().trim());