How to Add Style to Text in DOCX with C#
IronWord's TextStyle class enables .NET developers to apply professional text formatting to Word documents programmatically, including fonts, colors, bold, italic, underline, and more. Whether you're generating reports, creating templates, or automating document creation, IronWord provides comprehensive styling tools that replicate Microsoft Word's formatting options.
Quickstart: Style Text in DOCX with C#
-
Install IronWord with NuGet Package Manager
PM > Install-Package IronWord -
Copy and run this code snippet.
// Quick example using IronWord; using IronWord.Models; // Initialize a new Word document WordDocument doc = new WordDocument(); // Create a Run with styled text Run textRun = new Run(new TextContent("Styled text")); // Apply styling properties to the Run textRun.Style = new TextStyle() { IsBold = true, Color = Color.Red, FontSize = 16, TextFont = new Font() { FontFamily = "Arial" } }; // Create paragraph and add the styled Run Paragraph paragraph = new Paragraph(); paragraph.AddChild(textRun); // Add paragraph to document and save doc.AddParagraph(paragraph); doc.SaveAs("styled.docx"); -
Deploy to test on your live environment
Start using IronWord in your project today with a free trial
Minimal Workflow (5 steps)
- Install the IronWord C# library
- Create a
Runobject containingTextContent - Apply
TextStyleto theRunwith properties likeFontSize,IsBold,Color - Add the styled
Runto aParagraphusingAddChild - Save the document with applied styles
How Do I Add Text Style to DOCX?
Applying text styles in IronWord requires using the Run wrapper pattern. Create a WordDocument object, then create a Run object containing TextContent with your text. Apply a TextStyle to the Run (not the TextContent) using properties such as IsBold, Color, or FontSize.
Once styled, add the Run to a Paragraph using AddChild, insert the paragraph into the document, and save the result. This approach provides programmatic control over text formatting for automated document generation scenarios requiring consistent styling.
:path=/static-assets/word/content-code-examples/how-to/add-style-text-simple.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("sample.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
// Configure text style settings
textRun.Style = new TextStyle()
{
FontSize = 24, // Text Size is 24
TextFont = new Font()
{
FontFamily = "Calibri" // Text Font is "Calibri"
},
Color = Color.Red, // Set text color to red
IsBold = true, // Make text bold
IsItalic = true, // Make text italic
Underline = new Underline(), // Have an underline
Strike = StrikeValue.DoubleStrike, // No strike-through
};
Paragraph paragraph = new Paragraph();
// Add text to paragraph
paragraph.AddChild(textRun);
// Add paragraph to document
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("add-text-style.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("sample.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
' Configure text style settings
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Text Size is 24
.TextFont = New Font() With {
.FontFamily = "Calibri" ' Text Font is "Calibri"
},
.Color = Color.Red, ' Set text color to red
.IsBold = True, ' Make text bold
.IsItalic = True, ' Make text italic
.Underline = New Underline(), ' Have an underline
.Strike = StrikeValue.DoubleStrike ' No strike-through
}
Dim paragraph As New Paragraph()
' Add text to paragraph
paragraph.AddChild(textRun)
' Add paragraph to document
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("add-text-style.docx")
What Output Will This Generate?
The TextStyle class provides essential formatting options including font properties, text color, bold, italic, and underline. Note that FontSize is configured at the TextStyle level (not inside Font), while FontFamily is set within the TextFont property. The Run object wraps the TextContent and holds the style, following IronWord's document hierarchy pattern. The example demonstrates how multiple styling properties combine to create richly formatted text when applied to a Run.
What Specific Styles Can I Add?
How Do I Change Text Color?
The Color property in TextStyle sets text color using predefined colors from IronWord.Models.Color or custom hex values. This emphasizes specific content or matches brand colors. IronWord supports a wide range of colors including Red, Blue, Green, Olive, Navy, and Maroon.
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-text.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add colored text
Run textRun = new Run(new TextContent("This text is olive-colored!"));
textRun.Style = new TextStyle()
{
Color = IronWord.Models.Color.Olive // defining text to be colored olive
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("colored-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add colored text
Dim textRun As New Run(New TextContent("This text is olive-colored!"))
textRun.Style = New TextStyle() With {
.Color = IronWord.Models.Color.Olive ' defining text to be colored olive
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("colored-text.docx")
What Does Colored Text Look Like?
How Do I Set Font Family and Size?
Customize text appearance with the TextFont property. Set FontFamily to any installed font name (e.g., "Arial", "Times New Roman") and FontSize in points. This establishes visual hierarchy and ensures readability across different devices and platforms.
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-font.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add text with custom font family and size
Run textRun = new Run(new TextContent("This text uses Arial at 24pt!"));
textRun.Style = new TextStyle()
{
FontSize = 24, // Set font size in points
TextFont = new IronWord.Models.Font()
{
FontFamily = "Arial" // Set font family
}
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("font-styled-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add text with custom font family and size
Dim textRun As New Run(New TextContent("This text uses Arial at 24pt!"))
textRun.Style = New TextStyle() With {
.FontSize = 24, ' Set font size in points
.TextFont = New IronWord.Models.Font() With {
.FontFamily = "Arial" ' Set font family
}
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("font-styled-text.docx")
What Does Custom Font Styling Look Like?
How Do I Make Text Bold?
Set the IsBold property to true to make text bold. Bold text is commonly used for headings, emphasis, or highlighting important information. Combined with other styling properties, bold text creates visual hierarchy and improves readability.
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-bold.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add bold text
Run textRun = new Run(new TextContent("this is bold!"));
textRun.Style = new TextStyle()
{
IsBold = true // Make text bold
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("bold-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add bold text
Dim textRun As New Run(New TextContent("this is bold!"))
textRun.Style = New TextStyle() With {
.IsBold = True ' Make text bold
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("bold-text.docx")
What Does Bold Text Look Like?
How Do I Make Text Italic?
Set the IsItalic property to true for italic styling. Italic text is typically used for emphasis, titles, foreign words, or technical terms. This subtle formatting differentiates text elements without the visual weight of bold formatting.
:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-italic.cs
using IronWord;
using IronWord.Models;
// Create document
WordDocument doc = new WordDocument();
// Add italic text
Run textRun = new Run(new TextContent("this is italic."));
textRun.Style = new TextStyle()
{
IsItalic = true // Make text italic
};
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
doc.AddParagraph(paragraph);
// Save document
doc.SaveAs("italic-text.docx");
Imports IronWord
Imports IronWord.Models
' Create document
Dim doc As New WordDocument()
' Add italic text
Dim textRun As New Run(New TextContent("this is italic."))
textRun.Style = New TextStyle() With {
.IsItalic = True ' Make text italic
}
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
doc.AddParagraph(paragraph)
' Save document
doc.SaveAs("italic-text.docx")
What Does Italic Text Look Like?
What Are the Available Styling Properties?
IronWord provides comprehensive styling properties mirroring Microsoft Word's formatting options. These properties combine to create complex text formatting meeting professional document standards.
| Styling Method | Description | Example |
|---|---|---|
TextFont |
Customizes text appearance with a Font object, setting font family. Note: FontSize is set at TextStyle level, not inside Font. |
textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
Color |
Sets text color using predefined colors from IronWord.Models.Color or custom hex values. |
textRun.Style.Color = IronWord.Models.Color.Red; |
IsBold |
Makes text bold when set to true, commonly used for headings or emphasis. |
textRun.Style.IsBold = true; |
IsItalic |
Applies italic styling to text when set to true, typically used for emphasis or titles. |
textRun.Style.IsItalic = true; |
Underline |
Adds an underline to text using an Underline object with various underline styles. |
textRun.Style.Underline = new Underline(); |
Strike |
Applies strikethrough to text using StrikeValue enum (Strike or DoubleStrike). |
textRun.Style.Strike = StrikeValue.Strike; |
Caps |
Applies capitalization effects to text, converting all characters to uppercase display. | textRun.Style.Caps = true; |
CharacterScale |
Adjusts the proportional width of characters as a percentage of their normal size. | textRun.Style.CharacterScale = 150; |
Emboss |
Applies an embossed effect to text, creating a raised appearance. | textRun.Style.Emboss = true; |
Emphasis |
Adds emphasis marks to styled text using EmphasisMarkValues enum values. |
textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
LineSpacing |
Controls spacing between lines of text for improved readability using a LineSpacing object. |
textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
Outline |
Renders text with an outline effect, displaying only the character borders. | textRun.Style.Outline = true; |
Shading |
Applies background color or shading to text using a Shading object. |
textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
SmallCaps |
Converts lowercase letters to small capital letters while maintaining case distinction. | textRun.Style.SmallCaps = true; |
VerticalPosition |
Adjusts vertical placement of text relative to its baseline, measured in points. | textRun.Style.VerticalPosition = 5.0; |
VerticalTextAlignment |
Positions text vertically within its container using VerticalPositionValues enum. |
textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript; |
Combining Multiple Styles
IronWord's text styling power comes from combining multiple properties to achieve complex formatting. Here's an example demonstrating professionally styled text by combining various styling properties:
using IronWord;
using IronWord.Models;
// Create a new document
WordDocument doc = new WordDocument();
// Create richly formatted header text using Run
Run headerRun = new Run(new TextContent("Professional Document Header"));
headerRun.Style = new TextStyle()
{
FontSize = 28,
TextFont = new Font()
{
FontFamily = "Georgia"
},
Color = Color.DarkBlue,
IsBold = true,
SmallCaps = true,
Underline = new Underline(),
CharacterScale = 110, // Slightly expand character width
Shading = new Shading()
{
Color = Color.LightGray // Light background
}
};
// Add header to document using AddChild for styled Run
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddChild(headerRun);
doc.AddParagraph(headerParagraph);
// Create body text with different styling
Run bodyRun = new Run(new TextContent("This is professionally formatted body text with custom styling."));
bodyRun.Style = new TextStyle()
{
FontSize = 11,
TextFont = new Font()
{
FontFamily = "Calibri"
},
Color = Color.Black,
LineSpacing = new LineSpacing() { Value = 1.15 } // Slightly increased line spacing
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");
using IronWord;
using IronWord.Models;
// Create a new document
WordDocument doc = new WordDocument();
// Create richly formatted header text using Run
Run headerRun = new Run(new TextContent("Professional Document Header"));
headerRun.Style = new TextStyle()
{
FontSize = 28,
TextFont = new Font()
{
FontFamily = "Georgia"
},
Color = Color.DarkBlue,
IsBold = true,
SmallCaps = true,
Underline = new Underline(),
CharacterScale = 110, // Slightly expand character width
Shading = new Shading()
{
Color = Color.LightGray // Light background
}
};
// Add header to document using AddChild for styled Run
Paragraph headerParagraph = new Paragraph();
headerParagraph.AddChild(headerRun);
doc.AddParagraph(headerParagraph);
// Create body text with different styling
Run bodyRun = new Run(new TextContent("This is professionally formatted body text with custom styling."));
bodyRun.Style = new TextStyle()
{
FontSize = 11,
TextFont = new Font()
{
FontFamily = "Calibri"
},
Color = Color.Black,
LineSpacing = new LineSpacing() { Value = 1.15 } // Slightly increased line spacing
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");
Imports IronWord
Imports IronWord.Models
' Create a new document
Dim doc As New WordDocument()
' Create richly formatted header text using Run
Dim headerRun As New Run(New TextContent("Professional Document Header"))
headerRun.Style = New TextStyle() With {
.FontSize = 28,
.TextFont = New Font() With {
.FontFamily = "Georgia"
},
.Color = Color.DarkBlue,
.IsBold = True,
.SmallCaps = True,
.Underline = New Underline(),
.CharacterScale = 110, ' Slightly expand character width
.Shading = New Shading() With {
.Color = Color.LightGray ' Light background
}
}
' Add header to document using AddChild for styled Run
Dim headerParagraph As New Paragraph()
headerParagraph.AddChild(headerRun)
doc.AddParagraph(headerParagraph)
' Create body text with different styling
Dim bodyRun As New Run(New TextContent("This is professionally formatted body text with custom styling."))
bodyRun.Style = New TextStyle() With {
.FontSize = 11,
.TextFont = New Font() With {
.FontFamily = "Calibri"
},
.Color = Color.Black,
.LineSpacing = New LineSpacing() With {.Value = 1.15} ' Slightly increased line spacing
}
Dim bodyParagraph As New Paragraph()
bodyParagraph.AddChild(bodyRun)
doc.AddParagraph(bodyParagraph)
' Save the document
doc.SaveAs("professional-document.docx")
This comprehensive styling approach creates documents maintaining consistent branding and professional appearance throughout your application's document generation process.
Frequently Asked Questions
How do I apply text formatting to Word documents programmatically in C#?
IronWord's TextStyle class enables you to apply professional text formatting including fonts, colors, bold, italic, and underline. Simply create a TextContent object with your text, apply a TextStyle with desired properties, add it to a paragraph, and save the document.
What are the basic steps to style text in DOCX files?
To style text with IronWord: 1) Install IronWord via NuGet, 2) Create a WordDocument object, 3) Create TextContent with your text, 4) Apply TextStyle properties like font, color, or bold, 5) Add the text to a paragraph and save.
What text formatting options are available?
IronWord's TextStyle class provides essential formatting options including font properties (FontFamily and FontSize), text color, bold, italic, underline, and strikethrough. These options can be combined to create richly formatted text.
How do I change the font family and size of text?
Use the TextFont property in TextStyle to specify font family and size. Set the FontFamily to fonts like 'Arial' or 'Times New Roman', and FontSize to your desired point size, such as 16 for larger text.
Can I apply multiple text styles simultaneously?
Yes, IronWord allows you to combine multiple styling properties in a single TextStyle object. You can apply bold, italic, color, and font changes all at once to create complex text formatting.
How do I change text color in Word documents using C#?
The Color property in IronWord's TextStyle allows you to set text color using predefined colors from IronWord.Models.Color or custom hex values. This feature helps emphasize specific content or match brand colors in your documents.

