Aspose::Words::Run class
Contents
[
Hide
]Run class
Represents a run of characters with the same font formatting. To learn more, visit the Programming with Documents documentation article.
class Run : public Aspose::Words::Inline
Methods
Method | Description |
---|---|
Accept(System::SharedPtr<Aspose::Words::DocumentVisitor>) override | Accepts a visitor. |
Clone(bool) | Creates a duplicate of the node. |
get_CustomNodeId() const | Specifies custom node identifier. |
virtual get_Document() const | Gets the document to which this node belongs. |
get_Font() | Provides access to the font formatting of this object. |
virtual get_IsComposite() | Returns true if this node can contain other nodes. |
get_IsDeleteRevision() | Returns true if this object was deleted in Microsoft Word while change tracking was enabled. |
get_IsFormatRevision() | Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled. |
get_IsInsertRevision() | Returns true if this object was inserted in Microsoft Word while change tracking was enabled. |
get_IsMoveFromRevision() | Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled. |
get_IsMoveToRevision() | Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled. |
get_IsPhoneticGuide() | Gets a boolean value indicating either the run is a phonetic guide. |
get_NextNode() const | |
get_NextSibling() | Gets the node immediately following this node. |
get_NodeType() const override | Returns Run. |
get_ParentNode() | Gets the immediate parent of this node. |
get_ParentParagraph() | Retrieves the parent Paragraph of this node. |
get_PhoneticGuide() | Gets a PhoneticGuide object. |
get_PreviousSibling() | Gets the node immediately preceding this node. |
get_PrevNode() const | |
get_Range() | Returns a Range object that represents the portion of a document that is contained in this node. |
get_Text() const | Gets or sets the text of the run. |
GetAncestor(Aspose::Words::NodeType) | Gets the first ancestor of the specified NodeType. |
GetAncestorOf() | |
GetText() override | Gets the text of the run. |
GetType() const override | |
Is(const System::TypeInfo&) const override | |
IsAncestorNode(const System::SharedPtr<Aspose::Words::Node>&) | |
NextPreOrder(const System::SharedPtr<Aspose::Words::Node>&) | Gets next node according to the pre-order tree traversal algorithm. |
static NodeTypeToString(Aspose::Words::NodeType) | A utility method that converts a node type enum value into a user friendly string. |
PreviousPreOrder(const System::SharedPtr<Aspose::Words::Node>&) | Gets the previous node according to the pre-order tree traversal algorithm. |
Remove() | Removes itself from the parent. |
Run(const System::SharedPtr<Aspose::Words::DocumentBase>&) | Initializes a new instance of the Run class. |
Run(const System::SharedPtr<Aspose::Words::DocumentBase>&, const System::String&) | Initializes a new instance of the Run class. |
set_CustomNodeId(int32_t) | Setter for Aspose::Words::Node::get_CustomNodeId. |
set_NextNode(const System::SharedPtr<Aspose::Words::Node>&) | |
set_PrevNode(const System::SharedPtr<Aspose::Words::Node>&) | |
set_Text(const System::String&) | Setter for Aspose::Words::Run::get_Text. |
SetParent(const System::SharedPtr<Aspose::Words::Node>&) | |
ToString(Aspose::Words::SaveFormat) | Exports the content of the node into a string in the specified format. |
ToString(const System::SharedPtr<Aspose::Words::Saving::SaveOptions>&) | Exports the content of the node into a string using the specified save options. |
static Type() |
Remarks
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 format a run of text using its font property.
auto doc = MakeObject<Document>();
auto run = MakeObject<Run>(doc, u"Hello world!");
SharedPtr<Aspose::Words::Font> font = run->get_Font();
font->set_Name(u"Courier New");
font->set_Size(36);
font->set_HighlightColor(System::Drawing::Color::get_Yellow());
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(run);
doc->Save(ArtifactsDir + u"Font.CreateFormattedRun.docx");
Shows how to add, update and delete child nodes in a CompositeNode’s collection of children.
auto doc = MakeObject<Document>();
// An empty document, by default, has one paragraph.
ASSERT_EQ(1, doc->get_FirstSection()->get_Body()->get_Paragraphs()->get_Count());
// Composite nodes such as our paragraph can contain other composite and inline nodes as children.
SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
auto paragraphText = MakeObject<Run>(doc, u"Initial text. ");
paragraph->AppendChild(paragraphText);
// Create three more run nodes.
auto run1 = MakeObject<Run>(doc, u"Run 1. ");
auto run2 = MakeObject<Run>(doc, u"Run 2. ");
auto run3 = MakeObject<Run>(doc, u"Run 3. ");
// The document body will not display these runs until we insert them into a composite node
// that itself is a part of the document's node tree, as we did with the first run.
// We can determine where the text contents of nodes that we insert
// appears in the document by specifying an insertion location relative to another node in the paragraph.
ASSERT_EQ(u"Initial text.", paragraph->GetText().Trim());
// Insert the second run into the paragraph in front of the initial run.
paragraph->InsertBefore(run2, paragraphText);
ASSERT_EQ(u"Run 2. Initial text.", paragraph->GetText().Trim());
// Insert the third run after the initial run.
paragraph->InsertAfter(run3, paragraphText);
ASSERT_EQ(u"Run 2. Initial text. Run 3.", paragraph->GetText().Trim());
// Insert the first run to the start of the paragraph's child nodes collection.
paragraph->PrependChild(run1);
ASSERT_EQ(u"Run 1. Run 2. Initial text. Run 3.", paragraph->GetText().Trim());
ASSERT_EQ(4, paragraph->GetChildNodes(NodeType::Any, true)->get_Count());
// We can modify the contents of the run by editing and deleting existing child nodes.
(System::ExplicitCast<Run>(paragraph->GetChildNodes(NodeType::Run, true)->idx_get(1)))->set_Text(u"Updated run 2. ");
paragraph->GetChildNodes(NodeType::Run, true)->Remove(paragraphText);
ASSERT_EQ(u"Run 1. Updated run 2. Run 3.", paragraph->GetText().Trim());
ASSERT_EQ(3, paragraph->GetChildNodes(NodeType::Any, true)->get_Count());
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");
See Also
- Class Inline
- Namespace Aspose::Words
- Library Aspose.Words for C++