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

Ahmad Sohail
Ahmad Sohail
更新: 2026年7月21日

IronWord 的 TextStyle 类使 .NET 开发人员能够以编程方式为 Word 文档设置字体、文本颜色、粗体、斜体和下划线等格式。无论是生成报告、创建模板还是自动生成文档,IronWord 都提供与 Microsoft Word 格式选项对应的样式工具。

快速入门:使用 C# 为 DOCX 中的文本设置样式
  1. 1使用 IronWord 安装 NuGet Package Manager

    PM > Install-Package IronWord

  2. 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");
  3. 3部署到您的生产环境中进行测试

    今天就用免费试用在您的项目中开始使用IronWord
    arrow pointer

如何为 DOCX 文本设置样式?

在 IronWord 中,需要通过 Run 包装文本内容以设置样式。先创建 WordDocument 对象,再创建 Run 对象,其中包含存放实际文本的 TextContent。将 TextStyle 应用于 Run(而非 TextContent),并按需设置 IsBold、Color 或 FontSize 等属性。

设置样式后,使用 AddChild 将 Run 添加到 Paragraph,再将段落插入文档并保存。这样可以通过代码控制文本格式,适用于需要统一样式的文档自动生成场景。

请注意: IronWord的文档层次结构遵循以下结构:Document → (可选DocumentSection)→ Paragraph → Run → TextContent。 可以通过AddParagraph直接将段落添加到WordDocument中,无需显式的DocumentSection,如本页示例所示。 样式应用在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");

生成的文档是什么样的?

Microsoft Word 界面,显示布局功能区和应用了红色删除线文本格式的文档

TextStyle 类提供字体属性、文本颜色、加粗、斜体和下划线等基本格式选项。FontSize 在 TextStyle 层级设置(而非在 Font 内设置),FontFamily 则在 TextFont 属性中设置。按照 IronWord 的文档层次结构,Run 对象封装 TextContent 并保存样式。此示例演示如何为 Run 组合设置多个样式属性,以生成带有多种格式的文本。


我能添加哪些特定样式?

如何更改文本颜色?

通过 TextStyle 的 Color 属性设置文本颜色,可使用 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");

彩色文本是什么样的?

Microsoft Word 显示橄榄色文本格式,主页选项卡功能区显示字体和段落工具

如何设置字体族和大小?

使用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");

自定义字体样式是什么样的?

Microsoft Word 显示在工具栏中选择了 24pt 的 Arial 字体,并显示了格式化的示例文本

如何使文本加粗?

将 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");

加粗文本是什么样的?

Microsoft Word 界面显示粗体文本格式,文档中显示 '这是粗体!'文本

如何使文本变斜体?

将 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");

斜体文本是什么样的?

显示斜体文本的 Word 文档,在功能区界面中可以看到主页选项卡格式选项

可用的样式属性有哪些?

IronWord 提供与 Microsoft Word 格式选项对应的多种样式属性。可组合使用这些属性,实现符合专业文档标准的复杂文本格式。

样式设置方法说明范例
TextFont使用 Font 对象设置字体族,从而自定义文本外观。请在 TextStyle 层级设置 FontSize,而非在 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 时,对文本应用加粗(Bold)样式,通常用于标题或强调文字。textRun.Style.IsBold = true;
IsItalic当设置为true时,对文本应用Italic样式,通常用于强调或标题。textRun.Style.IsItalic = true;
Underline使用 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 对象控制文本行距,提高可读性。其中 Value 表示行高倍数。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
};

Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddChild(bodyRun);
doc.AddParagraph(bodyParagraph);

// Save the document
doc.SaveAs("professional-document.docx");

通过组合这些样式,应用程序生成的文档可以保持统一的品牌风格和专业外观。

常见问题

如何使用C#在DOCX文件中设置文本样式?

您可以通过创建包含TextContent的Run对象并将TextStyle应用于该Run,在DOCX文件中使用IronWord库设置文本样式。TextStyle属性如FontSize、Color和IsBold允许您以编程方式自定义文本外观。

TextStyle类在IronWord中用于什么?

IronWord中的TextStyle类用于对Word文档中的文本应用各种格式选项,如字体、颜色、粗体、斜体和下划线。

如何使用IronWord更改文本的字体颜色?

要使用IronWord更改文本的字体颜色,请在TextStyle类中将Color属性设置为IronWord.Models.Color中的预定义颜色或使用自定义十六进制值来满足特定的颜色需求。

我可以在IronWord中同时设置字体家族和大小吗?

可以,IronWord允许您同时设置文本的字体家族和大小。您可以使用TextFont属性指定FontFamily,并在TextStyle中使用FontSize属性设置大小(以磅为单位)。

Run对象在IronWord中的用途是什么?

IronWord中的Run对象作为TextContent的包装器,在段落内的细节级别上应用文本样式,例如字体、颜色和装饰。

如何在IronWord文档中将文本加粗?

要在IronWord文档中将文本加粗,请将TextStyle类的IsBold属性设置为true,并将其应用于包含您文本的Run对象。

在IronWord中,用于将文本斜体化的属性是什么?

IsItalic属性用于TextStyle类中以将斜体格式应用于IronWord文档中的文本。

如何使用IronWord为文本添加下划线?

要在IronWord文档中为文本添加下划线,请创建一个新的Underline对象并将其赋值给TextStyle类中Run对象的Underline属性。

我可以在IronWord中应用多种属性进行复杂文本样式设置吗?

可以,IronWord通过结合多种TextStyle属性(如FontSize、Color、IsBold和Underline)实现复杂的文本样式设置,以在Word文档中创建丰富格式的文本。

使用IronWord在DOCX文档中进行文本样式化的好处是什么?

IronWord提供了对DOCX文档中文本样式的编程控制,允许开发人员在自动创建文档的同时确保与Microsoft Word类似的一致和专业的格式。

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

阅读更多

准备开始了吗?

NuGet 下载量 63,050版本:2026.11刚刚发布