# 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.
*as-heading:2(Quickstart: Style Text in DOCX with C#)*
```csharp
:title=Quickstart
// 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");
```
<div class="hsg-featured-snippet">
<h3>Minimal Workflow (5 steps)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronWord/">Install the IronWord C# library</a></li>
<li>Create a <code>Run</code> object containing <code>TextContent</code></li>
<li>Apply <code>TextStyle</code> to the <code>Run</code> with properties like <code>FontSize</code>, <code>IsBold</code>, <code>Color</code></li>
<li>Add the styled <code>Run</code> to a <code>Paragraph</code> using <code>AddChild</code></li>
<li>Save the document with applied styles</li>
</ol>
</div>
## 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.
[[i:(IronWord's document hierarchy follows the structure: `Document` → (optional `DocumentSection`) → `Paragraph` → `Run` → `TextContent`. Paragraphs can be added directly to `WordDocument` via `AddParagraph` without an explicit `DocumentSection`, as shown in the examples on this page. Styles are applied at the `Run` level, not on `TextContent` directly.)]]
```csharp
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, // Double 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");
```
### What Output Will This Generate?
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/word/how-to/add-style-text/add-style-text-simple.webp" alt="Microsoft Word interface showing Layout ribbon and document with red strikethrough text formatting applied" class="img-responsive add-shadow" />
</div>
</div>
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`.
<hr />
## 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.
```csharp
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");
```
### What Does Colored Text Look Like?
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/word/how-to/add-style-text/add-style-text-add-text.webp" alt="Microsoft Word showing olive-colored text formatting with Home tab ribbon displaying font and paragraph tools" class="img-responsive add-shadow" />
</div>
</div>
### 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.
```csharp
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");
```
### What Does Custom Font Styling Look Like?
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/word/how-to/add-style-text/add-style-text-add-font.webp" alt="Microsoft Word showing Arial font at 24pt selected in toolbar with formatted sample text displayed" class="img-responsive add-shadow" />
</div>
</div>
### 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.
```csharp
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");
```
### What Does Bold Text Look Like?
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/word/how-to/add-style-text/add-style-text-add-bold.webp" alt="Microsoft Word interface showing bold text formatting with 'this is bold!' text displayed in the document" class="img-responsive add-shadow" />
</div>
</div>
### 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.
```csharp
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");
```
### What Does Italic Text Look Like?
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/word/how-to/add-style-text/add-style-text-add-italic.webp" alt="Word document showing italicized text with Home tab formatting options visible in ribbon interface" class="img-responsive add-shadow" />
</div>
</div>
<hr />
## 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.
<table class="tg">
<thead>
<tr>
<th class="tg-fymr">Styling Method</th>
<th class="tg-fymr">Description</th>
<th class="tg-fymr">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky"><code>TextFont</code></td>
<td class="tg-0pky">Customizes text appearance with a <strong><code>Font</code> object</strong>, setting font family. Note: <code>FontSize</code> is set at <code>TextStyle</code> level, not inside <code>Font</code>.</td>
<td class="tg-0pky"><code>textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } };</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Color</code></td>
<td class="tg-0pky">Sets text color using predefined colors from <strong>IronWord.Models.Color</strong> or custom hex values.</td>
<td class="tg-0pky"><code>textRun.Style.Color = IronWord.Models.Color.Red;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>IsBold</code></td>
<td class="tg-0pky">Makes text <code>Bold</code> when set to <code>true</code>, commonly used for headings or emphasis.</td>
<td class="tg-0pky"><code>textRun.Style.IsBold = true;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>IsItalic</code></td>
<td class="tg-0pky">Applies <code>Italic</code> styling to text when set to <code>true</code>, typically used for emphasis or titles.</td>
<td class="tg-0pky"><code>textRun.Style.IsItalic = true;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Underline</code></td>
<td class="tg-0pky">Adds an <code>Underline</code> to text using a <strong><code>Underline</code> object</strong> with various underline styles.</td>
<td class="tg-0pky"><code>textRun.Style.Underline = new Underline();</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Strike</code></td>
<td class="tg-0pky">Applies strikethrough to text using <strong><code>StrikeValue</code> enum</strong> (<code>Strike</code> or <code>DoubleStrike</code>).</td>
<td class="tg-0pky"><code>textRun.Style.Strike = StrikeValue.Strike;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Caps</code></td>
<td class="tg-0pky">Applies capitalization effects to text, converting all characters to uppercase display.</td>
<td class="tg-0pky"><code>textRun.Style.Caps = true;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>CharacterScale</code></td>
<td class="tg-0pky">Adjusts the proportional width of characters as a percentage of their normal size.</td>
<td class="tg-0pky"><code>textRun.Style.CharacterScale = 150;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Emboss</code></td>
<td class="tg-0pky">Applies an embossed effect to text, creating a raised appearance.</td>
<td class="tg-0pky"><code>textRun.Style.Emboss = true;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Emphasis</code></td>
<td class="tg-0pky">Adds emphasis marks to styled text using <strong><code>EmphasisMarkValues</code> enum</strong> values.</td>
<td class="tg-0pky"><code>textRun.Style.Emphasis = EmphasisMarkValues.Dot;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>LineSpacing</code></td>
<td class="tg-0pky">Controls spacing between lines of text for improved readability using a <strong><code>LineSpacing</code> object</strong>, where <code>Value</code> is a line-height multiplier.</td>
<td class="tg-0pky"><code>textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 };</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Outline</code></td>
<td class="tg-0pky">Renders text with an outline effect, displaying only the character borders.</td>
<td class="tg-0pky"><code>textRun.Style.Outline = true;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>Shading</code></td>
<td class="tg-0pky">Applies background color or shading to text using a <strong><code>Shading</code> object</strong>.</td>
<td class="tg-0pky"><code>textRun.Style.Shading = new Shading() { Color = Color.Yellow };</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>SmallCaps</code></td>
<td class="tg-0pky">Converts lowercase letters to small capital letters while maintaining case distinction.</td>
<td class="tg-0pky"><code>textRun.Style.SmallCaps = true;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>VerticalPosition</code></td>
<td class="tg-0pky">Adjusts vertical placement of text relative to its baseline, measured in points.</td>
<td class="tg-0pky"><code>textRun.Style.VerticalPosition = 5.0;</code></td>
</tr>
<tr>
<td class="tg-0pky"><code>VerticalTextAlignment</code></td>
<td class="tg-0pky">Positions text vertically within its container using <strong><code>VerticalPositionValues</code> enum</strong>.</td>
<td class="tg-0pky"><code>textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript;</code></td>
</tr>
</tbody>
</table>
### 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:
```csharp
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
};
Paragraph bodyParagraph = 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.
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#
1Install IronWord with NuGet Package Manager
PM > Install-Package IronWord
Install-Package IronWord
2Copy and run this code snippet.
// Quick exampleusing IronWord;using IronWord.Models;// Initialize a new Word documentWordDocument doc = new WordDocument();// Create a Run with styled textRun textRun = new Run(new TextContent("Styled text"));// Apply styling properties to the RuntextRun.Style = new TextStyle(){IsBold = true,Color = Color.Red,FontSize = 16,TextFont = new Font() {FontFamily = "Arial" }};// Create paragraph and add the styled RunParagraph paragraph = new Paragraph();paragraph.AddChild(textRun);// Add paragraph to document and savedoc.AddParagraph(paragraph);doc.SaveAs("styled.docx");
// 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");
C#
3Deploy to test on your live environment
Start using IronWord in your project today with a free trial
Apply TextStyle to the Run with properties like FontSize, IsBold, Color
Add the styled Run to a Paragraph using AddChild
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.
Please note: IronWord's document hierarchy follows the structure: Document → (optional DocumentSection) → Paragraph → Run → TextContent. Paragraphs can be added directly to WordDocument via AddParagraph without an explicit DocumentSection, as shown in the examples on this page. Styles are applied at the Run level, not on TextContent directly.
using IronWord;using IronWord.Models;using IronWord.Models.Enums;// Load docxWordDocument doc = new WordDocument("sample.docx");// Configure textRun textRun = new Run(new TextContent("Add text using IronWord"));// Configure text style settingstextRun.Style = new TextStyle(){FontSize = 24, // Text Size is 24TextFont = new Font() {FontFamily = "Calibri" // Text Font is "Calibri" },Color = Color.Red, // Set text color to redIsBold = true, // Make text boldIsItalic = true, // Make text italicUnderline = new Underline(), // Have an underlineStrike = StrikeValue.DoubleStrike, // Double strike-through};Paragraph paragraph = new Paragraph();// Add text to paragraphparagraph.AddChild(textRun);// Add paragraph to documentdoc.AddParagraph(paragraph);// Save documentdoc.SaveAs("add-text-style.docx");
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, // Double 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");
C#
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.
using IronWord;using IronWord.Models;// Create documentWordDocument doc = new WordDocument();// Add colored textRun 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 documentdoc.SaveAs("colored-text.docx");
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");
ImportsIronWordImportsIronWord.Models' Create documentDim doc As New WordDocument()' Add colored textDim 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 documentdoc.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.
using IronWord;using IronWord.Models;// Create documentWordDocument doc = new WordDocument();// Add text with custom font family and sizeRun textRun = new Run(new TextContent("This text uses Arial at 24pt!"));textRun.Style = new TextStyle(){FontSize = 24, // Set font size in pointsTextFont = new IronWord.Models.Font() {FontFamily = "Arial" // Set font family }};Paragraph paragraph = new Paragraph();paragraph.AddChild(textRun);doc.AddParagraph(paragraph);// Save documentdoc.SaveAs("font-styled-text.docx");
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");
ImportsIronWordImportsIronWord.Models' Create documentDim doc As New WordDocument()' Add text with custom font family and sizeDim 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 documentdoc.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.
using IronWord;using IronWord.Models;// Create documentWordDocument doc = new WordDocument();// Add bold textRun 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 documentdoc.SaveAs("bold-text.docx");
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");
ImportsIronWordImportsIronWord.Models' Create documentDim doc As New WordDocument()' Add bold textDim 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 documentdoc.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.
using IronWord;using IronWord.Models;// Create documentWordDocument doc = new WordDocument();// Add italic textRun 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 documentdoc.SaveAs("italic-text.docx");
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");
ImportsIronWordImportsIronWord.Models' Create documentDim doc As New WordDocument()' Add italic textDim 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 documentdoc.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 a 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, where Value is a line-height multiplier.
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.
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 documentWordDocument doc = new WordDocument();// Create richly formatted header text using RunRun 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 widthShading = new Shading() {Color = Color.LightGray// Light background }};// Add header to document using AddChild for styled RunParagraph headerParagraph = new Paragraph();headerParagraph.AddChild(headerRun);doc.AddParagraph(headerParagraph);// Create body text with different stylingRun 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};Paragraph bodyParagraph = new Paragraph();bodyParagraph.AddChild(bodyRun);doc.AddParagraph(bodyParagraph);// Save the documentdoc.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
};
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);
// Save the document
doc.SaveAs("professional-document.docx");
C#
This comprehensive styling approach creates documents maintaining consistent branding and professional appearance throughout your application's document generation process.
Frequently Asked Questions
How can I style text in a DOCX file using C#?
You can style text in a DOCX file using the IronWord library by creating a Run object containing TextContent and applying a TextStyle to the Run. TextStyle properties such as FontSize, Color, and IsBold allow you to customize the text appearance programmatically.
What is the TextStyle class used for in IronWord?
The TextStyle class in IronWord is used to apply various text formatting options, such as fonts, colors, bold, italic, and underline, to text within Word documents.
How do I change the font color of text using IronWord?
To change the font color of text using IronWord, set the Color property in the TextStyle class to a predefined color from IronWord.Models.Color or use custom hex values for specific color needs.
Can I set both font family and size in IronWord?
Yes, IronWord allows you to set both the font family and size for text. You can use the TextFont property to specify the FontFamily and the FontSize property in TextStyle to set the size in points.
What is the purpose of the Run object in IronWord?
The Run object in IronWord serves as a wrapper for TextContent, enabling the application of text styles such as fonts, colors, and decorations at a granular level within paragraphs.
How do I make text bold in an IronWord document?
To make text bold in an IronWord document, set the IsBold property of the TextStyle class to true, and apply it to the Run object containing your text.
What property is used to italicize text in IronWord?
The IsItalic property is used within the TextStyle class to apply italic formatting to text in IronWord documents.
How do I apply an underline to text using IronWord?
To underline text in an IronWord document, create a new Underline object and assign it to the Underline property in the TextStyle class for the Run object containing your text.
Can I apply complex text styling using multiple properties in IronWord?
Yes, IronWord enables complex text styling by combining multiple TextStyle properties, such as FontSize, Color, IsBold, and Underline, to create richly formatted text in Word documents.
What is the benefit of using IronWord for text styling in DOCX documents?
IronWord offers programmatic control over text styling in DOCX documents, allowing developers to automate document creation while ensuring consistent and professional formatting similar to Microsoft Word.
Ahmad is a full-stack developer with a strong foundation in C#, Python, and web technologies. He has a deep interest in building scalable software solutions and enjoys exploring how design and functionality meet in real-world applications.