Run Class

Represents a run of characters with the same font formatting.

ExpandedInheritance Hierarchy

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.10.0

ExpandedSyntax

public class Run : Inline

The Run type exposes the following members.

ExpandedConstructors

  NameDescription
Public methodCode exampleRun(DocumentBase)
Initializes a new instance of the Run class.
Public methodCode exampleRun(DocumentBase, String)
Initializes a new instance of the Run class.

ExpandedProperties

  NameDescription
Public propertyCode exampleDocument
Gets the document to which this node belongs.
(Inherited from Node.)
Public propertyCode exampleFont
Provides access to the font formatting of this object.
(Inherited from Inline.)
Public propertyCode exampleIsComposite
Returns true if this node can contain other nodes.
(Inherited from Node.)
Public propertyCode exampleIsDeleteRevision
Returns true if this object was deleted in Microsoft Word while change tracking was enabled.
(Inherited from Inline.)
Public propertyCode exampleIsFormatRevision
Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled.
(Inherited from Inline.)
Public propertyCode exampleIsInsertRevision
Returns true if this object was inserted in Microsoft Word while change tracking was enabled.
(Inherited from Inline.)
Public propertyCode exampleIsMoveFromRevision
Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.
(Inherited from Inline.)
Public propertyCode exampleIsMoveToRevision
Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.
(Inherited from Inline.)
Public propertyCode exampleNextSibling
Gets the node immediately following this node.
(Inherited from Node.)
Public propertyCode exampleNodeType
Returns NodeType.Run.
(Overrides NodeNodeType.)
Public propertyCode exampleParentNode
Gets the immediate parent of this node.
(Inherited from Node.)
Public propertyCode exampleParentParagraph
Retrieves the parent Paragraph of this node.
(Inherited from Inline.)
Public propertyCode examplePreviousSibling
Gets the node immediately preceding this node.
(Inherited from Node.)
Public propertyCode exampleRange
Returns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node.)
Public propertyCode exampleText
Gets or sets the text of the run.

ExpandedMethods

  NameDescription
Public methodCode exampleAccept
Accepts a visitor.
(Overrides NodeAccept(DocumentVisitor).)
Public methodCode exampleClone
Creates a duplicate of the node.
(Inherited from Node.)
Public methodEquals (Inherited from Object.)
Public methodCode exampleGetAncestor(Type)
Gets the first ancestor of the specified object type.
(Inherited from Node.)
Public methodCode exampleGetAncestor(NodeType)
Gets the first ancestor of the specified NodeType.
(Inherited from Node.)
Public methodGetHashCode (Inherited from Object.)
Public methodCode exampleGetText
Gets the text of the run.
(Overrides NodeGetText.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleNextPreOrder
Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodCode examplePreviousPreOrder
Gets the previous node according to the pre-order tree traversal algorithm.
(Inherited from Node.)
Public methodCode exampleRemove
Removes itself from the parent.
(Inherited from Node.)
Public methodToString (Inherited from Object.)
Public methodCode exampleToString(SaveFormat)
Exports the content of the node into a string in the specified format.
(Inherited from Node.)
Public methodCode exampleToString(SaveOptions)
Exports the content of the node into a string using the specified save options.
(Inherited from Node.)

ExpandedRemarks

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

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

ExpandedExamples

Shows how to add a formatted run of text to a document using the object model.
Document doc = new Document();

// Create a new run of text
Run run = new Run(doc, "Hello");

// Specify character formatting for the run of text
Aspose.Words.Font f = run.Font;
f.Name = "Courier New";
f.Size = 36;
f.HighlightColor = Color.Yellow;

// Append the run of text to the end of the first paragraph
// in the body of the first section of the document
doc.FirstSection.Body.FirstParagraph.AppendChild(run);
Shows how to add, update and delete child nodes from a CompositeNode's child collection.
Document doc = new Document();

// An empty document has one paragraph by default
Assert.AreEqual(1, doc.FirstSection.Body.Paragraphs.Count);

// A paragraph is a composite node because it can contain runs, which are another type of node
Paragraph paragraph = doc.FirstSection.Body.FirstParagraph;
Run paragraphText = new Run(doc, "Initial text. ");
paragraph.AppendChild(paragraphText);

// We will place these 3 children into the main text of our paragraph
Run run1 = new Run(doc, "Run 1. ");
Run run2 = new Run(doc, "Run 2. ");
Run run3 = new Run(doc, "Run 3. ");

// We initialized them but not in our paragraph yet
Assert.AreEqual("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.AreEqual("Run 1. Run 2. Initial text. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(4, paragraph.GetChildNodes(NodeType.Any, true).Count);

// Access the child node collection and update/delete children
((Run)paragraph.GetChildNodes(NodeType.Run, true)[1]).Text = "Updated run 2. ";
paragraph.GetChildNodes(NodeType.Run, true).Remove(paragraphText);

Assert.AreEqual("Run 1. Updated run 2. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, true).Count);
Shows how to construct an Aspose Words document node by node.
Document doc = new 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
Section section = new Section(doc);

// Append the section to the document
doc.AppendChild(section);

// Set some properties for the section
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;

// A section needs a body, which will contain all other nodes that can be edited
Body body = new 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
Paragraph para = new Paragraph(doc);
body.AppendChild(para);

// We can set some formatting for the paragraph
para.ParagraphFormat.StyleName = "Heading 1";
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;

// Now we can begin adding content to the document
Run run = new Run(doc);
run.Text = "Hello World!";
run.Font.Color = Color.Red;
para.AppendChild(run);

Assert.AreEqual("Hello World!" + ControlChar.SectionBreakChar, doc.GetText());

doc.Save(ArtifactsDir + "Section.CreateFromScratch.docx");

ExpandedSee Also