添加样式文本 IronWord 中的"添加样式文本"功能允许开发人员在向 DOCX 文档添加内容时应用各种文本样式选项。 它可以对文本外观进行精细控制,例如指定字体系列、大小、颜色以及粗体、斜体、下划线和删除线等样式属性。 通过创建一个包含 Run 的 TextContent 对象,并将 TextStyle 分配给 Run,开发人员可以定制文档中各个文本元素的呈现方式,从而确保自定义的外观和感觉。 此功能可用于动态生成专业格式的文档,例如报告或信函,其中特定部分需要不同的样式。 TextStyle 类可以轻松操作这些属性,从而在同一文档中实现简单和复杂的样式。 关键点 1.创建样式化跑步: A Run object is created containing TextContent with the desired text. The Style property of the Run is assigned a TextStyle object to apply formatting. 2.配置文本样式: FontSize: Set at the TextStyle level (not inside Font) to specify text size. TextFont: Contains font properties including FontFamily for font selection. Color: Specifies the text color using IronWord.Models.Color. IsBold and IsItalic: Boolean properties for bold and italic formatting. Underline: Adds underline styling to the text. Strike: Applies strikethrough formatting using StrikeValue enum. 3.添加到文档: Use AddChild to add the styled Run to a Paragraph. The paragraph is then added to the document with AddParagraph. 代码解释 此代码演示了如何使用 IronWord 在 DOCX 文档中创建和设置文本样式。 It begins by initializing a new WordDocument object, representing the document to be generated. A Run object is created containing TextContent with the string "Styled text example" and a TextStyle is applied to the Run to configure the appearance of the text. The TextStyle includes settings for font size set at the TextStyle level (not inside Font), font family configured via TextFont, text color, and bold formatting. 这些设置可以自定义文本在最终文档中的显示方式。 After the Run is styled, the AddChild method adds the Run object to a paragraph in the document. 此方法会将样式化的内容以适当的格式插入到 Word 文档中。 Finally, the SaveAs method is called to export the document as "styled_document.docx". 最终生成的 Word 文档中,插入的文本将按照指定的样式进行格式化,并在输出文件中保留所有字体和格式属性。 浏览文档元素教程,获取更多样式技巧 相关文档链接 在GitHub上查看 相关教程 相关操作指南 类文档 下载IronWord DLL 报告此页面的问题 准备开始了吗? Nuget 下载 35,581 | 版本: 2026.3 刚刚发布 免费试用 免费 NuGet 下载 总下载量:35,581 查看许可证 还在滚动吗? 想快速获得证据? PM > Install-Package IronWord 运行示例 观看您的数据变成 Word 文档。 免费 NuGet 下载 总下载量:35,581 查看许可证