Run Class

Represents a run of characters with the same font formatting.
Inheritance Hierarchy

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class Run : Inline

The Run type exposes the following members.

Constructors
  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.
Properties
  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.
Methods
  NameDescription
Public methodCode exampleAccept
Accepts a visitor.
(Overrides NodeAccept(DocumentVisitor).)
Public methodCode exampleClone (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.)
Remarks

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

Run can only be a child of Paragraph.

Examples
Shows how to add a formatted run of text to a document using the object model.
// Create an empty document. It contains one empty paragraph
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);
Examples
Gets all fonts used in a document.
Document doc = new Document(MyDir + "Rendering.docx");

// Select all runs in the document
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);

// Use a hashtable so we will keep only unique font names
Hashtable fontNames = new Hashtable();

foreach (Run run in runs.OfType<Run>())
{
    // This adds an entry into the hashtable
    // The key is the font name. The value is null, we don't need the value
    fontNames[run.Font.Name] = null;
}

// There are two fonts used in this document
Console.WriteLine("Font Count: " + fontNames.Count);
Examples
Creates a simple document from scratch using the Aspose.Words object model.
// Create an "empty" document. Note that like in Microsoft Word, 
// the empty document has one section, body and one paragraph in it
Document doc = new Document();

// This truly makes the document empty. No sections (not possible in Microsoft Word)
doc.RemoveAllChildren();

// Create a new section node
// Note that the section has not yet been added to the document, 
// but we have to specify the parent document
Section section = new Section(doc);

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

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

// The section that we created is empty, lets populate it. The section needs at least the Body node
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;

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

// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character
Console.WriteLine("Hello World!\x000c");

// Save the document
doc.Save(ArtifactsDir + "Section.CreateFromScratch.doc");
See Also