在日常工作中,我们有时会需要修改字体的颜色来突出文本重点,让读者更容易抓住文章要点。在今天这篇文章中,我将为大家介绍如何以编程方式,在Word更改字体颜色。本文将分为两部分分别介绍如何实现此操作。以下是我整理的步骤及方法,并附上C#/VB.NET代码供大家参考。

更改段落字体颜色

更改特定文本字体颜色

程序环境

本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用 Free Spire.Doc.dll文件:

方法1:将 Free Spire.Doc for .NET下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的 Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。

方法2:通过NuGet安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。

(2)将以下内容复制到PM控制台安装。

Install-Package FreeSpire.Doc -Version 10.8.0

更改段落字体颜色

以下是更改 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#

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.Blue;
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.Green;
document.Styles.Add(s2);
p2.ApplyStyle(s2.Name); //保存结果文档
document.SaveToFile("更改段落字体颜色.docx", FileFormat.Docx);
}
}
}

VB.NET

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.Blue
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.Green
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#

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("生死疲劳", false, true); //更改特定文本的字体颜色
foreach (TextSelection seletion in text)
{
seletion.GetAsOneRange().CharacterFormat.TextColor = Color.HotPink;
} //保存结果文档
document.SaveToFile("更改特定文本字体颜色.docx", FileFormat.Docx);
}
}
}

VB.NET

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("生死疲劳", False, True) '更改特定文本的字体颜色
For Each seletion As TextSelection In text
seletion.GetAsOneRange().CharacterFormat.TextColor = Color.HotPink
Next '保存结果文档
document.SaveToFile("更改特定文本字体颜色.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

效果图

—本文完—

如何通过C#/VB.NET代码在Word中更改字体颜色的更多相关文章

  1. Word中的字体大小

    Word中的字体大小(几号-几磅)   Word对字体大小采用两种不同的度量单位,其中一种是以"号"为度量单位,如常用的"初号.小初.一号.小一--七号.八号" ...

  2. android 在代码中设置字体颜色 问题

    项目中需要在代码中控制字体颜色 之前是直接引用资源文件  但是不行 tv.setTextColor(R.color.textColor_black); 无效果   后来在网上找了资料发现 要从reso ...

  3. word中更改图片和标题之间的垂直距离

    word中插入图片后.往往须要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距.你的图片和标题之间的距离也是2倍行 ...

  4. 导出word功能,用html代码在word中插入分页符

    <span lang=EN-US style="font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:" mce_st ...

  5. 怎样把代码复制到word中并保持颜色

  6. html中对应Word中的字体和字号

    原文链接:https://blog.csdn.net/xuexizhe88/article/details/79277587:https://blog.csdn.net/qq_31603575/art ...

  7. Word中的代码怎样语法高亮

    在平常我们粘贴代码到Word中的时候,经常会遇到代码粘贴到Word中后没有语法高亮,看着很乱很不友好,Word自带的样式---语法使用着也不尽人意, 网上有很多做法可以使得在插入在Word中的代码能够 ...

  8. (转)如何优雅的在 Microsoft word中插入代码

    背景:最近项目需要自己编写文档,在文档中需要插入部分代码,记录下这个方法. 一.工具 方法1.打开这个网页PlanetB; 方法2.或者谷歌搜索syntax highlight code in wor ...

  9. 如何在 Microsoft word中插入代码

    一.工具 方法1.打开这个网页PlanetB; 方法2.或者谷歌搜索syntax highlight code in word documents,检索结果的第一个.如下图: PS. 方法1和2打开的 ...

  10. Java 在Word中创建多级项目符号列表和编号列表

    本文分享通过Java程序代码在Word中创建多级项目符号列表和编号列表的方法.程序运行环境如下: IntelliJ IDEA 2018(JDK 1.8.0) Word 2013 Word Jar包:F ...

随机推荐

  1. docker和docker-compose便捷安装

    安装docker: curl -fsSL get.docker.com -o get-docker.sh&&sh get-docker.sh 或: curl -sSL https:// ...

  2. NavBarControl 通过代码添加

    看到网上很多都是直接在控件上面添加的 而我的是保存在数据库读取后添加的. 后来自己摸索了一下. 通过代码添加 navBarControl1.Groups.Add(new NavBarGroup( st ...

  3. 私藏!资深数据专家SQL效率优化技巧 ⛵

    作者:韩信子@ShowMeAI 数据分析实战系列:https://www.showmeai.tech/tutorials/40 本文地址:https://www.showmeai.tech/artic ...

  4. 【数据库】E-R图相关知识、手动自动绘制方法及工具推荐

    一.知识 1.介绍 E-R图也称实体-联系图(Entity Relationship Diagram),提供了表示实体.属性和联系的方法,用来描述现实世界的概念模型. 2.组成 (1)实体(Entit ...

  5. Linux常用软件的安装及Nginx的使用

    主要内容: 软件安装方式 上传与下载工具 常用软件的安装--jdk.tomcat.mysql.redis 项目的部署 Nginx的安装 Nginx的功能 静态网站部署 虚拟主机配置及端口绑定 域名绑定 ...

  6. 【每日一题】【暴力&双指针&动态规划】42. 接雨水-211130/220214

    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 方法1:暴力求解(超时) import java.util.*; public class Solu ...

  7. 最大值减去最小值小于或等于 num 的子数组数量问题

    最大值减去最小值小于或等于 num 的子数组数量问题 作者:Grey 原文地址: 博客园:最大值减去最小值小于或等于 num 的子数组数量问题 CSDN:最大值减去最小值小于或等于 num 的子数组数 ...

  8. Chaos 测试下的若干 NebulaGraph Raft 问题分析

    Raft 是一种广泛使用的分布式共识算法.NebulaGraph 底层采用 Raft 算法实现 metad 和 storaged 的分布式功能.Raft 算法使 NebulaGraph 中的 meta ...

  9. PhaApi NOTORM 实现分表分库

    通过自增ID取模要分表的数量,便可得到表名.例如log表分成100张表:log_1,log2...,log100. 每次数据库CURD都先通过获取ID分配到相对应的表,例如:id=66,取模后的结果是 ...

  10. selenium 之可视模式、静默模式、忽略证书不可用的设置

    1.可视模式的设置(在前台工作) from selenium import webdriver import time url = "https://y.qq.com/n/ryqq/song ...