How to Add Style to Text in DOCX with C#
IronWord 的 TextStyle 类使 .NET 开发人员可以通过编程方式对 Word 文档应用专业文本格式,包括字体、颜色、粗体、斜体、下划线等。 无论是生成报告、创建模板还是自动创建文档,IronWord 都能提供全面的样式工具,复制 Microsoft Word 的格式选项。
-
1Install IronWord with NuGet Package Manager
-
2复制并运行这段代码。
// 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# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronWord
最小工作流程(5 个步骤)
- 安装 IronWord C# 库
- 创建一个包含
TextContentRun对象 - 将
TextStyle应用于Run,并设置FontSize、IsBold和颜色等属性。 - 使用
AddChild将样式化的Run添加到Paragraph - 使用应用的样式保存文档
如何在 DOCX 中添加文本样式?
在 IronWord 中应用文本样式需要使用 Run 包装模式。 创建一个 WordDocument 对象,然后创建一个包含 TextContent 的 Run 对象,包含您的文本。 对 Run(而不是 TextContent)应用 TextStyle,使用属性例如 Color 或 FontSize。
样式化后,使用 AddChild 将 Run 添加到 Paragraph,将段落插入文档,并保存结果。 这种方法可对文本格式进行编程控制,适用于需要一致样式的自动文档生成场景。
Document → DocumentSection → Paragraph → Run → TextContent。)] 样式应用在 Run 级别,而不是直接在 TextContent 上。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");
这将产生什么输出?

TextStyle 类提供基本格式选项,包括字体属性、文本颜色、粗体、斜体和下划线。注意,FontSize 配置在 TextStyle 级别(而不是在 Font 内),而 FontFamily 在 TextFont 属性中设置。 Run 对象包裹了 TextContent 并持有样式,遵循 IronWord 的文档层次模式。 示例展示了多种样式属性如何结合以创建丰富格式化的文本,当应用于 Run 时。
我可以添加哪些特定样式?
如何更改文本颜色?
Color 属性在 TextStyle 中设置文本颜色,使用 IronWord.Models.Color 的预定义颜色或自定义十六进制值。 这样可以强调特定内容或与品牌色彩相匹配。 IronWord 支持多种颜色,包括红色、蓝色、绿色、橄榄色、深蓝色和栗色。
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")彩色文本看起来像什么?

如何设置字体家族和大小?
using TextFont 属性自定义文本外观。 将 FontFamily 设置为任何已安装的字体名称(例如,"Arial"、"Times New Roman")和以点为单位的 FontSize。 这样可以建立视觉层次,确保在不同设备和平台上的可读性。
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")自定义字体样式看起来像什么?

如何使文本加粗?
将 IsBold 属性设置为 true 以将文本 Bold。 Bold 文本通常用于标题、强调或突出重要信息。 与其他样式属性结合,Bold 文本创造视觉层次并提高可读性。
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")粗体文本看起来像什么?

如何使文本为斜体?
将 IsItalic 属性设置为 true 以进行 Italic 样式化。 Italic 文本通常用于强调、标题、外来词或技术术语。 这种细微的格式化区别于其他文本元素而不会有 Bold 格式的视觉重量。
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")斜体文本看起来像什么?

可用的样式属性有哪些?
IronWord 提供了全面的样式属性,与 Microsoft Word 的格式选项如出一辙。 这些特性结合在一起,形成了符合专业文档标准的复杂文本格式。
| 风格设计方法 | 说明 | 范例 |
|---|---|---|
文本字体 | 使用Font对象自定义文本外观,设置字体系列。注意: FontSize是在TextStyle层级设置的,而不是在Font内部设置的。 | textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } }; |
颜色 | 使用来自IronWord.Models.Color的预定义颜色或自定义十六进制值设置文本颜色。 | textRun.Style.Color = IronWord.Models.Color.Red; |
IsBold | 将文本设为Bold时,设置为true,通常用于标题或强调。 | textRun.Style.IsBold = true; |
IsItalic | 当设置为true时,对文本应用Italic样式,通常用于强调或标题。 | textRun.Style.IsItalic = true; |
下划线 | 使用下划线object和各种下划线样式为文本添加下划线。 | textRun.Style.Underline = new Underline(); |
Strike | 通过使用 StrikeValue 枚举(Strike或 DoubleStrike)来应用删除线到文本。 | textRun.Style.Strike = StrikeValue.Strike; |
大写 | 对文本应用大写效果,将所有字符转换为大写显示。 | textRun.Style.Caps = true; |
字符量表 | 按字符正常大小的百分比调整字符的宽度比例。 | textRun.Style.CharacterScale = 150; |
Emboss | 将浮雕效果应用于文本,创造出凸起的外观。 | textRun.Style.Emboss = true; |
重点 | 使用 EmphasisMarkValues 枚举值为样式文本添加强调标记。 | textRun.Style.Emphasis = EmphasisMarkValues.Dot; |
行距 | 使用 行距对象控制文本行间距,以提高可读性。 | textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; |
大纲 | 以轮廓效果渲染文本,仅显示字符边界。 | textRun.Style.Outline = true; |
着色 | 使用 着色对象为文本应用背景颜色或阴影。 | textRun.Style.Shading = new Shading() { Color = Color.Yellow }; |
小帽子 | 将小写字母转换为小写字母,同时保持大小写的区别。 | textRun.Style.SmallCaps = true; |
垂直位置 | 调整文本相对于基线的垂直位置,以点为单位。 | textRun.Style.VerticalPosition = 5.0; |
垂直文本对齐 | 使用 VerticalPositionValues枚举在其容器中垂直定位文本。 | textRun.Style.VerticalTextAlignment = VerticalPositionValues.Superscript; |
组合多种样式
IronWord 的文本样式功能来自于结合多个属性来实现复杂的格式化。 下面是一个通过结合各种样式属性演示专业样式文本的示例:
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# 编程将文本格式应用于 Word 文档?
通过 IronWord 的 TextStyle 类,您可以应用专业的文本格式,包括字体、颜色、粗体、斜体和下划线。只需创建一个包含文本的 TextContent 对象,应用具有所需属性的 TextStyle,将其添加到段落中,然后保存文档即可。
在 DOCX 文件中为文本添加样式的基本步骤是什么?
using IronWord 创建文本样式的方法如下:1)通过 NuGet 安装 IronWord;2)创建 WordDocument 对象;3)创建包含文本的 TextContent;4)应用 TextStyle 属性,如字体、颜色或粗体;5)将文本添加到段落中并保存。
有哪些文本格式选项?
IronWord 的 TextStyle 类提供了基本的格式选项,包括字体属性(FontFamily 和 FontSize)、文本颜色、粗体、斜体、下划线和删除线。这些选项可以组合使用,以创建格式丰富的文本。
如何更改文本的字体系列和大小?
using TextStyle 中的 TextFont 属性指定字体家族和大小。将 FontFamily 设置为 "Arial "或 "Times New Roman "等字体,并将 FontSize 设置为所需的点大小,如较大的文本设置为 16。
我可以同时应用多种文本样式吗?
是的,IronWord 允许您在单个 TextStyle 对象中组合多个样式属性。您可以同时应用粗体、斜体、颜色和字体变化来创建复杂的文本格式。
如何使用 C# 更改 Word 文档中的文本颜色?
通过 IronWord TextStyle 中的 Color 属性,您可以使用 IronWord.Models.Color 中的预定义颜色或自定义十六进制值来设置文本颜色。该功能有助于在文档中强调特定内容或匹配品牌颜色。
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.
