Class TextContent
Represents a single run of text within a text content element.
Implements
Inherited Members
Namespace: IronWord.Models
Assembly: IronWord.dll
Syntax
public class TextContent : ContentElement, ITextContentElement, IWordTextObject
TextContent is the run of text you hold whenever you add, find, or restyle words inside a Word document. It carries the characters and the formatting that travel with them, so a single object answers both "what does this say" and "how does it look" when you build a paragraph in C#.
A developer rarely constructs one in isolation. Paragraph.AddText and Run.AddText return a TextContent, FindText hands one back when you search, and Run accepts a params TextContent[] of them. Once you hold the object it slots into the document's element tree, ready to be appended to, split, or styled before the file is saved.
Set the words through the Text property and the formatting through Style, which is a TextStyle covering bold, italic, color, font, and the effect stack. Convenience accessors expose the resolved formatting directly so you can read it without reaching into the style object: IsBold and IsItalic report the toggles, Color gives the fill, and Font, FontName, FontSize, and FontStyle describe the typeface. These accessors read the effective look, which is useful when you inspect a run that came back from a search rather than one you just built.
For editing existing copy, the run carries its own methods. Append joins another run onto this one and returns the combined run, Split breaks a run on a delimiter into a List<TextContent>, Find returns the offsets where a search string occurs, and Replace swaps text in place and hands back the updated element. ToJson and ToString serialize the run for logging, diffing, or inspection while you debug a document tree.
using IronWord.Models;
var run = new TextContent("Quarterly Report");
run.Style = new TextStyle { IsBold = true, FontSize = 18 };The add text how-to walks through placing runs in a document, the style text how-to applies formatting, and the replace words how-to shows find-and-replace on existing copy.
Constructors
TextContent()
Initializes a new instance of the Text class.
Declaration
public TextContent()
TextContent(String)
Initializes a new instance of the Text class with the specified text content.
Declaration
public TextContent(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | text | The initial text content of the Text object. |
Properties
Color
The color of the text.
Declaration
public Color Color { get; }
Property Value
| Type | Description |
|---|---|
| System.Drawing.Color |
Font
Declaration
public IFont Font { get; }
Property Value
| Type | Description |
|---|---|
| IronSoftware.Abstractions.Word.IFont |
FontName
Declaration
public string FontName { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
FontSize
Declaration
public float FontSize { get; }
Property Value
| Type | Description |
|---|---|
| System.Single |
FontStyle
Declaration
public string FontStyle { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
IsBold
Declaration
public bool IsBold { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
IsItalic
Declaration
public bool IsItalic { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Style
Style for text.
Declaration
public TextStyle Style { get; set; }
Property Value
| Type | Description |
|---|---|
| TextStyle |
Text
Gets or sets the text content of the Run.
Declaration
public string Text { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Methods
Append(TextContent)
Appends the text content to this Text content.
Declaration
public TextContent Append(TextContent textContent)
Parameters
| Type | Name | Description |
|---|---|---|
| TextContent | textContent | The text content to append. |
Returns
| Type | Description |
|---|---|
| TextContent |
Find(String, Nullable<RegexOptions>, Boolean, Boolean)
Finds the indices of the specified search text within the text.
Declaration
public List<int> Find(string searchText, Nullable<RegexOptions> regexOption = null, bool wholeWordOnly = false, bool caseSensitive = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | searchText | The text to search for. |
| System.Nullable<System.Text.RegularExpressions.RegexOptions> | regexOption | Optional. The regular expression options. |
| System.Boolean | wholeWordOnly | Optional. Indicates whether to match the whole word only. |
| System.Boolean | caseSensitive | Optional. Indicates whether the search is case-sensitive. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<System.Int32> | A list of indices where the search text is found. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown when |
Replace(String, String, Nullable<RegexOptions>, Boolean, Boolean)
Finds and replaces text within this TextContentElement.
Declaration
public ITextContentElement Replace(string searchText, string replaceText, Nullable<RegexOptions> regexOption = null, bool wholeWordOnly = false, bool caseSensitive = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | searchText | The text to search for. |
| System.String | replaceText | The text to replace with. |
| System.Nullable<System.Text.RegularExpressions.RegexOptions> | regexOption | Optional regex options |
| System.Boolean | wholeWordOnly | Replace whole words only |
| System.Boolean | caseSensitive | Whether the search is case-sensitive |
Returns
| Type | Description |
|---|---|
| ITextContentElement |
Split(String)
Splits the current Run into smaller Runs based on a delimiter.
Declaration
public List<TextContent> Split(string delimiter)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | delimiter | The delimiter to use for splitting. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<TextContent> | List of Run objects after split. |
ToJson()
Declaration
public string ToJson()
Returns
| Type | Description |
|---|---|
| System.String |
ToString()
Provides a textual representation of the TextContentElement.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | A string representation of the element. |