How to Add Style to Text in DOCX

This article was translated from English: Does it need improvement?
Translated
View the article in English

Customizing text styling in Word documents is essential for creating professional and visually appealing content. IronWord offers industry-grade text styling options similar to what's available in your favorite DOCX editor, allowing you to apply formatting programmatically with ease.

In this how-to, we'll explore how to stylize text content with custom settings using IronWord's TextStyle class.

Get started with IronWord

Commencez à utiliser IronWord dans votre projet aujourd'hui avec un essai gratuit.

Première étape :
green arrow pointer


Adding Text Style Example

Applying text styles in IronWord is straightforward. Create a WordDocument object, then create a TextContent object with your text. Apply a TextStyle to it using properties such as IsBold, Color, or TextFont, and enhance the styling further with options like underline or strikethrough.

Once the text is fully styled, add it to a Paragraph, insert the paragraph into the document, and save the final result.

:path=/static-assets/word/content-code-examples/how-to/add-style-text-simple.cs
using IronWord;

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

// Configure text
TextContent text = new TextContent();
text.Text = "Add text using IronWord";

// Configure text style settings
text.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Calibri", // Text Font is "Calibri"
        FontSize = 24, // Text Size is 24
    },
    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.AddText(text);

// Add paragraph to document
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("add-text-style.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Add style to text in DOCX

The TextStyle class provides essential formatting options, including font properties, text color, bold, italic, and underline, giving you the flexibility to create professional-looking documents quickly.


Adding Specific Styles

Text Color

The Color property in TextStyle allows you to set text color using predefined colors from IronWord.Models.Color or custom hex values. This is useful for emphasizing specific content or matching brand colors in your documents.

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-text.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add colored text
TextContent text = new TextContent("This text is olive-colored!");
text.Style = new TextStyle()
{
    Color = IronWord.Models.Color.Olive // defining text to be colored olive
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("colored-text.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Add text color in DOCX

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 allows you to match document styles or establish a clear visual hierarchy.

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-font.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add text with custom font family and size
TextContent text = new TextContent("This text uses Arial at 24pt!");
text.Style = new TextStyle()
{
    TextFont = new IronWord.Models.Font()
    {
        FontFamily = "Arial",  // Set font family
        FontSize = 24          // Set font size in points
    }
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("font-styled-text.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Add font style to text in DOCX

Bold

Use the IsBold property set to true to make text bold. Bold text is commonly used for headings, emphasis, or highlighting important information in your documents.

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-bold.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add bold text
TextContent text = new TextContent("this is bold!");
text.Style = new TextStyle()
{
    IsBold = true  // Make text bold
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("bold-text.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Add bold text in DOCX

Italic

The IsItalic property set to true applies italic styling to text. Italic text is typically used for emphasis, titles, foreign words, or technical terms.

:path=/static-assets/word/content-code-examples/how-to/add-style-text-add-italic.cs
using IronWord;

// Create document
WordDocument doc = new WordDocument();

// Add italic text
TextContent text = new TextContent("this is italic.");
text.Style = new TextStyle()
{
    IsItalic = true  // Make text italic
};

Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
doc.AddParagraph(paragraph);

// Save document
doc.SaveAs("italic-text.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Add italic text in DOCX

Styling Properties

Expore more styling options in the below table.

Styling Method Description Example
TextFont Customizes text appearance with a Font object, setting font family and size in points. text.Style.TextFont = new Font() { FontFamily = "Calibri", FontSize = 24 };
Color Sets text color using predefined colors from IronWord.Models.Color or custom hex values. text.Style.Color = IronWord.Models.Color.Red;
IsBold Makes text bold when set to true, commonly used for headings or emphasis. text.Style.IsBold = true;
IsItalic Applies italic styling to text when set to true, typically used for emphasis or titles. text.Style.IsItalic = true;
Underline Adds an underline to text using an Underline object with various underline styles. text.Style.Underline = new Underline();
Strike Applies strikethrough to text using StrikeValue enum (Strike or DoubleStrike). text.Style.Strike = StrikeValue.Strike;
Caps Applies capitalization effects to text, converting all characters to uppercase display. text.Style.Caps = true;
CharacterScale Adjusts the proportional width of characters as a percentage of their normal size. text.Style.CharacterScale = 150;
Emboss Applies an embossed effect to text, creating a raised appearance. text.Style.Emboss = true;
Emphasis Adds emphasis marks to styled text using EmphasisMarkValues enum values. text.Style.Emphasis = EmphasisMarkValues.Dot;
LineSpacing Controls spacing between lines of text for improved readability using a LineSpacing object. text.Style.LineSpacing = new LineSpacing() { Value = 1.5 };
Outline Renders text with an outline effect, displaying only the character borders. text.Style.Outline = true;
Shading Applies background color or shading to text using a Shading object. text.Style.Shading = new Shading() { Color = Color.Yellow };
SmallCaps Converts lowercase letters to small capital letters while maintaining case distinction. text.Style.SmallCaps = true;
VerticalPosition Adjusts vertical placement of text relative to its baseline, measured in points. text.Style.VerticalPosition = 5.0;
VerticalTextAlignment Positions text vertically within its container using VerticalPositionValues enum. text.Style.VerticalTextAlignment = VerticalPositionValues.Superscript;

Questions Fréquemment Posées

Qu'est-ce qu'IronWord ?

IronWord est une bibliothèque .NET qui permet aux développeurs de créer, modifier et manipuler des documents Word par programmation. Elle offre des fonctionnalités complètes pour la mise en forme et le style du texte.

Comment puis-je modifier la couleur du texte dans un fichier DOCX avec IronWord ?

IronWord propose des méthodes pour modifier les styles de texte, notamment sa couleur. Vous pouvez spécifier le code couleur souhaité à appliquer au texte sélectionné dans votre document Word.

IronWord peut-il mettre du texte en gras dans un document Word ?

Oui, IronWord prend en charge les options de mise en forme de texte standard, y compris la mise en gras. Vous pouvez appliquer le style gras à des éléments de texte spécifiques dans vos fichiers DOCX.

Est-il possible d'appliquer plusieurs styles à un texte avec IronWord ?

Absolument, IronWord vous permet d'appliquer de nombreuses options de style à votre texte. Vous pouvez combiner différents styles comme le gras, l'italique et le souligné pour personnaliser entièrement votre document Word.

Quels types de styles de texte peuvent être appliqués avec IronWord ?

IronWord prend en charge un large éventail d'options de style de texte, notamment la taille de la police, la couleur de la police, le gras, l'italique, le soulignement, etc., vous permettant de personnaliser l'apparence de votre document DOCX.

IronWord prend-il en charge les styles de police personnalisés ?

Oui, IronWord vous permet d'utiliser des styles de police personnalisés dans vos documents Word. Vous pouvez spécifier les polices installées sur votre système pour améliorer l'apparence du document.

Est-il possible d'automatiser la création de documents Word mis en forme à l'aide d'IronWord ?

IronWord est conçu pour simplifier l'automatisation de la création et de la mise en forme des documents Word. Vous pouvez appliquer et modifier par programmation les styles de texte afin de générer des documents efficacement.

Comment IronWord gère-t-il l'alignement du texte dans les fichiers DOCX ?

IronWord offre des fonctionnalités permettant d'ajuster l'alignement du texte dans vos documents Word. Vous pouvez aligner le texte à gauche, à droite, au centre ou le justifier selon vos besoins de mise en page.

Quels sont les avantages de l'utilisation d'IronWord pour la mise en forme du texte DOCX ?

L'utilisation d'IronWord pour la mise en forme du texte DOCX offre des avantages tels que la facilité d'intégration dans les applications .NET, la flexibilité dans l'application de différents styles de texte et la possibilité d'automatiser les tâches de traitement des documents.

IronWord est-il compatible avec différentes versions de Microsoft Word?

IronWord est conçu pour fonctionner avec les fichiers DOCX, compatibles avec différentes versions de Microsoft Word. Il offre une prise en charge complète de la création et de la modification de ces fichiers, quelle que soit la version de Word utilisée.

Ahmad Sohail
Développeur Full Stack

Ahmad est un développeur full-stack avec une solide fondation en C#, Python et technologies web. Il a un profond intérêt pour la construction de solutions logicielles évolutives et aime explorer comment le design et la fonctionnalité se rencontrent dans des applications du monde réel.

<...
Lire la suite
Prêt à commencer?
Nuget Téléchargements 25,807 | Version : 2025.11 vient de sortir