如何使用 C# 为 DOCX 中的文本添加样式;

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

自定义 Word 文档中的文本样式是创建专业且具有视觉吸引力内容的必要条件。 IronWord 提供了全面的样式 API,可将丰富的文本格式应用于 DOCX 文件。

本指南演示了如何使用 IronWord 的 TextStyle 类自定义设置文本内容的样式。

开始使用 IronWord

今天在您的项目中使用 IronWord,免费试用。

第一步:
green arrow pointer


添加文本样式示例

在 IronWord 中应用文本样式非常简单。 实例化一个 WordDocument 对象和一个包含文本的 TextContent 对象。 为文本的 Style 属性分配一个新的 TextStyle 对象,并指定诸如 IsBold, ColorTextFont 等属性。 使用下划线或删除线等选项进一步增强样式。

文本样式完全确定后,将其添加到 Paragraph 中,将段落插入到文档中,然后保存最终结果。

: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

输出

在 DOCX 中为文本添加样式

TextStyle类提供了必要的格式设置选项:字体属性、文本颜色、粗体、斜体和下划线。这使得以编程方式创建简洁专业的文档成为可能。


添加特定样式

文本颜色

TextStyle 中的 Color 属性允许您使用 IronWord.Models.Color 中的预定义颜色或自定义十六进制值设置文本颜色,以强调特定内容或匹配品牌颜色。

: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

输出

在 DOCX 中添加文本颜色

字体系列和大小

通过 TextFont 属性自定义文本外观。 将 FontFamily 设置为任何已安装的字体名称(如 "Arial"),并将 FontSize 设置为点(如 12)。 这样既能执行文档标准,又能定义清晰的视觉层次。

: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

输出

在 DOCX 中为文本添加字体样式

大胆的

IsBold 属性设置为 true 以强调标题或突出关键数据。

: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

输出

在 DOCX 中添加粗体文本

斜体

IsItalic属性设置为true ,文本将应用斜体样式。 通常用于强调、标题、外来词或技术术语。

: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

输出

在 DOCX 中添加斜体文本

样式属性

在下表中探索更多样式选项。

风格设计方法 说明 范例
TextFont 使用Font对象自定义文本外观,设置字体系列和磅数大小。 text.Style.TextFont = new Font() { FontFamily = "Calibri", FontSize = 24 };
Color 使用IronWord.Models.Color中的预定义颜色或自定义十六进制值设置文本颜色。 text.Style.Color = IronWord.Models.Color.Red;
IsBold 设置为true时,文本将加粗,常用于标题或强调。 text.Style.IsBold = true;
IsItalic 设置为true时,文本将应用斜体样式,通常用于强调或标题。 text.Style.IsItalic = true;
Underline 使用具有各种下划线样式的Underline对象为文本添加下划线。 text.Style.Underline = new Underline();
Strike 使用StrikeValue枚举(Strike 或 DoubleStrike)为文本添加删除线。 text.Style.Strike = StrikeValue.Strike;
Caps 对文本应用大写效果,将所有字符转换为大写显示。 text.Style.Caps = true;
CharacterScale 按字符正常大小的百分比调整字符的宽度比例。 text.Style.CharacterScale = 150;
Emboss 将浮雕效果应用于文本,创造出凸起的外观。 text.Style.Emboss = true;
Emphasis 使用EmphasisMarkValues枚举值向样式文本添加强调标记。 text.Style.Emphasis = EmphasisMarkValues.Dot;
LineSpacing 使用LineSpacing对象控制文本行之间的间距,以提高可读性。 text.Style.LineSpacing = new LineSpacing() { Value = 1.5 };
Outline 以轮廓效果渲染文本,仅显示字符边界。 text.Style.Outline = true;
Shading 使用Shading对象为文本应用背景颜色或阴影。 text.Style.Shading = new Shading() { Color = Color.Yellow };
SmallCaps 将小写字母转换为小写字母,同时保持大小写的区别。 text.Style.SmallCaps = true;
VerticalPosition 调整文本相对于基线的垂直位置,以点为单位。 text.Style.VerticalPosition = 5.0;
VerticalTextAlignment 使用VerticalPositionValues枚举值在容器内垂直定位文本。 text.Style.VerticalTextAlignment = VerticalPositionValues.Superscript;

常见问题解答

什么是 IronWord?

IronWord是一个.NET库,允许开发人员以编程方式创建、编辑和操作Word文档。它提供了全面的文本样式和格式设置功能。

如何使用 IronWord 更改 DOCX 文件中的文本颜色?

IronWord 提供修改文本样式的方法,包括更改文本颜色。您可以指定要应用于 Word 文档中选定文本的颜色代码。

IronWord 能否将 Word 文档中的文本加粗?

是的,IronWord 支持标准文本格式设置选项,包括加粗文本。您可以将粗体样式应用于 DOCX 文件中的特定文本元素。

是否可以使用 IronWord 对文本应用多种样式?

当然,IronWord 允许您对文本应用多种样式选项。您可以组合使用粗体、斜体和下划线等不同样式,从而完全自定义您的 Word 文档。

IronWord 可以应用哪些类型的文本样式?

IronWord 支持多种文本样式选项,包括字体大小、字体颜色、粗体、斜体、下划线等,使您能够自定义 DOCX 文档的外观。

IronWord是否支持自定义字体样式?

是的,IronWord 允许您在 Word 文档中使用自定义字体样式。您可以指定系统上已安装的字体来增强文档的外观。

我能否使用 IronWord 自动创建带样式的 Word 文档?

IronWord旨在简化Word文档的创建和样式设置流程。您可以通过编程方式应用和修改文本样式,从而高效地生成文档。

IronWord如何处理DOCX文件中的文本对齐方式?

IronWord 提供调整 Word 文档中文本对齐方式的功能。您可以根据布局要求,将文本设置为左对齐、右对齐、居中对齐或两端对齐。

使用 IronWord 对 DOCX 文本进行样式设置有哪些好处?

使用 IronWord 对 DOCX 文本进行样式设置具有诸多优势,例如易于集成到 .NET 应用程序中、灵活应用各种文本样式以及能够自动执行文档处理任务。

IronWord是否与不同版本的Microsoft Word兼容?

IronWord 专为处理 DOCX 文件而设计,DOCX 文件与各种版本的 Microsoft Word 兼容。它为跨不同 Word 版本创建和编辑这些文件提供了强大的支持。

Ahmad Sohail
全栈开发者

Ahmad 是一名全栈开发人员,拥有扎实的 C#、Python 和 Web 技术基础。他对构建可扩展的软件解决方案深感兴趣,并喜欢探索设计和功能在实际应用中如何结合。

在加入 Iron Software 团队之前,Ahmad 致力于自动化项目和 API 集成,专注于提高性能和开发人员体验。

在业余时间,他喜欢尝试 UI/UX 想法,贡献开源工具,并偶尔从事技术写作和文档工作,以便让复杂主题更易于理解。

准备开始了吗?
Nuget 下载 27,129 | Version: 2025.11 刚刚发布