How to Work with Text in .NET
This guide shows how to work with text in Aspose.Words FOSS for .NET: formatting a run’s font, formatting a paragraph, finding and replacing text with Range, adding a comment, and turning a paragraph into a list item. Aspose.Words FOSS for .NET is not yet published as a NuGet package, so reference it by building from source first.
Step-by-Step Guide
Step 1: Get the Library
Clone the repository and build Aspose.Words.sln in Release configuration, then add a project reference to Aspose.Words.csproj from your own .NET project, as described in the Installation guide. Add using Aspose.Words; at the top of any file that works with text.
Step 2: Format a Run’s Font
A Run is a span of text sharing one set of formatting; its Text property holds the plain-text content and its Font property (a Font object) holds character-level formatting — Name, Size, Bold, Italic, Underline, Color, HighlightColor, Subscript/Superscript, and StrikeThrough among others. Font.ClearFormatting() resets a run back to defaults. A paragraph’s runs are available through its Runs property, a RunCollection. Setting font properties on DocumentBuilder.Font before writing new text is usually simpler than formatting individual Run objects after the fact.
Step 3: Format a Paragraph
Paragraph.ParagraphFormat (a ParagraphFormat object) controls paragraph-level layout: Alignment, indents (LeftIndent, RightIndent, FirstLineIndent), spacing (SpaceBefore, SpaceAfter, LineSpacing, LineSpacingRule), and pagination behavior (KeepTogether, KeepWithNext, PageBreakBefore, WidowControl). ParagraphFormat.ClearFormatting() resets these to defaults, and Paragraph.JoinRunsWithSameFormatting() merges adjacent runs that carry identical formatting — useful after a series of programmatic edits leaves a paragraph with more runs than necessary.
Step 4: Find and Replace Text with Range
Every content-bearing node — a Document, Section, Paragraph, or Run — exposes a Range. Range.Text returns the plain text of everything inside that range, and Range.Replace(pattern, replacement) performs a find-and-replace scoped to just that range: call it on a Paragraph’s Range to change text in one paragraph only, or on Document.Range to search the whole document. Range.Delete() removes the range’s content outright.
Step 5: Add a Comment
Construct a Comment with new Comment(doc, author, initial, dateTime), set its text with SetText(text), then insert it into the tree like any other node. Comment.AddReply(author, initial, dateTime, text) attaches a threaded reply — itself a Comment — and RemoveReply(reply) / RemoveAllReplies() take replies back off. A document’s comments are collected in a CommentCollection.
Step 6: Turn a Paragraph into a List Item
Paragraph.ListFormat (a ListFormat object) reports and controls whether a paragraph participates in a list: IsListItem and ListLevelNumber report its current state. ApplyBulletDefault() turns the paragraph into a default bulleted item and ApplyNumberDefault() into a default numbered item; ListIndent() / ListOutdent() change its nesting level, and RemoveNumbers() turns it back into a plain paragraph.
Common Issues and Fixes
Formatting one Run’s font doesn’t change the adjacent text.
Adjacent text is a different Run with its own Font instance. Either locate and format every affected Run, or set formatting on DocumentBuilder.Font before writing new text.
Range.Replace changes text outside the area intended.
Replace was called on Document.Range instead of a narrower node’s range. Call it on the specific Paragraph or Section whose text should change.
A paragraph looks like a bulleted list item but IsListItem is false.
The bullet is typed text, not real list formatting. Apply it with ListFormat.ApplyBulletDefault() instead.
The document ends up with far more Run nodes than expected.
Repeated small programmatic edits split runs without rejoining them. Call Paragraph.JoinRunsWithSameFormatting() after a batch of edits.
Frequently Asked Questions
What’s the difference between Run and Paragraph?
A Run is a span of text sharing one set of character formatting (Font). A Paragraph is the composite node containing one or more runs plus paragraph-level formatting (ParagraphFormat).
How do I read all the text in a document or a single section?
Read the Text property of that node’s Range — document.Range.Text or section.Range.Text.
How do I replace text inside just one paragraph, not the whole document?
Call Replace(pattern, replacement) on that Paragraph’s Range, not on Document.Range.
How do I reply to an existing comment?
Call Comment.AddReply(author, initial, dateTime, text) on the original Comment instance.
How do I turn a numbered list item back into a plain paragraph?
Call RemoveNumbers() on that paragraph’s ListFormat.