Aspose::Words::Node Class Referenceabstract

Detailed Description

Base class for all nodes of a Word document.

A document is represented as a tree of nodes, similar to DOM or XmlDocument.

For more info see the Composite design pattern.

The Node class:

  • Defines the child node interface.
  • Defines the interface for visiting nodes.
  • Provides default cloning capability.
  • Implements parent node and owner document mechanisms.
  • Implements access to sibling nodes.
Examples

Shows how to clone composite nodes with and without their child nodes.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
para->AppendChild(MakeObject<Run>(doc, u"Hello world!"));
// Clone the paragraph and the child nodes
SharedPtr<Node> cloneWithChildren = para->Clone(true);
ASSERT_TRUE((System::DynamicCast<CompositeNode>(cloneWithChildren))->get_HasChildNodes());
ASSERT_EQ(u"Hello world!", cloneWithChildren->GetText().Trim());
// Clone the paragraph without its clild nodes
SharedPtr<Node> cloneWithoutChildren = para->Clone(false);
ASSERT_FALSE((System::DynamicCast<CompositeNode>(cloneWithoutChildren))->get_HasChildNodes());
ASSERT_EQ(String::Empty, cloneWithoutChildren->GetText().Trim());

Shows how to enumerate immediate children of a CompositeNode using the enumerator provided by the ChildNodes collection.

auto doc = MakeObject<Document>();
auto paragraph = System::DynamicCast<Paragraph>(doc->GetChild(NodeType::Paragraph, 0, true));
paragraph->AppendChild(MakeObject<Run>(doc, u"Hello world!"));
paragraph->AppendChild(MakeObject<Run>(doc, u" Hello again!"));
SharedPtr<NodeCollection> children = paragraph->get_ChildNodes();
// Paragraph may contain children of various types such as runs, shapes and so on
for (auto child : System::IterateOver(children))
{
if (child->get_NodeType() == NodeType::Run)
{
auto run = System::DynamicCast<Run>(child);
System::Console::WriteLine(run->get_Text());
}
}

Shows how to remove all nodes of a specific type from a composite node.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
ASSERT_EQ(2, doc->GetChildNodes(NodeType::Table, true)->get_Count());
// Select the first child node in the body
SharedPtr<Node> curNode = doc->get_FirstSection()->get_Body()->get_FirstChild();
while (curNode != nullptr)
{
// Save the next sibling node as a variable in case we want to move to it after deleting this node
SharedPtr<Node> nextNode = curNode->get_NextSibling();
// A section body can contain Paragraph and Table nodes
// If the node is a Table, remove it from the parent
if (curNode->get_NodeType() == NodeType::Table)
{
curNode->Remove();
}
// Continue going through child nodes until null (no more siblings) is reached
curNode = nextNode;
}
ASSERT_EQ(0, doc->GetChildNodes(NodeType::Table, true)->get_Count());

#include <Aspose.Words.Cpp/Model/Nodes/Node.h>

+ Inheritance diagram for Aspose::Words::Node:

Public Member Functions

virtual bool Accept (SharedPtr< DocumentVisitor > visitor)=0
 Accepts a visitor. More...
 
SharedPtr< NodeClone (bool isCloneChildren)
 
virtual SharedPtr< DocumentBaseget_Document () const
 Gets the document to which this node belongs. More...
 
virtual bool get_IsComposite ()
 Returns true if this node can contain other nodes. More...
 
SharedPtr< Nodeget_NextSibling ()
 Gets the node immediately following this node. More...
 
virtual NodeType get_NodeType () const =0
 Gets the type of 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 ()
 
virtual String GetText ()
 Gets the text of this node and of all its children. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
SharedPtr< NodeNextPreOrder (SharedPtr< Node > rootNode)
 Gets next node according to the pre-order tree traversal algorithm. More...
 
SharedPtr< NodePreviousPreOrder (SharedPtr< Node > rootNode)
 Gets the previous node according to the pre-order tree traversal algorithm. More...
 
void Remove ()
 Removes itself from the parent. More...
 
String ToString (SaveFormat saveFormat)
 Exports the content of the node into a string in the specified format. More...
 
String ToString (SharedPtr< SaveOptions > saveOptions)
 Exports the content of the node into a string using the specified save options. More...
 
- Public Member Functions inherited from Object
ASPOSECPP_SHARED_API Object ()
 
ASPOSECPP_SHARED_API Object (Object const &x)
 
virtual ASPOSECPP_SHARED_API ~Object ()
 
virtual ASPOSECPP_SHARED_API bool Equals (ptr obj)
 
Detail::SmartPtrCounter * GetCounter ()
 
virtual ASPOSECPP_SHARED_API int GetHashCode () const
 
ASPOSECPP_SHARED_API void Lock ()
 
virtual ASPOSECPP_SHARED_API ptr MemberwiseClone () const
 
Objectoperator= (Object const &x)
 
bool ReferenceEquals (String const &str, std::nullptr_t)
 
bool ReferenceEquals (String const &str1, String const &str2)
 
int RemovedSharedRefs (int count)
 
virtual ASPOSECPP_SHARED_API void SetTemplateWeakPtr (unsigned int argument)
 
int SharedCount () const
 
ObjectSharedRefAdded ()
 
int SharedRefRemovedSafe ()
 
virtual ASPOSECPP_SHARED_API String ToString () const
 
ASPOSECPP_SHARED_API void Unlock ()
 
Detail::SmartPtrCounter * WeakRefAdded ()
 
void WeakRefRemoved ()
 

Static Public Member Functions

static String NodeTypeToString (NodeType nodeType)
 A utility method that converts a node type enum value into a user friendly string. More...
 
static const TypeInfoType ()
 
- Static Public Member Functions inherited from Object
bool Equals (double const &objA, double const &objB)
 
bool Equals (float const &objA, float const &objB)
 
static std::enable_if<!IsSmartPtr< T1 >::value &&!IsSmartPtr< T2 >::value, bool >::type Equals (T1 const &objA, T2 const &objB)
 
static std::enable_if< IsSmartPtr< T1 >::value &&IsSmartPtr< T2 >::value, bool >::type Equals (T1 const &objA, T2 const &objB)
 
static bool ReferenceEquals (ptr const &objA, ptr const &objB)
 
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals (T const &objA, std::nullptr_t)
 
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals (T const &objA, T const &objB)
 
static const TypeInfoType ()
 

Additional Inherited Members

- Public Types inherited from Object
typedef SmartPtr< Objectptr
 
typedef System::Details::SharedMembersType shared_members_type
 

Member Function Documentation

◆ Accept()

virtual bool Aspose::Words::Node::Accept ( System::SharedPtr< Aspose::Words::DocumentVisitor visitor)
pure virtual

◆ Clone()

System::SharedPtr<Aspose::Words::Node> Aspose::Words::Node::Clone ( bool  isCloneChildren)
Examples

Shows how to clone composite nodes with and without their child nodes.

auto doc = MakeObject<Document>();
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
para->AppendChild(MakeObject<Run>(doc, u"Hello world!"));
// Clone the paragraph and the child nodes
SharedPtr<Node> cloneWithChildren = para->Clone(true);
ASSERT_TRUE((System::DynamicCast<CompositeNode>(cloneWithChildren))->get_HasChildNodes());
ASSERT_EQ(u"Hello world!", cloneWithChildren->GetText().Trim());
// Clone the paragraph without its clild nodes
SharedPtr<Node> cloneWithoutChildren = para->Clone(false);
ASSERT_FALSE((System::DynamicCast<CompositeNode>(cloneWithoutChildren))->get_HasChildNodes());
ASSERT_EQ(String::Empty, cloneWithoutChildren->GetText().Trim());

◆ get_Document()

virtual System::SharedPtr<Aspose::Words::DocumentBase> Aspose::Words::Node::get_Document ( ) const
virtual

Gets the document to which this node belongs.

The node always belongs to a document even if it has just been created and not yet added to the tree, or if it has been removed from the tree.

Examples

Shows how to create a node and set its owning document.

// Open a file from disk
auto doc = MakeObject<Document>();
// Creating a new node of any type requires a document passed into the constructor
auto para = MakeObject<Paragraph>(doc);
// The new paragraph node does not yet have a parent
System::Console::WriteLine(String(u"Paragraph has no parent node: ") + (para->get_ParentNode() == nullptr));
// But the paragraph node knows its document
System::Console::WriteLine(String(u"Both nodes' documents are the same: ") + (para->get_Document() == doc));
// The fact that a node always belongs to a document allows us to access and modify
// properties that reference the document-wide data such as styles or lists
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
// Now add the paragraph to the main text of the first section
doc->get_FirstSection()->get_Body()->AppendChild(para);
// The paragraph node is now a child of the Body node
System::Console::WriteLine(String(u"Paragraph has a parent node: ") + (para->get_ParentNode() != nullptr));

Reimplemented in Aspose::Words::DocumentBase.

◆ get_IsComposite()

virtual bool Aspose::Words::Node::get_IsComposite ( )
virtual

Returns true if this node can contain other nodes.

Reimplemented in Aspose::Words::CompositeNode.

◆ get_NextSibling()

System::SharedPtr<Aspose::Words::Node> Aspose::Words::Node::get_NextSibling ( )

Gets the node immediately following this node.

Examples

Shows how to enumerate immediate child nodes of a composite node using NextSibling.

auto doc = MakeObject<Document>(MyDir + u"Paragraphs.docx");
// Loop starting from the first child until we reach null
for (SharedPtr<Node> node = doc->get_FirstSection()->get_Body()->get_FirstChild(); node != nullptr; node = node->get_NextSibling())
{
// Output the types of the nodes that we come across
}

◆ get_NodeType()

virtual Aspose::Words::NodeType Aspose::Words::Node::get_NodeType ( ) const
pure virtual

Gets the type of this node.

Examples

Shows how to remove all nodes of a specific type from a composite node.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
ASSERT_EQ(2, doc->GetChildNodes(NodeType::Table, true)->get_Count());
// Select the first child node in the body
SharedPtr<Node> curNode = doc->get_FirstSection()->get_Body()->get_FirstChild();
while (curNode != nullptr)
{
// Save the next sibling node as a variable in case we want to move to it after deleting this node
SharedPtr<Node> nextNode = curNode->get_NextSibling();
// A section body can contain Paragraph and Table nodes
// If the node is a Table, remove it from the parent
if (curNode->get_NodeType() == NodeType::Table)
{
curNode->Remove();
}
// Continue going through child nodes until null (no more siblings) is reached
curNode = nextNode;
}
ASSERT_EQ(0, doc->GetChildNodes(NodeType::Table, true)->get_Count());

Shows how to enumerate immediate child nodes of a composite node using NextSibling.

auto doc = MakeObject<Document>(MyDir + u"Paragraphs.docx");
// Loop starting from the first child until we reach null
for (SharedPtr<Node> node = doc->get_FirstSection()->get_Body()->get_FirstChild(); node != nullptr; node = node->get_NextSibling())
{
// Output the types of the nodes that we come across
}

Implemented in Aspose::Words::Document, Aspose::Words::Paragraph, Aspose::Words::Drawing::Shape, Aspose::Words::Tables::Cell, Aspose::Words::Tables::Row, Aspose::Words::Tables::Table, Aspose::Words::Section, Aspose::Words::Run, Aspose::Words::Math::OfficeMath, Aspose::Words::Markup::StructuredDocumentTag, Aspose::Words::HeaderFooter, Aspose::Words::Body, Aspose::Words::Comment, Aspose::Words::Fields::FormField, Aspose::Words::Footnote, Aspose::Words::Fields::FieldStart, Aspose::Words::Markup::StructuredDocumentTagRangeStart, Aspose::Words::SubDocument, Aspose::Words::Fields::FieldSeparator, Aspose::Words::Fields::FieldEnd, Aspose::Words::BookmarkStart, Aspose::Words::EditableRangeStart, Aspose::Words::BuildingBlocks::GlossaryDocument, Aspose::Words::EditableRangeEnd, Aspose::Words::BuildingBlocks::BuildingBlock, Aspose::Words::SpecialChar, Aspose::Words::BookmarkEnd, Aspose::Words::Markup::SmartTag, Aspose::Words::Drawing::GroupShape, Aspose::Words::CommentRangeEnd, Aspose::Words::CommentRangeStart, and Aspose::Words::Markup::StructuredDocumentTagRangeEnd.

◆ get_ParentNode()

System::SharedPtr<Aspose::Words::CompositeNode> Aspose::Words::Node::get_ParentNode ( )

Gets the immediate parent of this node.

If a node has just been created and not yet added to the tree, or if it has been removed from the tree, the parent is null.

Examples

Shows how to access the parent node.

auto doc = MakeObject<Document>();
// Get the document's first paragraph and append a child node to it in the form of a run with text
SharedPtr<Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// When inserting a new node, the document that the node will belong to must be provided as an argument
auto run = MakeObject<Run>(doc, u"Hello world!");
para->AppendChild(run);
// The node lineage can be traced back to the document itself
ASPOSE_ASSERT_EQ(para, run->get_ParentNode());
ASPOSE_ASSERT_EQ(doc->get_FirstSection()->get_Body(), para->get_ParentNode());
ASPOSE_ASSERT_EQ(doc->get_FirstSection(), doc->get_FirstSection()->get_Body()->get_ParentNode());
ASPOSE_ASSERT_EQ(doc, doc->get_FirstSection()->get_ParentNode());

Shows how to create a node and set its owning document.

// Open a file from disk
auto doc = MakeObject<Document>();
// Creating a new node of any type requires a document passed into the constructor
auto para = MakeObject<Paragraph>(doc);
// The new paragraph node does not yet have a parent
System::Console::WriteLine(String(u"Paragraph has no parent node: ") + (para->get_ParentNode() == nullptr));
// But the paragraph node knows its document
System::Console::WriteLine(String(u"Both nodes' documents are the same: ") + (para->get_Document() == doc));
// The fact that a node always belongs to a document allows us to access and modify
// properties that reference the document-wide data such as styles or lists
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
// Now add the paragraph to the main text of the first section
doc->get_FirstSection()->get_Body()->AppendChild(para);
// The paragraph node is now a child of the Body node
System::Console::WriteLine(String(u"Paragraph has a parent node: ") + (para->get_ParentNode() != nullptr));

◆ get_PreviousSibling()

System::SharedPtr<Aspose::Words::Node> Aspose::Words::Node::get_PreviousSibling ( )

Gets the node immediately preceding this node.

Examples

Shows how to use of methods of Node and CompositeNode to remove a section before the last section in the document.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Create a second section by inserting a section break and add text to both sections
builder->Writeln(u"Section 1 text.");
builder->InsertBreak(BreakType::SectionBreakContinuous);
builder->Writeln(u"Section 2 text.");
// Both sections are siblings of each other
auto lastSection = System::DynamicCast<Section>(doc->get_LastChild());
auto firstSection = System::DynamicCast<Section>(lastSection->get_PreviousSibling());
// Remove a section based on its sibling relationship with another section
if (lastSection->get_PreviousSibling() != nullptr)
{
doc->RemoveChild(firstSection);
}
// The section we removed was the first one, leaving the document with only the second
ASSERT_EQ(u"Section 2 text.", doc->GetText().Trim());

◆ get_Range()

System::SharedPtr<Aspose::Words::Range> Aspose::Words::Node::get_Range ( )

Returns a Range object that represents the portion of a document that is contained in this node.

Examples

Shows how to delete all characters of a range.

// Insert two sections into a blank document
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1. ");
builder->InsertBreak(BreakType::SectionBreakContinuous);
builder->Write(u"Section 2.");
// Verify the whole text of the document
ASSERT_EQ(u"Section 1. \fSection 2.", doc->GetText().Trim());
// Delete the first section from the document
doc->get_Sections()->idx_get(0)->get_Range()->Delete();
// Check the first section was deleted by looking at the text of the whole document again
ASSERT_EQ(u"Section 2.", doc->GetText().Trim());

◆ GetAncestor()

System::SharedPtr<Aspose::Words::CompositeNode> Aspose::Words::Node::GetAncestor ( Aspose::Words::NodeType  ancestorType)

Gets the first ancestor of the specified NodeType.

Parameters
ancestorTypeThe node type of the ancestor to retrieve.
Returns
The ancestor of the specified type or null if no ancestor of this type was found.

◆ GetAncestorOf()

template<typename T >
T Aspose::Words::Node::GetAncestorOf ( )
inline

◆ GetText()

virtual System::String Aspose::Words::Node::GetText ( )
virtual

Gets the text of this node and of all its children.

The returned string includes all control and special characters as described in ControlChar.

Examples

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

auto doc = MakeObject<Document>();
// A newly created blank document still comes one section, one body and one paragraph
// Calling this method will remove all those nodes to completely empty the document
doc->RemoveAllChildren();
// This document now has no composite nodes that content can be added to
// If we wish to edit it, we will need to repopulate its node collection,
// which we will start to do with by creating a new Section node
auto section = MakeObject<Section>(doc);
// Append the section to the document
doc->AppendChild(section);
// Lets set some properties for the section
section->get_PageSetup()->set_SectionStart(SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(PaperSize::Letter);
// The section that we created is empty, lets populate it. The section needs at least the Body node
auto body = MakeObject<Body>(doc);
section->AppendChild(body);
// The body needs to have at least one paragraph
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information
auto para = MakeObject<Paragraph>(doc);
body->AppendChild(para);
// We can set some formatting for the paragraph
para->get_ParagraphFormat()->set_StyleName(u"Heading 1");
para->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
// So far we have one empty paragraph in the document
// The document is valid and can be saved, but lets add some text before saving
// Create a new run of text and add it to our 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(String(u"Hello World!") + ControlChar::SectionBreakChar, doc->GetText());
doc->Save(ArtifactsDir + u"Section.CreateFromScratch.docx");

Reimplemented in Aspose::Words::Paragraph, Aspose::Words::CompositeNode, Aspose::Words::Tables::Row, Aspose::Words::Run, Aspose::Words::BookmarkStart, and Aspose::Words::SpecialChar.

◆ GetType()

◆ Is()

◆ NextPreOrder()

System::SharedPtr<Aspose::Words::Node> Aspose::Words::Node::NextPreOrder ( System::SharedPtr< Aspose::Words::Node rootNode)

Gets next node according to the pre-order tree traversal algorithm.

Parameters
rootNodeThe top node (limit) of traversal.
Returns
Next node in pre-order order. Null if reached the rootNode.
Examples

Shows how to delete all images from a document using pre-order tree traversal.

auto doc = MakeObject<Document>(MyDir + u"Images.docx");
ASSERT_EQ(10, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
SharedPtr<Node> curNode = doc;
while (curNode != nullptr)
{
SharedPtr<Node> nextNode = curNode->NextPreOrder(doc);
if (curNode->PreviousPreOrder(doc) != nullptr && nextNode != nullptr)
{
ASPOSE_ASSERT_EQ(curNode, nextNode->PreviousPreOrder(doc));
}
// Several shape types can have an image including image shapes and OLE objects
if (curNode->get_NodeType() == NodeType::Shape && (System::DynamicCast<Shape>(curNode))->get_HasImage())
{
curNode->Remove();
}
curNode = nextNode;
}
// The only remaining shape doesn't have an image
ASSERT_EQ(1, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
ASSERT_FALSE((System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true)))->get_HasImage());

◆ NodeTypeToString()

static System::String Aspose::Words::Node::NodeTypeToString ( Aspose::Words::NodeType  nodeType)
static

A utility method that converts a node type enum value into a user friendly string.

Examples

Shows how to enumerate immediate child nodes of a composite node using NextSibling.

auto doc = MakeObject<Document>(MyDir + u"Paragraphs.docx");
// Loop starting from the first child until we reach null
for (SharedPtr<Node> node = doc->get_FirstSection()->get_Body()->get_FirstChild(); node != nullptr; node = node->get_NextSibling())
{
// Output the types of the nodes that we come across
}

◆ PreviousPreOrder()

System::SharedPtr<Aspose::Words::Node> Aspose::Words::Node::PreviousPreOrder ( System::SharedPtr< Aspose::Words::Node rootNode)

Gets the previous node according to the pre-order tree traversal algorithm.

Parameters
rootNodeThe top node (limit) of traversal.
Returns
Previous node in pre-order order. Null if reached the rootNode.
Examples

Shows how to delete all images from a document using pre-order tree traversal.

auto doc = MakeObject<Document>(MyDir + u"Images.docx");
ASSERT_EQ(10, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
SharedPtr<Node> curNode = doc;
while (curNode != nullptr)
{
SharedPtr<Node> nextNode = curNode->NextPreOrder(doc);
if (curNode->PreviousPreOrder(doc) != nullptr && nextNode != nullptr)
{
ASPOSE_ASSERT_EQ(curNode, nextNode->PreviousPreOrder(doc));
}
// Several shape types can have an image including image shapes and OLE objects
if (curNode->get_NodeType() == NodeType::Shape && (System::DynamicCast<Shape>(curNode))->get_HasImage())
{
curNode->Remove();
}
curNode = nextNode;
}
// The only remaining shape doesn't have an image
ASSERT_EQ(1, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
ASSERT_FALSE((System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true)))->get_HasImage());

◆ Remove()

void Aspose::Words::Node::Remove ( )

Removes itself from the parent.

Examples

Shows how to delete all images from a document.

auto doc = MakeObject<Document>(MyDir + u"Images.docx");
ASSERT_EQ(10, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
// Here we get all shapes from the document node, but you can do this for any smaller
// node too, for example delete shapes from a single section or a paragraph
SharedPtr<NodeCollection> shapes = doc->GetChildNodes(NodeType::Shape, true);
// We cannot delete shape nodes while we enumerate through the collection
// One solution is to add nodes that we want to delete to a temporary array and delete afterwards
SharedPtr<System::Collections::Generic::List<SharedPtr<Shape>>> shapesToDelete = MakeObject<System::Collections::Generic::List<SharedPtr<Shape>>>();
// Several shape types can have an image including image shapes and OLE objects
for (auto shape : System::IterateOver(shapes->LINQ_OfType<SharedPtr<Shape> >()))
{
if (shape->get_HasImage())
{
shapesToDelete->Add(shape);
}
}
// Now we can delete shapes
for (auto shape : shapesToDelete)
{
shape->Remove();
}
// The only remaining shape doesn't have an image
ASSERT_EQ(1, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
ASSERT_FALSE((System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true)))->get_HasImage());

Shows how to remove all nodes of a specific type from a composite node.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
ASSERT_EQ(2, doc->GetChildNodes(NodeType::Table, true)->get_Count());
// Select the first child node in the body
SharedPtr<Node> curNode = doc->get_FirstSection()->get_Body()->get_FirstChild();
while (curNode != nullptr)
{
// Save the next sibling node as a variable in case we want to move to it after deleting this node
SharedPtr<Node> nextNode = curNode->get_NextSibling();
// A section body can contain Paragraph and Table nodes
// If the node is a Table, remove it from the parent
if (curNode->get_NodeType() == NodeType::Table)
{
curNode->Remove();
}
// Continue going through child nodes until null (no more siblings) is reached
curNode = nextNode;
}
ASSERT_EQ(0, doc->GetChildNodes(NodeType::Table, true)->get_Count());

◆ ToString() [1/2]

System::String Aspose::Words::Node::ToString ( Aspose::Words::SaveFormat  saveFormat)

Exports the content of the node into a string in the specified format.

Returns
The content of the node in the specified format.
Examples

Shows the difference between calling the GetText and ToString methods on a node.

auto doc = MakeObject<Document>();
// Enter a field into the document
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u"MERGEFIELD Field");
// GetText will retrieve all field codes and special characters
ASSERT_EQ(u"\u0013MERGEFIELD Field\u0014«Field»\u0015\u000c", doc->GetText());
// ToString will give us the plaintext version of the document in the save format we put into the parameter
ASSERT_EQ(u"«Field»\r\n", doc->ToString(SaveFormat::Text));

Shows how to extract the label of each paragraph in a list as a value or a String.

auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");
doc->UpdateListLabels();
SharedPtr<NodeCollection> paras = doc->GetChildNodes(NodeType::Paragraph, true);
// Find if we have the paragraph list. In our document our list uses plain arabic numbers,
// which start at three and ends at six
for (auto paragraph : System::IterateOver(paras->LINQ_OfType<SharedPtr<Paragraph>>()->LINQ_Where([](SharedPtr<Paragraph> p) { return p->get_ListFormat()->get_IsListItem(); })))
{
System::Console::WriteLine(String::Format(u"List item paragraph #{0}", paras->IndexOf(paragraph)));
// This is the text we get when actually getting when we output this node to text format
// The list labels are not included in this text output. Trim any paragraph formatting characters
String paragraphText = paragraph->ToString(SaveFormat::Text).Trim();
System::Console::WriteLine(String::Format(u"\tExported Text: {0}", paragraphText));
SharedPtr<ListLabel> label = paragraph->get_ListLabel();
// This gets the position of the paragraph in current level of the list. If we have a list with multiple level then this
// will tell us what position it is on that particular level
System::Console::WriteLine(String::Format(u"\tNumerical Id: {0}", label->get_LabelValue()));
// Combine them together to include the list label with the text in the output
System::Console::WriteLine(String::Format(u"\tList label combined with text: {0} {1}", label->get_LabelString(), paragraphText));
}

Exports the content of a node to String in HTML format.

auto doc = MakeObject<Document>(MyDir + u"Document.docx");
// Extract the last paragraph in the document to convert to HTML
SharedPtr<Node> node = doc->get_LastSection()->get_Body()->get_LastParagraph();
// When ToString is called using the html SaveFormat overload then the node is converted directly to html
ASSERT_EQ(String(u"<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%; font-size:12pt\">") + u"<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + u"</p>", node->ToString(SaveFormat::Html));
// We can also modify the result of this conversion using a SaveOptions object
auto saveOptions = MakeObject<HtmlSaveOptions>();
saveOptions->set_ExportRelativeFontSize(true);
ASSERT_EQ(String(u"<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%\">") + u"<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + u"</p>", node->ToString(saveOptions));

◆ ToString() [2/2]

System::String Aspose::Words::Node::ToString ( System::SharedPtr< Aspose::Words::Saving::SaveOptions saveOptions)

Exports the content of the node into a string using the specified save options.

Parameters
saveOptionsSpecifies the options that control how the node is saved.
Returns
The content of the node in the specified format.
Examples

Exports the content of a node to String in HTML format.

auto doc = MakeObject<Document>(MyDir + u"Document.docx");
// Extract the last paragraph in the document to convert to HTML
SharedPtr<Node> node = doc->get_LastSection()->get_Body()->get_LastParagraph();
// When ToString is called using the html SaveFormat overload then the node is converted directly to html
ASSERT_EQ(String(u"<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%; font-size:12pt\">") + u"<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + u"</p>", node->ToString(SaveFormat::Html));
// We can also modify the result of this conversion using a SaveOptions object
auto saveOptions = MakeObject<HtmlSaveOptions>();
saveOptions->set_ExportRelativeFontSize(true);
ASSERT_EQ(String(u"<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%\">") + u"<span style=\"font-family:'Times New Roman'\">Hello World!</span>" + u"</p>", node->ToString(saveOptions));

◆ Type()

static const System::TypeInfo& Aspose::Words::Node::Type ( )
static
String
@ Shape
A drawing object, such as an OfficeArt shape, image or an OLE object. A Shape node can contain Paragr...
static constexpr char16_t SectionBreakChar
End of section character: (char)12 or "\f".
Definition: ControlChar.h:1253
@ Text
Saves the document in the plain text format.
static ASPOSECPP_SHARED_API void WriteLine()
std::enable_if_t<!Details::IsIterable< Enumerable >::value, Details::EnumeratorAdapter< Enumerable, T > > IterateOver(System::SmartPtr< Enumerable > enumerable)
@ Letter
8.5 x 11 inches.
@ NewPage
The section starts from a new page.
static System::String NodeTypeToString(Aspose::Words::NodeType nodeType)
A utility method that converts a node type enum value into a user friendly string.
@ Html
Saves the document in the HTML format.
static ASPOSECPP_SHARED_API Color get_Red()
@ Run
A run of text.
@ Paragraph
A paragraph of text. A Paragraph node is a container for inline level elements Run,...
@ Table
A Table object that represents a table in a Word document. A Table node can have Row nodes.
@ SectionBreakContinuous
Specifies start of new section on the same page as the previous section.
@ Center
Text is centered horizontally.