Run

Run class

Represents a run of characters with the same font formatting.

public class Run : Inline

Constructors

Name Description
Run(DocumentBase) Initializes a new instance of the Run class.
Run(DocumentBase, string) Initializes a new instance of the Run class.

Properties

Name Description
CustomNodeId { get; set; } Specifies custom node identifier.
virtual Document { get; } Gets the document to which this node belongs.
Font { get; } Provides access to the font formatting of this object.
virtual IsComposite { get; } Returns true if this node can contain other nodes.
IsDeleteRevision { get; } Returns true if this object was deleted in Microsoft Word while change tracking was enabled.
IsFormatRevision { get; } Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled.
IsInsertRevision { get; } Returns true if this object was inserted in Microsoft Word while change tracking was enabled.
IsMoveFromRevision { get; } Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.
IsMoveToRevision { get; } Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.
NextSibling { get; } Gets the node immediately following this node.
override NodeType { get; } Returns NodeType.Run.
ParentNode { get; } Gets the immediate parent of this node.
ParentParagraph { get; } Retrieves the parent Paragraph of this node.
PreviousSibling { get; } Gets the node immediately preceding this node.
Range { get; } Returns a Range object that represents the portion of a document that is contained in this node.
Text { get; set; } Gets or sets the text of the run.

Methods

Name Description
override Accept(DocumentVisitor) Accepts a visitor.
Clone(bool) Creates a duplicate of the node.
GetAncestor(NodeType) Gets the first ancestor of the specified NodeType.
GetAncestor(Type) Gets the first ancestor of the specified object type.
override GetText() Gets the text of the run.
NextPreOrder(Node) Gets next node according to the pre-order tree traversal algorithm.
PreviousPreOrder(Node) Gets the previous node according to the pre-order tree traversal algorithm.
Remove() Removes itself from the parent.
ToString(SaveFormat) Exports the content of the node into a string in the specified format.
ToString(SaveOptions) Exports the content of the node into a string using the specified save options.

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.

Document doc = new Document();
Run run = new Run(doc, "Hello world!");

Aspose.Words.Font font = run.Font;
font.Name = "Courier New";
font.Size = 36;
font.HighlightColor = Color.Yellow;

doc.FirstSection.Body.FirstParagraph.AppendChild(run);
doc.Save(ArtifactsDir + "Font.CreateFormattedRun.docx");

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

Document doc = new 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.
Section section = new Section(doc);
doc.AppendChild(section);

// Set some page setup properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.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.
Body body = new Body(doc);
section.AppendChild(body);

// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);

para.ParagraphFormat.StyleName = "Heading 1";
para.ParagraphFormat.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.
Run run = new Run(doc);
run.Text = "Hello World!";
run.Font.Color = Color.Red;
para.AppendChild(run);

Assert.AreEqual("Hello World!", doc.GetText().Trim());

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

Shows how to add, update and delete child nodes in a CompositeNode’s collection of children.

Document doc = new Document();

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

// Composite nodes such as our paragraph can contain other composite and inline nodes as children.
Paragraph paragraph = doc.FirstSection.Body.FirstParagraph;
Run paragraphText = new Run(doc, "Initial text. ");
paragraph.AppendChild(paragraphText);

// Create three more run nodes.
Run run1 = new Run(doc, "Run 1. ");
Run run2 = new Run(doc, "Run 2. ");
Run run3 = new Run(doc, "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.AreEqual("Initial text.", paragraph.GetText().Trim());

// Insert the second run into the paragraph in front of the initial run.
paragraph.InsertBefore(run2, paragraphText);

Assert.AreEqual("Run 2. Initial text.", paragraph.GetText().Trim());

// Insert the third run after the initial run.
paragraph.InsertAfter(run3, paragraphText);

Assert.AreEqual("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.AreEqual("Run 1. Run 2. Initial text. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(4, paragraph.GetChildNodes(NodeType.Any, true).Count);

// We can modify the contents of the run by editing and deleting existing child nodes.
((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);

See Also