顾乔芝士网

持续更新的前后端开发技术栈

C#/VB.NET:在 Word 中更改字体颜色

如果您想在 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

在 C# 和 VB.NET 中更改段落的字体颜色

以下是在 Word 文档中更改段落字体颜色的步骤:

  1. 创建一个 Document 实例。
  2. 使用 Document.LoadFromFile() 方法加载 Word 文档。
  3. 通过 Document.Sections[sectionIndex] 属性获取目标节(Section)。
  4. 通过 Section.Paragraphs[paragraphIndex] 属性获取要更改字体颜色的段落。
  5. 创建一个 ParagraphStyle 实例。
  6. 使用 ParagraphStyle.Name 设置样式名称,并通过 ParagraphStyle.CharacterFormat.TextColor 设置字体颜色。
  7. 使用 Document.Styles.Add() 方法将样式添加到文档中。
  8. 使用 Paragraph.ApplyStyle() 方法将样式应用到段落。
  9. 使用 Document.SaveToFile() 方法保存结果文档。
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace ChangeFontColorForParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个 Document 实例
            Document document = new Document();
            //加载 Word 文档
            document.LoadFromFile("Sample.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.DarkBlue;
            document.Styles.Add(s2);
            p2.ApplyStyle(s2.Name);

            //保存修改后的文件
            document.SaveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx);
        }
    }
}

在 C# 和 VB.NET 中更改特定文本的字体颜色

以下是在 Word 文档中更改特定文本字体颜色的步骤:

  1. 创建一个 Document 实例。
  2. 使用 Document.LoadFromFile() 方法加载 Word 文档。
  3. 使用 Document.FindAllString() 方法查找要更改字体颜色的文本。
  4. 浏览所有查找到的文本,将每个匹配项的字体颜色通过 TextSelection.GetAsOneRange().CharacterFormat.TextColor 属性进行修改。
  5. 使用 Document.SaveToFile() 方法保存结果文档。
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace ChangeFontColorForText
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个 Document 实例
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //查找您想要更改字体颜色的文本
            TextSelection[] text = document.FindAllString("Spire.Doc for .NET", false, true);
            //为查找到的文本更改字体颜色
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red;
            }

            //保存修改后的文档
            document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);
        }
    }
}

申请临时授权

如果您希望去除生成文档中的评估信息,或解除功能限制,可以联系官方申请一个 30 天的试用授权。

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言