FormattedText Class


Formatted Text Class

FormattedText is used to create, edit and format text in a TextNote or to query the text and format properties of a TextNode
Inheritance Hierarchy
System Object
Autodesk.Revit.DB FormattedText

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntax
public class FormattedText : IDisposable

The FormattedText type exposes the following members.

Constructors
Name Description
Public method FormattedText Creates a new default FormattedText object, which contains only a carriage return character and has no formatting.
Public method Code example FormattedText(String) Creates a new FormattedText object with unformatted text.
Top
Properties
Name Description
Public property IsValidObject Specifies whether the .NET object represents a valid Revit entity.
Top
Methods
Name Description
Public method Code example AsTextRange Returns a TextRange object that represents the entire text.
Public method Dispose Releases all resources used by the FormattedText
Public method Equals Determines whether the specified object is equal to the current object.
(Inherited from Object )
Public method Code example Find Returns a text range identifying the first occurrence of the given string within the text, starting from a given index.
Public method GetAllCapsStatus Returns whether All , None or a Mixed of characters in the entire text are in all caps.
Public method GetAllCapsStatus(TextRange) Returns whether All , None or a Mixed set of characters in a given text range are in all caps.
Public method GetBoldStatus Returns whether All , None or a Mixed of characters in the entire text are bold.
Public method GetBoldStatus(TextRange) Returns whether All , None or a Mixed set of characters in a given text range are bold.
Public method GetHashCode Serves as the default hash function.
(Inherited from Object )
Public method GetIndentLevel Returns the indent level of the paragraphs in the text range.
Public method GetItalicStatus Returns whether All , None or a Mixed of characters in the entire text are italic.
Public method GetItalicStatus(TextRange) Returns whether All , None or a Mixed set of characters in a given text range are italic.
Public method GetListStartNumber Returns the list start number of the paragraphs in a given text range.
Public method GetListType Returns the ListType of a paragraph.
Public method GetMaximumIndentLevel Returns the maximum allowed indent level
Public method GetMaximumListStartNumber Returns the maximum allowed list start number.
Public method GetMinimumListStartNumber Returns the minumum allowed list start number.
Public method GetPlainText Returns the entire text in a plain text form.
Public method GetPlainText(TextRange) Returns a substring of the text in a plain text form. The start and end of the substring is identified by a given TextRange .
Public method GetSubscriptStatus Returns whether All , None or a Mixed of characters in the entire text are subscripted.
Public method GetSubscriptStatus(TextRange) Returns whether All , None or a Mixed set of characters in a given text range are subscripted.
Public method GetSuperscriptStatus Returns whether All , None or a Mixed of characters in the entire text are superscripted.
Public method GetSuperscriptStatus(TextRange) Returns whether All , None or a Mixed set of characters in a given text range are superscripted.
Public method GetType Gets the Type of the current instance.
(Inherited from Object )
Public method GetUnderlineStatus Returns whether All , None or a Mixed of characters in the entire text are underlined.
Public method GetUnderlineStatus(TextRange) Returns whether All , None or a Mixed set of characters in a given text range are underlined.
Public method SetAllCapsStatus(Boolean) Sets the characters in the entire text to be in all caps or not.
Public method SetAllCapsStatus(TextRange, Boolean) Sets the characters in a given text range to be in all caps or not.
Public method SetBoldStatus(Boolean) Sets the characters in the entire text to be bold or not bold.
Public method SetBoldStatus(TextRange, Boolean) Sets the characters in a given text range to be bold or not bold.
Public method SetIndentLevel Sets the number of tab stops that the paragraph should be indented.
Public method SetItalicStatus(Boolean) Sets the characters in the entire text to be italic or not italic.
Public method SetItalicStatus(TextRange, Boolean) Sets the characters in a given text range to be italic or not italic.
Public method SetListStartNumber Sets the list start number on the paragraphs in a given text range.
Public method Code example SetListType Sets the ListType of a paragraph.
Public method SetPlainText(String) Sets the entire text with the given text in a plain text form.
Public method SetPlainText(TextRange, String) Sets the text with the given text in a plain text form in a range.
Public method SetSubscriptStatus(Boolean) Sets the characters in the entire text to be subscript or not subscript.
Public method SetSubscriptStatus(TextRange, Boolean) Sets the characters in a given text range to be subscript or not subscript.
Public method SetSuperscriptStatus(Boolean) Sets the characters in the entire text to be superscript or not superscript.
Public method SetSuperscriptStatus(TextRange, Boolean) Sets the characters in a given text range to be superscript or not superscript.
Public method SetUnderlineStatus(Boolean) Sets the characters in the entire text to be underlined or not underlined.
Public method SetUnderlineStatus(TextRange, Boolean) Sets the characters in a given text range to be underlined or not underlined.
Public method ToString Returns a string that represents the current object.
(Inherited from Object )
Top
Remarks

An instance of FormattedText can be obtained from a TextNote (See TextNote.GetFormattedText() ) or from a TextNode (See TextNode.GetFormattedText() )

It is also possible to create a new instance of FormattedText and assign it to a TextNote (See TextNote.SetFormattedText() ) This will result in a TextNote with text with the specified formatting applied

Formatted text can be used to:

Formatted text can be populated with plain text by using its constructor FormattedText(String) that takes a string, or by using the SetPlainText(String) method.

In addition, selected ranges of text can be added, removed, or replaced with the SetPlainText(TextRange, String) method by specifying a TextRange .

Use the Find(String, Int32, Boolean, Boolean) method to find the location of existing text.

Formatted text can have up to 30,000 characters. All characters, except the linefeed character ('\n'), are allowed. This means that you should not use the 'Environment.NewLine' property, since that includes a linefeed character. Use the carriage return character ('\r') to terminate a paragraph. And use a vertical tab character ('\v') to create a new line without terminating the paragraph.

Formatted text allows for individual characters to be formatted. The following formatting can be applied.

  • Bold
  • Italic
  • Underline
  • Superscript/Subscript
  • All Caps
Use SetBoldStatus(TextRange, Boolean) ) , SetItalicStatus(TextRange, Boolean) ) , SetUnderlineStatus(TextRange, Boolean) ) , SetSuperscriptStatus(TextRange, Boolean) ) , SetSubscriptStatus(TextRange, Boolean) ) , or SetAllCapsStatus(TextRange, Boolean) ) to set the character formatting on a range of text.

Use GetBoldStatus(TextRange) ) , GetItalicStatus(TextRange) ) , GetUnderlineStatus(TextRange) ) , GetSuperscriptStatus(TextRange) ) , GetSubscriptStatus(TextRange) ) , or GetAllCapsStatus(TextRange) ) to get the character formatting of a range of text.

Text can be broken up in paragraphs. Paragraphs are terminated by a carriage return character ('\r').

Each paragraph can be indented several levels deep. For each additional level the indentation increments by one tab size. The total indentation is the product of a tab size and the indent level. Use SetIndentLevel(TextRange, Int32) to set the level of indenting up to a maximum indent level that can be obtained from GetMaximumIndentLevel Use GetIndentLevel(TextRange) to find the indent level of a given range of text.

Note that the tab size is determined by the object that will contain the FormattedText.

In the case of a TextNote the tab size is a property of the TextNoteType returned from TextNote.TextNoteType . The tab size can be found by calling the Parameter Guid with TEXT_TAB_SIZE on the TextNoteType obtained from the TextNote

In the case of a TextNode the tab size can be obtained from its TabSize property

Formatted text can also be used to create numbered or bulleted paragraphs with the SetListType(TextRange, ListType) method.

The following ListType options are available:

Paragraphs with a ListType other than None are considered to be 'list' paragraphs. Consecutive list paragraphs with the same indentation level are treated as part of the same list. A list ends when a list paragraph is followed by Note that a list will continue uninterrupted after list paragraphs that have higher indentation level. These paragraphs form a 'sub-list' of the list they interrupt. Sub-lists can have their own sub-sub-lists. The nesting level is only limited by the maximum indent level. Using SetIndentLevel(TextRange, Int32) it is therefore possible to create multi-level lists.

FormattedText will keep lists consistent. That means that list paragraphs will automatically get sequential numbers or letters. It also means that if the list type of one of the paragraphs in a list is changed then that change is propagated to all the paragraphs in that list. Note that this will not affect the list type of any nested sub-lists.

Use a vertical tab character ('\v') to insert a line without a bullet or number. Since this does not end the paragraph this will allow the list to continue to the next paragraph.

Example
public void FormatText(TextNote textNote)
{
    // TextNote created with "New sample text"
    FormattedText formatText = textNote.GetFormattedText();

    // italicize "New"
    TextRange range = new TextRange(0, 3);
    formatText.SetItalicStatus(range, true);

    // make "sample" bold
    range = formatText.Find("sample", 0, false, true);
    if (range.Length > 0)
        formatText.SetBoldStatus(range, true);

    // make "text" underlined
    range = formatText.Find("text", 0, false, true);
    if (range.Length > 0)
        formatText.SetUnderlineStatus(range, true);

    // make all text uppercase
    formatText.SetAllCapsStatus(true);

    textNote.SetFormattedText(formatText);
}
See Also