Detailed Description

Represents a run of characters with the same font formatting.

All text of the document is stored in runs of text.

Run can only be a child of Paragraph or inline StructuredDocumentTag.

Examples

Shows how to add a formatted run of text to a document using the object model.

auto doc = MakeObject<Document>();
// Create a new run of text
auto run = MakeObject<Run>(doc, u"Hello");
// Specify character formatting for the run of text
SharedPtr<Aspose::Words::Font> f = run->get_Font();
f->set_Name(u"Courier New");
f->set_Size(36);
f->set_HighlightColor(System::Drawing::Color::get_Yellow());
// Append the run of text to the end of the first paragraph
// in the body of the first section of the document
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(run);

Shows how to add, update and delete child nodes from a CompositeNode's child collection.

auto doc = MakeObject<Document>();
// An empty document has one paragraph by default
ASSERT_EQ(1, doc->get_FirstSection()->get_Body()->get_Paragraphs()->get_Count());
// A paragraph is a composite node because it can contain runs, which are another type of node
SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
auto paragraphText = MakeObject<Run>(doc, u"Initial text. ");
paragraph->AppendChild(paragraphText);
// We will place these 3 children into the main text of our paragraph
auto run1 = MakeObject<Run>(doc, u"Run 1. ");
auto run2 = MakeObject<Run>(doc, u"Run 2. ");
auto run3 = MakeObject<Run>(doc, u"Run 3. ");
// We initialized them but not in our paragraph yet
ASSERT_EQ(u"Initial text.", paragraph->GetText().Trim());
// Insert run2 before initial paragraph text. This will be at the start of the paragraph
paragraph->InsertBefore(run2, paragraphText);
// Insert run3 after initial paragraph text. This will be at the end of the paragraph
paragraph->InsertAfter(run3, paragraphText);
// Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1
paragraph->PrependChild(run1);
ASSERT_EQ(u"Run 1. Run 2. Initial text. Run 3.", paragraph->GetText().Trim());
ASSERT_EQ(4, paragraph->GetChildNodes(Aspose::Words::NodeType::Any, true)->get_Count());
// Access the child node collection and update/delete children
(System::DynamicCast<Aspose::Words::Run>(paragraph->GetChildNodes(Aspose::Words::NodeType::Run, true)->idx_get(1)))->set_Text(u"Updated run 2. ");
paragraph->GetChildNodes(Aspose::Words::NodeType::Run, true)->Remove(paragraphText);
ASSERT_EQ(u"Run 1. Updated run 2. Run 3.", paragraph->GetText().Trim());
ASSERT_EQ(3, paragraph->GetChildNodes(Aspose::Words::NodeType::Any, true)->get_Count());

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(Aspose::Words::SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(Aspose::Words::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(Aspose::Words::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");

#include <Aspose.Words.Cpp/Model/Text/Run.h>

+ Inheritance diagram for Aspose::Words::Run:

Public Member Functions

 Run (SharedPtr< DocumentBase > doc)
 Initializes a new instance of the Run class. More...
 
 Run (SharedPtr< DocumentBase > doc, String text)
 Initializes a new instance of the Run class. More...
 
bool Accept (SharedPtr< DocumentVisitor > visitor) override
 Accepts a visitor. More...
 
NodeType get_NodeType () const override
 Returns NodeType.Run. More...
 
String get_Text () const
 Gets or sets the text of the run. More...
 
String GetText () override
 Gets the text of the run. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_Text (String value)
 Setter for get_Text. More...
 
- Public Member Functions inherited from Inline
SharedPtr< Fontget_Font ()
 Provides access to the font formatting of this object. More...
 
bool get_IsDeleteRevision ()
 Returns true if this object was deleted in Microsoft Word while change tracking was enabled. More...
 
bool get_IsFormatRevision ()
 Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled. More...
 
bool get_IsInsertRevision ()
 Returns true if this object was inserted in Microsoft Word while change tracking was enabled. More...
 
bool get_IsMoveFromRevision ()
 Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled. More...
 
bool get_IsMoveToRevision ()
 Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled. More...
 
SharedPtr< Paragraphget_ParentParagraph ()
 Retrieves the parent Paragraph of this node. More...
 
- Public Member Functions inherited from Node
SharedPtr< NodeClone (bool isCloneChildren)
 Creates a duplicate of the node. More...
 
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...
 
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 (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 const TypeInfoType ()
 
- Static Public Member Functions inherited from Inline
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 ()
 
- 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
 

Constructor & Destructor Documentation

◆ Run() [1/2]

Aspose::Words::Run::Run ( System::SharedPtr< Aspose::Words::DocumentBase doc)

Initializes a new instance of the Run class.

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

To append Run to the document use InsertAfter or InsertBefore on the paragraph where you want the run inserted.

Parameters
docThe owner document.
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(Aspose::Words::SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(Aspose::Words::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(Aspose::Words::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");

◆ Run() [2/2]

Aspose::Words::Run::Run ( System::SharedPtr< Aspose::Words::DocumentBase doc,
System::String  text 
)

Initializes a new instance of the Run class.

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

To append Run to the document use InsertAfter or InsertBefore on the paragraph where you want the run inserted.

Parameters
docThe owner document.
textThe text of the run.
Examples

Shows how to add a formatted run of text to a document using the object model.

auto doc = MakeObject<Document>();
// Create a new run of text
auto run = MakeObject<Run>(doc, u"Hello");
// Specify character formatting for the run of text
SharedPtr<Aspose::Words::Font> f = run->get_Font();
f->set_Name(u"Courier New");
f->set_Size(36);
f->set_HighlightColor(System::Drawing::Color::get_Yellow());
// Append the run of text to the end of the first paragraph
// in the body of the first section of the document
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(run);

Member Function Documentation

◆ Accept()

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

Accepts a visitor.

Calls DocumentVisitor.VisitRun.

For more info see the Visitor design pattern.

Parameters
visitorThe visitor that will visit the node.
Returns
False if the visitor requested the enumeration to stop.

Implements Aspose::Words::Node.

◆ get_NodeType()

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

Returns NodeType.Run.

Implements Aspose::Words::Node.

◆ get_Text()

System::String Aspose::Words::Run::get_Text ( ) const

Gets or sets the text of the run.

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(Aspose::Words::SectionStart::NewPage);
section->get_PageSetup()->set_PaperSize(Aspose::Words::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(Aspose::Words::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");

◆ GetText()

System::String Aspose::Words::Run::GetText ( )
overridevirtual

Gets the text of the run.

Returns
The text of the run.

Reimplemented from Aspose::Words::Node.

◆ GetType()

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

Reimplemented from Aspose::Words::Inline.

◆ Is()

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

Reimplemented from Aspose::Words::Inline.

◆ set_Text()

void Aspose::Words::Run::set_Text ( System::String  value)

◆ Type()

static const System::TypeInfo& Aspose::Words::Run::Type ( )
static
String
static constexpr char16_t SectionBreakChar
End of section character: (char)12 or "\f".
Definition: ControlChar.h:1255
static ASPOSECPP_SHARED_API Color get_Yellow()
@ Letter
8.5 x 11 inches.
@ NewPage
The section starts from a new page.
void set_Text(System::String value)
Setter for Aspose::Words::Run::get_Text.
static ASPOSECPP_SHARED_API Color get_Red()
@ Run
A run of text.
@ Any
Indicates all node types. Allows to select all children.
@ Center
Text is centered horizontally.