Detailed Description

Represents a container for the main text of a section.

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.

auto doc = MakeObject<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.
auto section = MakeObject<Section>(doc);
doc->AppendChild(section);
// Set some page setup properties for the section.
section->get_PageSetup()->set_SectionStart(SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(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.
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
auto para = MakeObject<Paragraph>(doc);
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(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.
auto run = MakeObject<Run>(doc);
run->set_Text(u"Hello World!");
run->get_Font()->set_Color(System::Drawing::Color::get_Red());
para->AppendChild(run);
ASSERT_EQ(u"Hello World!", doc->GetText().Trim());
doc->Save(ArtifactsDir + u"Section.CreateManually.docx");

#include <Aspose.Words.Cpp/Body.h>

+ Inheritance diagram for Aspose::Words::Body:

Public Member Functions

 Body (const SharedPtr< DocumentBase > &doc)
 Initializes a new instance of the Body class. More...
 
bool Accept (SharedPtr< DocumentVisitor > visitor) override
 Accepts a visitor. More...
 
void EnsureMinimum ()
 If the last child is not a paragraph, creates and appends one empty paragraph. More...
 
NodeType get_NodeType () const override
 Returns NodeType.Body. More...
 
SharedPtr< Sectionget_ParentSection ()
 Gets the parent section of this story. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
- Public Member Functions inherited from Story
SharedPtr< ParagraphAppendParagraph (const String &text)
 A shortcut method that creates a Paragraph object with optional text and appends it to the end of this object. More...
 
void DeleteShapes ()
 Deletes all shapes from the text of this story. More...
 
SharedPtr< Paragraphget_FirstParagraph ()
 Gets the first paragraph in the story. More...
 
SharedPtr< Paragraphget_LastParagraph ()
 Gets the last paragraph in the story. More...
 
SharedPtr< ParagraphCollectionget_Paragraphs ()
 Gets a collection of paragraphs that are immediate children of the story. More...
 
StoryType get_StoryType () const
 Gets the type of this story. More...
 
SharedPtr< TableCollectionget_Tables ()
 Gets a collection of tables that are immediate children of the story. More...
 
- Public Member Functions inherited from CompositeNode
SharedPtr< NodeAppendChild (const SharedPtr< Node > &newChild)
 Adds the specified node to the end of the list of child nodes for this node. More...
 
SharedPtr< NodeCollectionget_ChildNodes ()
 Gets all immediate child nodes of this node. More...
 
int32_t get_Count ()
 Gets the number of immediate children of this node. More...
 
SharedPtr< Nodeget_FirstChild () const
 Gets the first child of the node. More...
 
bool get_HasChildNodes ()
 Returns true if this node has any child nodes. More...
 
bool get_IsComposite () override
 Returns true as this node can have child nodes. More...
 
SharedPtr< Nodeget_LastChild () const
 Gets the last child of the node. More...
 
SharedPtr< NodeGetChild (NodeType nodeType, int32_t index, bool isDeep)
 Returns an Nth child node that matches the specified type. More...
 
SharedPtr< NodeCollectionGetChildNodes (NodeType nodeType, bool isDeep)
 Returns a live collection of child nodes that match the specified type. More...
 
SharedPtr< IEnumerator< SharedPtr< Node > > > GetEnumerator () override
 Provides support for the for each style iteration over the child nodes of this node. More...
 
String GetText () override
 Gets the text of this node and of all its children. More...
 
int32_t IndexOf (const SharedPtr< Node > &child)
 Returns the index of the specified child node in the child node array. More...
 
SharedPtr< NodeInsertAfter (const SharedPtr< Node > &newChild, const SharedPtr< Node > &refChild)
 Inserts the specified node immediately after the specified reference node. More...
 
SharedPtr< NodeInsertBefore (const SharedPtr< Node > &newChild, const SharedPtr< Node > &refChild)
 Inserts the specified node immediately before the specified reference node. More...
 
SharedPtr< NodePrependChild (const SharedPtr< Node > &newChild)
 Adds the specified node to the beginning of the list of child nodes for this node. More...
 
void RemoveAllChildren ()
 Removes all the child nodes of the current node. More...
 
SharedPtr< NodeRemoveChild (const SharedPtr< Node > &oldChild)
 Removes the specified child node. More...
 
void RemoveSmartTags ()
 Removes all SmartTag descendant nodes of the current node. More...
 
SharedPtr< NodeListSelectNodes (const String &xpath)
 Selects a list of nodes matching the XPath expression. More...
 
SharedPtr< NodeSelectSingleNode (const String &xpath)
 Selects the first Node that matches the XPath expression. More...
 
void SetTemplateWeakPtr (uint32_t argument) override
 
- Public Member Functions inherited from Node
SharedPtr< NodeClone (bool isCloneChildren)
 Creates a duplicate of the node. More...
 
int32_t get_CustomNodeId () const
 Specifies custom node identifier. More...
 
virtual SharedPtr< DocumentBaseget_Document () const
 Gets the document to which this node belongs. More...
 
SharedPtr< Nodeget_NextSibling ()
 Gets the node immediately following this node. More...
 
SharedPtr< CompositeNodeget_ParentNode ()
 Gets the immediate parent of this node. More...
 
SharedPtr< Nodeget_PreviousSibling ()
 Gets the node immediately preceding this node. More...
 
SharedPtr< Rangeget_Range ()
 Returns a Range object that represents the portion of a document that is contained in this node. More...
 
SharedPtr< CompositeNodeGetAncestor (NodeType ancestorType)
 Gets the first ancestor of the specified NodeType. More...
 
template<typename T >
GetAncestorOf ()
 
SharedPtr< NodeNextPreOrder (const SharedPtr< Node > &rootNode)
 Gets next node according to the pre-order tree traversal algorithm. More...
 
SharedPtr< NodePreviousPreOrder (const SharedPtr< Node > &rootNode)
 Gets the previous node according to the pre-order tree traversal algorithm. More...
 
void Remove ()
 Removes itself from the parent. More...
 
void set_CustomNodeId (int32_t value)
 Setter for get_CustomNodeId. More...
 
String ToString (SaveFormat saveFormat)
 Exports the content of the node into a string in the specified format. More...
 
String ToString (const SharedPtr< SaveOptions > &saveOptions)
 Exports the content of the node into a string using the specified save options. More...
 

Static Public Member Functions

static const TypeInfoType ()
 
- Static Public Member Functions inherited from Story
static const TypeInfoType ()
 
- Static Public Member Functions inherited from CompositeNode
static const TypeInfoType ()
 
- Static Public Member Functions inherited from Node
static String NodeTypeToString (NodeType nodeType)
 A utility method that converts a node type enum value into a user friendly string. More...
 
static const TypeInfoType ()
 

Constructor & Destructor Documentation

◆ Body()

Aspose::Words::Body::Body ( const System::SharedPtr< Aspose::Words::DocumentBase > &  doc)

Initializes a new instance of the Body class.

When Body is created, it belongs to the specified document, but is not yet part of the document and ParentNode is null.

To append Body to a Section use Section.InsertAfter or Section.InsertBefore.

Parameters
docThe owner document.
Examples

Shows how to construct an Aspose.Words document by hand.

auto doc = MakeObject<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.
auto section = MakeObject<Section>(doc);
doc->AppendChild(section);
// Set some page setup properties for the section.
section->get_PageSetup()->set_SectionStart(SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(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.
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
auto para = MakeObject<Paragraph>(doc);
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(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.
auto run = MakeObject<Run>(doc);
run->set_Text(u"Hello World!");
run->get_Font()->set_Color(System::Drawing::Color::get_Red());
para->AppendChild(run);
ASSERT_EQ(u"Hello World!", doc->GetText().Trim());
doc->Save(ArtifactsDir + u"Section.CreateManually.docx");

Member Function Documentation

◆ Accept()

bool Aspose::Words::Body::Accept ( System::SharedPtr< Aspose::Words::DocumentVisitor visitor)
overridevirtual

Accepts a visitor.

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.

Parameters
visitorThe visitor that will visit the nodes.
Returns
True if all nodes were visited; false if DocumentVisitor stopped the operation before visiting all nodes.
Examples

Shows how to use a document visitor to print a document's node structure.

void DocStructureToText()
{
auto doc = MakeObject<Document>(MyDir + u"DocumentVisitor-compatible features.docx");
auto visitor = MakeObject<ExDocumentVisitor::DocStructurePrinter>();
// When we get a composite node to accept a document visitor, the visitor visits the accepting node,
// and then traverses all the node's children in a depth-first manner.
// The visitor can read and modify each visited node.
doc->Accept(visitor);
std::cout << visitor->GetText() << std::endl;
}
class DocStructurePrinter : public DocumentVisitor
{
public:
DocStructurePrinter() : mDocTraversalDepth(0)
{
mAcceptingNodeChildTree = MakeObject<System::Text::StringBuilder>();
}
String GetText()
{
return mAcceptingNodeChildTree->ToString();
}
VisitorAction VisitDocumentStart(SharedPtr<Document> doc) override
{
int childNodeCount = doc->GetChildNodes(NodeType::Any, true)->get_Count();
IndentAndAppendLine(String(u"[Document start] Child nodes: ") + childNodeCount);
mDocTraversalDepth++;
// Allow the visitor to continue visiting other nodes.
}
VisitorAction VisitDocumentEnd(SharedPtr<Document> doc) override
{
mDocTraversalDepth--;
IndentAndAppendLine(u"[Document end]");
}
VisitorAction VisitSectionStart(SharedPtr<Section> section) override
{
// Get the index of our section within the document.
SharedPtr<NodeCollection> docSections = section->get_Document()->GetChildNodes(NodeType::Section, false);
int sectionIndex = docSections->IndexOf(section);
IndentAndAppendLine(String(u"[Section start] Section index: ") + sectionIndex);
mDocTraversalDepth++;
}
VisitorAction VisitSectionEnd(SharedPtr<Section> section) override
{
mDocTraversalDepth--;
IndentAndAppendLine(u"[Section end]");
}
VisitorAction VisitBodyStart(SharedPtr<Body> body) override
{
int paragraphCount = body->get_Paragraphs()->get_Count();
IndentAndAppendLine(String(u"[Body start] Paragraphs: ") + paragraphCount);
mDocTraversalDepth++;
}
VisitorAction VisitBodyEnd(SharedPtr<Body> body) override
{
mDocTraversalDepth--;
IndentAndAppendLine(u"[Body end]");
}
VisitorAction VisitParagraphStart(SharedPtr<Paragraph> paragraph) override
{
IndentAndAppendLine(u"[Paragraph start]");
mDocTraversalDepth++;
}
VisitorAction VisitParagraphEnd(SharedPtr<Paragraph> paragraph) override
{
mDocTraversalDepth--;
IndentAndAppendLine(u"[Paragraph end]");
}
VisitorAction VisitRun(SharedPtr<Run> run) override
{
IndentAndAppendLine(String(u"[Run] \"") + run->GetText() + u"\"");
}
VisitorAction VisitSubDocument(SharedPtr<SubDocument> subDocument) override
{
IndentAndAppendLine(u"[SubDocument]");
}
private:
int mDocTraversalDepth;
SharedPtr<System::Text::StringBuilder> mAcceptingNodeChildTree;
void IndentAndAppendLine(String text)
{
for (int i = 0; i < mDocTraversalDepth; i++)
{
mAcceptingNodeChildTree->Append(u"| ");
}
mAcceptingNodeChildTree->AppendLine(text);
}
};

Implements Aspose::Words::Node.

◆ EnsureMinimum()

void Aspose::Words::Body::EnsureMinimum ( )

If the last child is not a paragraph, creates and appends one empty paragraph.

Examples

Clears main text from all sections from the document leaving the sections themselves.

auto doc = MakeObject<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.
auto section = MakeObject<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.
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// This body has no children, so we cannot add runs to it yet.
ASSERT_EQ(0, doc->get_FirstSection()->get_Body()->GetChildNodes(NodeType::Any, true)->get_Count());
// 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->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"Hello world!"));
ASSERT_EQ(u"Hello world!", doc->GetText().Trim());

◆ get_NodeType()

Aspose::Words::NodeType Aspose::Words::Body::get_NodeType ( ) const
overridevirtual

Returns NodeType.Body.

Examples

Shows how to iterate through the children of a composite node.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->Write(u"Primary header");
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary);
builder->Write(u"Primary footer");
SharedPtr<Section> section = doc->get_FirstSection();
// 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 (const auto& node : System::IterateOver(section))
{
switch (node->get_NodeType())
{
auto body = System::DynamicCast<Body>(node);
std::cout << "Body:" << std::endl;
std::cout << "\t\"" << body->GetText().Trim() << "\"" << std::endl;
break;
}
auto headerFooter = System::DynamicCast<HeaderFooter>(node);
std::cout << String::Format(u"HeaderFooter type: {0}:", headerFooter->get_HeaderFooterType()) << std::endl;
std::cout << "\t\"" << headerFooter->GetText().Trim() << "\"" << std::endl;
break;
}
default: {
throw System::Exception(u"Unexpected node type in a section.");
}
}
}

Implements Aspose::Words::Node.

◆ get_ParentSection()

System::SharedPtr<Aspose::Words::Section> Aspose::Words::Body::get_ParentSection ( )

Gets the parent section of this story.

ParentSection is equivalent to %(Section)ParentNode.

Examples

Shows how to store endnotes at the end of each section, and modify their positions.

void SuppressEndnotes()
{
auto doc = MakeObject<Document>();
doc->RemoveAllChildren();
// By default, a document compiles all endnotes at its end.
ASSERT_EQ(EndnotePosition::EndOfDocument, doc->get_EndnoteOptions()->get_Position());
// We use the "Position" property of the document's "EndnoteOptions" object
// to collect endnotes at the end of each section instead.
doc->get_EndnoteOptions()->set_Position(EndnotePosition::EndOfSection);
InsertSectionWithEndnote(doc, u"Section 1", u"Endnote 1, will stay in section 1");
InsertSectionWithEndnote(doc, u"Section 2", u"Endnote 2, will be pushed down to section 3");
InsertSectionWithEndnote(doc, u"Section 3", u"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.
SharedPtr<PageSetup> pageSetup = doc->get_Sections()->idx_get(1)->get_PageSetup();
pageSetup->set_SuppressEndnotes(true);
doc->Save(ArtifactsDir + u"PageSetup.SuppressEndnotes.docx");
}
static void InsertSectionWithEndnote(SharedPtr<Document> doc, String sectionBodyText, String endnoteText)
{
auto section = MakeObject<Section>(doc);
doc->AppendChild(section);
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
ASPOSE_ASSERT_EQ(section, body->get_ParentNode());
auto para = MakeObject<Paragraph>(doc);
body->AppendChild(para);
ASPOSE_ASSERT_EQ(body, para->get_ParentNode());
auto builder = MakeObject<DocumentBuilder>(doc);
builder->MoveTo(para);
builder->Write(sectionBodyText);
builder->InsertFootnote(FootnoteType::Endnote, endnoteText);
}

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Body::GetType ( ) const
overridevirtual

Reimplemented from Aspose::Words::Story.

◆ Is()

virtual bool Aspose::Words::Body::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from Aspose::Words::Story.

◆ Type()

static const System::TypeInfo& Aspose::Words::Body::Type ( )
static