How to Add Style to Text in DOCX with C
IronWord 的 TextStyle 类使 .NET 开发人员能够通过编程方式对 Word 文档应用 Professional 文本格式设置,包括字体、颜色、加粗、斜体、下划线等。 无论是生成报告、创建模板还是自动创建文档,IronWord 都能提供全面的样式工具,复制 Microsoft Word 的格式选项。
快速入门:使用 C# 为 DOCX 中的文本设置样式
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronWord
PM > Install-Package IronWord -
复制并运行这段代码。
// 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"); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronWord
最小工作流程(5 个步骤)
- 安装 IronWord C# 库
- 创建一个包含`TextContent` `Run`对象
- 将`TextStyle`应用于`Run` ,并设置`FontSize` 、 `IsBold`和`颜色`等属性。
- 使用`AddChild`将样式化的`Run`添加到`Paragraph`
- 使用应用的样式保存文档
如何在 DOCX 中添加文本样式?
在 IronWord 中应用文本样式需使用 Run 包装模式。 创建一个 WordDocument 对象,然后创建一个包含 TextContent 并填入您文本的 Run 对象。 使用 Color 或 FontSize 等属性,将 TextStyle 应用于 Run(而非 TextContent)。
格式调整完成后,使用 AddChild 将 Run 添加到 Paragraph 中,将该段落插入文档,并保存结果。 这种方法可对文本格式进行编程控制,适用于需要一致样式的自动文档生成场景。
: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")
这将产生什么输出?
TextStyle 类提供了基本的格式设置选项,包括字体属性、文本颜色、加粗、斜体和下划线。 请注意,FontSize 在 TextStyle 级别进行配置(而非在 Font 内部),而 FontFamily 则在 TextFont 属性中设置。 Run 对象封装了 TextContent 并承载样式,遵循 IronWord 的文档层次结构模式。 该示例演示了当应用于 Run 时,多个样式属性如何组合以生成格式丰富的文本。
我可以添加哪些特定样式?
如何更改文本颜色?
Color 属性通过 TextStyle 中预定义的颜色或自定义十六进制值来设置文本颜色。 这样可以强调特定内容或与品牌色彩相匹配。 IronWord 支持多种颜色,包括红色、蓝色、绿色、橄榄色、深蓝色和栗色。
: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")
彩色文本看起来像什么?
如何设置字体家族和大小?
使用 TextFont 属性自定义文本外观。 将 FontFamily 设置为任何已安装的字体名称(例如"Arial"、"Times New Roman"),并将 FontSize 设置为点数。 这样可以建立视觉层次,确保在不同设备和平台上的可读性。
: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")
自定义字体样式看起来像什么?
如何使文本加粗?
将 IsBold 属性设置为 true 可使文本加粗。 粗体文字通常用于标题、强调或突出重要信息。 结合其他样式属性,粗体文字可创建视觉层次并提高可读性。
: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")
粗体文本看起来像什么?
如何使文本为斜体?
将 IsItalic 属性设置为 true 以实现斜体样式。 斜体字通常用于强调、标题、外来词或技术术语。 这种微妙的格式化既能区分文本元素,又不会产生粗体格式化的视觉效果。
: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")
斜体文本看起来像什么?
可用的样式属性有哪些?
IronWord 提供了全面的样式属性,与 Microsoft Word 的格式选项如出一辙。 这些特性结合在一起,形成了符合专业文档标准的复杂文本格式。
| 风格设计方法 | 说明 | 范例 |
|---|---|---|
| `TextFont` | 使用**`Font`对象**自定义文本外观,设置字体系列。注意: `FontSize`是在`TextStyle`层级设置的,而不是在`Font`内部设置的。 | `textRun.Style = new TextStyle() { FontSize = 24, TextFont = new Font() { FontFamily = "Calibri" } };` |
| `Color` | 使用 **`IronWord.Models.Color`** 中的预定义颜色或自定义十六进制值设置文本颜色。 | `textRun.Style.Color = IronWord.Models.Color.Red;` |
| `IsBold` | 设置为`true`时,文本将加粗,常用于标题或强调。 | `textRun.Style.IsBold = true;` |
| `IsItalic` | 设置为`true`时,文本将应用斜体样式,通常用于强调或标题。 | `textRun.Style.IsItalic = true;` |
| `Underline` | 使用具有各种下划线样式的 **`Underline`对象**为文本添加下划线。 | `textRun.Style.Underline = new Underline();` |
| `Strike` | 使用 **`StrikeValue`枚举**(Strike 或 DoubleStrike)对文本应用删除线。 | `textRun.Style.Strike = StrikeValue.Strike;` |
| `Caps` | 对文本应用大写效果,将所有字符转换为大写显示。 | `textRun.Style.Caps = true;` |
| `CharacterScale` | 按字符正常大小的百分比调整字符的宽度比例。 | `textRun.Style.CharacterScale = 150;` |
| `Emboss` | 将浮雕效果应用于文本,创造出凸起的外观。 | `textRun.Style.Emboss = true;` |
| `Emphasis` | 使用 **`EmphasisMarkValues` 枚举**值为样式文本添加强调标记。 | `textRun.Style.Emphasis = EmphasisMarkValues.Dot;` |
| `LineSpacing` | 使用 **`LineSpacing`对象**控制文本行间距,以提高可读性。 | `textRun.Style.LineSpacing = new LineSpacing() { Value = 1.5 };` |
| `Outline` | 以轮廓效果渲染文本,仅显示字符边界。 | `textRun.Style.Outline = true;` |
| `Shading` | 使用 **`Shading`对象**为文本应用背景颜色或阴影。 | `textRun.Style.Shading = new Shading() { Color = Color.Yellow };` |
| `SmallCaps` | 将小写字母转换为小写字母,同时保持大小写的区别。 | `textRun.Style.SmallCaps = true;` |
| `VerticalPosition` | 调整文本相对于基线的垂直位置,以点为单位。 | `textRun.Style.VerticalPosition = 5.0;` |
| `VerticalTextAlignment` | 使用 **`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,
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")
这种全面的样式设计方法创建的文档在整个应用程序的文档生成过程中都能保持一致的品牌和专业外观。
常见问题解答
如何用 C# 编程将文本格式应用于 Word 文档?
通过 IronWord 的 TextStyle 类,您可以应用专业的文本格式,包括字体、颜色、粗体、斜体和下划线。只需创建一个包含文本的 TextContent 对象,应用具有所需属性的 TextStyle,将其添加到段落中,然后保存文档即可。
在 DOCX 文件中为文本添加样式的基本步骤是什么?
使用 IronWord 创建文本样式的方法如下:1)通过 NuGet 安装 IronWord;2)创建 WordDocument 对象;3)创建包含文本的 TextContent;4)应用 TextStyle 属性,如字体、颜色或粗体;5)将文本添加到段落中并保存。
有哪些文本格式选项?
IronWord 的 TextStyle 类提供了基本的格式选项,包括字体属性(FontFamily 和 FontSize)、文本颜色、粗体、斜体、下划线和删除线。这些选项可以组合使用,以创建格式丰富的文本。
如何更改文本的字体系列和大小?
使用 TextStyle 中的 TextFont 属性指定字体家族和大小。将 FontFamily 设置为 "Arial "或 "Times New Roman "等字体,并将 FontSize 设置为所需的点大小,如较大的文本设置为 16。
我可以同时应用多种文本样式吗?
是的,IronWord 允许您在单个 TextStyle 对象中组合多个样式属性。您可以同时应用粗体、斜体、颜色和字体变化来创建复杂的文本格式。
如何使用 C# 更改 Word 文档中的文本颜色?
通过 IronWord TextStyle 中的 Color 属性,您可以使用 IronWord.Models.Color 中的预定义颜色或自定义十六进制值来设置文本颜色。该功能有助于在文档中强调特定内容或匹配品牌颜色。

