IronWord 操作指南 为文本添加样式 How to Add Style to Text in DOCX Ahmad Sohail 已更新:十一月 17, 2025 Download IronWord NuGet 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English Customizing text styling in Word documents is essential for creating professional and visually appealing content. IronWord offers industry-grade text styling options similar to what's available in your favorite DOCX editor, allowing you to apply formatting programmatically with ease. In this how-to, we'll explore how to stylize text content with custom settings using IronWord's TextStyle class. Get started with IronWord 今天在您的项目中使用 IronWord,免费试用。 第一步: 免费开始 How to Add Style to Text in DOCX Download and install the IronWord C# library Create a new Word document or load an existing file Specify the desired font family usingFontFamily Apply bold or italic formatting to highlight selected text content Save and export the updated document with the applied style changes Adding Text Style Example Applying text styles in IronWord is straightforward. Create a WordDocument object, then create a TextContent object with your text. Apply a TextStyle to it using properties such as IsBold, Color, or TextFont, and enhance the styling further with options like underline or strikethrough. Once the text is fully styled, add it to a Paragraph, insert the paragraph into the document, and save the final result. :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 Output The TextStyle class provides essential formatting options, including font properties, text color, bold, italic, and underline, giving you the flexibility to create professional-looking documents quickly. Adding Specific Styles Text Color The Color property in TextStyle allows you to set text color using predefined colors from IronWord.Models.Color or custom hex values. This is useful for emphasizing specific content or matching brand colors in your documents. :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 Output Font Family and Size Customize text appearance with the TextFont property. Set FontFamily to any installed font name (e.g., "Arial", "Times New Roman") and FontSize in points. This allows you to match document styles or establish a clear visual hierarchy. :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 Output Bold Use the IsBold property set to true to make text bold. Bold text is commonly used for headings, emphasis, or highlighting important information in your documents. :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 Output Italic The IsItalic property set to true applies italic styling to text. Italic text is typically used for emphasis, titles, foreign words, or technical terms. :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 Output Styling Properties Expore more styling options in the below table. Styling Method Description Example TextFont Customizes text appearance with a Font object, setting font family and size in points. text.Style.TextFont = new Font() { FontFamily = "Calibri", FontSize = 24 }; Color Sets text color using predefined colors from IronWord.Models.Color or custom hex values. text.Style.Color = IronWord.Models.Color.Red; IsBold Makes text bold when set to true, commonly used for headings or emphasis. text.Style.IsBold = true; IsItalic Applies italic styling to text when set to true, typically used for emphasis or titles. text.Style.IsItalic = true; Underline Adds an underline to text using an Underline object with various underline styles. text.Style.Underline = new Underline(); Strike Applies strikethrough to text using StrikeValue enum (Strike or DoubleStrike). text.Style.Strike = StrikeValue.Strike; Caps Applies capitalization effects to text, converting all characters to uppercase display. text.Style.Caps = true; CharacterScale Adjusts the proportional width of characters as a percentage of their normal size. text.Style.CharacterScale = 150; Emboss Applies an embossed effect to text, creating a raised appearance. text.Style.Emboss = true; Emphasis Adds emphasis marks to styled text using EmphasisMarkValues enum values. text.Style.Emphasis = EmphasisMarkValues.Dot; LineSpacing Controls spacing between lines of text for improved readability using a LineSpacing object. text.Style.LineSpacing = new LineSpacing() { Value = 1.5 }; Outline Renders text with an outline effect, displaying only the character borders. text.Style.Outline = true; Shading Applies background color or shading to text using a Shading object. text.Style.Shading = new Shading() { Color = Color.Yellow }; SmallCaps Converts lowercase letters to small capital letters while maintaining case distinction. text.Style.SmallCaps = true; VerticalPosition Adjusts vertical placement of text relative to its baseline, measured in points. text.Style.VerticalPosition = 5.0; VerticalTextAlignment Positions text vertically within its container using VerticalPositionValues enum. 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 下载 25,807 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:25,807 查看许可证