如果要强调 Word 文档中的特定段落或文本,可以更改其字体颜色。 本文将演示如何使用 Spire.Doc for .NET 在 C# 和 VB.NET 中更改 Word 中的字体颜色。
安装 Spire.Doc for .NET
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.Doc更改段落字体颜色
以下是更改 Word 文档中段落字体颜色的步骤:
- 创建一个 Document实例。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 使用 Document.Sections[sectionIndex] 属性获取所需的节。
- 使用 Section.Paragraphs[paragraphIndex] 属性获取要更改字体颜色的所需段落。
- 创建一个 ParagraphStyle 实例。
- 使用 ParagraphStyle.Name 和 ParagraphStyle.CharacterFormat.TextColor 属性设置样式名称和字体颜色。
- 使用 Document.Styles.Add() 方法将样式添加到文档中。
- 使用 Paragraph.ApplyStyle() 方法将样式应用于段落。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Document实例
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("示例.docx");
            //获取第一节
            Section section = document.Sections[0];
            //更改第一段文本颜色
            Paragraph p1 = section.Paragraphs[0];
            ParagraphStyle s1 = new ParagraphStyle(document);
            s1.Name = "Color1";
            s1.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(s1);
            p1.ApplyStyle(s1.Name);
            //更改第二段文本颜色
            Paragraph p2 = section.Paragraphs[1];
            ParagraphStyle s2 = new ParagraphStyle(document);
            s2.Name = "Color2";
            s2.CharacterFormat.TextColor = Color.MediumSlateBlue;
            document.Styles.Add(s2);
            p2.ApplyStyle(s2.Name);
            //保存结果文档
            document.SaveToFile("更改段落字体颜色.docx", FileFormat.Docx);
        }
    }
}Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace ChangeFontColorForParagraph
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '创建一个Document实例 Dim document As Document = New Document() 'Load a Word document
            document.LoadFromFile("示例.docx")
            '获取第一节 Dim section As Section = document.Sections(0) '更改第一段文本颜色
            Dim p1 As Paragraph = section.Paragraphs(0)
            Dim s1 As ParagraphStyle = New ParagraphStyle(document)
            s1.Name = "Color1"
            s1.CharacterFormat.TextColor = Color.RosyBrown
            document.Styles.Add(s1)
            p1.ApplyStyle(s1.Name)
            '更改第二段文本颜色 Dim p2 As Paragraph = section.Paragraphs(1) Dim s2 As ParagraphStyle = New ParagraphStyle(document) s2.Name = "Color2" s2.CharacterFormat.TextColor = Color.MediumSlateBlue document.Styles.Add(s2) p2.ApplyStyle(s2.Name) '保存结果文档
            document.SaveToFile("更改段落字体颜色.docx", FileFormat.Docx)
        End Sub
    End Class
End Namespace
更改特定文本字体颜色
以下是更改 Word 文档中特定文本字体颜色的步骤:
- 创建一个 Document 实例。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 使用 Document.FindAllString() 方法查找指定文本。
- 调用 TextSelection.GetAsOneRange().CharacterFormat.TextColor 属性,循环遍历所有指定文本,并更改其字体颜色
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForText
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Document实例
            Document document = new Document();
            //加载 Word 文档
            document.LoadFromFile("示例.docx");
            //查找指定文本
            TextSelection[] text = document.FindAllString("Spire.Doc for .NET", false, true);
            //更改特定文本的字体颜色
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Purple;
            }
            //保存结果文档
            document.SaveToFile("更改特定文本字体颜色.docx", FileFormat.Docx);
        }
    }
}Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace ChangeFontColorForText
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '创建一个Document实例 Dim document As Document = New Document() '加载 Word 文档
            document.LoadFromFile("示例.docx")
            '查找需要更改字体颜色的文本 Dim text As TextSelection() = document.FindAllString("Spire.Doc for .NET",False,True) '更改特定文本的字体颜色
            For Each seletion As TextSelection In text
                seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Purple
            Next
            '保存结果文档 document.SaveToFile("更改特定文本字体颜色.docx",FileFormat.Docx) End Sub End Class End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



