IRONSOFTWAREHOME
Add Style Text
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
	FontSize = 72,
	TextFont = new Font()
	{
		FontFamily = "Caveat"
	},
	Color = IronWord.Models.Color.Red,
	IsBold = true,
	IsItalic = true,
	Underline = new Underline(),
	Strike = StrikeValue.Strike,
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddChild(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Load docx
Dim doc As New WordDocument("document.docx")

' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
	.FontSize = 72,
	.TextFont = New Font() With {
		.FontFamily = "Caveat"
	},
	.Color = IronWord.Models.Color.Red,
	.IsBold = True,
	.IsItalic = True,
	.Underline = New Underline(),
	.Strike = StrikeValue.Strike
}

Dim paragraph As New Paragraph()

' Add text
paragraph.AddChild(textRun)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
NuGet Download
PM > Install-Package IronWord
Add Style Text

Add Style Text

The "Add Style Text" feature in IronWord allows developers to apply various text styling options while adding content to a DOCX document. It provides fine-grained control over the appearance of text, such as specifying the font family, size, color, and style attributes like bold, italic, underline, and strike-through. By creating a Run object containing TextContent and assigning a TextStyle to the Run, developers can tailor the presentation of individual text elements within a document, ensuring a customized look and feel.

This feature is useful for generating professionally formatted documents dynamically, such as reports or letters, where specific sections require distinct styles. The TextStyle class enables easy manipulation of these attributes, allowing for both simple and complex styling within the same document.

Key Points

  1. Creating a Styled Run:
    • A Run object is created containing TextContent with the desired text.
    • The Style property of the Run is assigned a TextStyle object to apply formatting. FontSize
  2. Configuring TextStyle:
    • TextStyle: Set at the TextStyle level (not inside Font) to specify text size.
    • TextFont: Contains font properties including fontFamily for font selection. FontFamily
    • Color: Specifies the text color using color. IronWord.Models.Color
    • bold and italic: Boolean properties for bold and italic formatting. IsBold
    • underline: Adds underline styling to the text. Underline
    • strike-through: Applies strikethrough formatting using StrikeValue enum. Strike
  3. Adding to Document:
    • Use AddChild to add the styled Run to a Paragraph.
    • The paragraph is then added to the document with AddParagraph.

Code Explanation

This code shows how to create and style text in a DOCX document using IronWord. It begins by loading an existing WordDocument object from "document.docx". A Run object is created containing TextContent with the string "Styled text example" and a TextStyle is applied to the Run to configure the appearance of the text.

The TextStyle includes settings for font size set at the TextStyle level (not inside Font), font family configured via fontFamily, TextFont text color, and bold formatting. These settings customize how the text will appear in the final document.

After the Run is styled, the AddChild method adds the Run object to a paragraph in the document. This method inserts the styled content into the Word document in the appropriate format. Finally, the SaveAs method is called to export the document as "save_document.docx". The result is a Word document where the inserted text is formatted according to the specified styles, preserving all font and formatting properties in the output file.

Explore Document Element Tutorials for More Styling Tips
View on GitHub

Ready to Get Started?

Nuget Downloads 56,401Version:2026.9just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required