利用C#實(shí)現(xiàn)在Word中更改字體顏色
在日常工作中,我們有時(shí)會(huì)需要修改字體的顏色來(lái)突出文本重點(diǎn),讓讀者更容易抓住文章要點(diǎn)。在今天這篇文章中,我將為大家介紹如何以編程方式,在Word更改字體顏色。本文將分為兩部分分別介紹如何實(shí)現(xiàn)此操作。以下是我整理的步驟及方法,并附上C#/VB.NET代碼供大家參考。
更改段落字體顏色
更改特定文本字體顏色
程序環(huán)境
本次測(cè)試時(shí),在程序中引入Free Spire.Doc for .NET??赏ㄟ^(guò)以下方法引用 Free Spire.Doc.dll文件:
方法1:將 Free Spire.Doc for .NET下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的 Spire.Doc.dll。然后在Visual Studio中打開(kāi)“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2:通過(guò)NuGet安裝??赏ㄟ^(guò)以下2種方法安裝:
(1)可以在Visual Studio中打開(kāi)“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點(diǎn)擊“安裝”。等待程序安裝完成。
(2)將以下內(nèi)容復(fù)制到PM控制臺(tái)安裝。
Install-Package FreeSpire.Doc -Version 10.8.0
更改段落字體顏色
以下是更改 Word 文檔中段落字體顏色的步驟:
- 創(chuàng)建一個(gè)Document實(shí)例。
- 使用 Document.LoadFromFile() 方法加載 Word 文檔。
- 使用 Document.Sections[sectionIndex] 屬性獲取所需的節(jié)。
- 使用 Section.Paragraphs[paragraphIndex] 屬性獲取要更改字體顏色的所需段落。
- 創(chuàng)建一個(gè) ParagraphStyle 實(shí)例。
- 使用 ParagraphStyle.Name 和 ParagraphStyle.CharacterFormat.TextColor 屬性設(shè)置樣式名稱和字體顏色。
- 使用 Document.Styles.Add() 方法將樣式添加到文檔中。
- 使用 Paragraph.ApplyStyle() 方法將樣式應(yīng)用于段落。
- 使用 Document.SaveToFile() 方法保存結(jié)果文檔。
完整代碼
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForParagraph
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè)Document實(shí)例
Document document = new Document();
//Load a Word document
document.LoadFromFile("生死疲勞.docx");
//獲取第一節(jié)
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);
//保存結(jié)果文檔
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())
'創(chuàng)建一個(gè)Document實(shí)例
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("生死疲勞.docx")
'獲取第一節(jié)
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)
'保存結(jié)果文檔
document.SaveToFile("更改段落字體顏色.docx", FileFormat.Docx)
End Sub
End Class
End Namespace效果圖

更改特定文本字體顏色
以下是更改 Word 文檔中特定文本字體顏色的步驟:
- 創(chuàng)建一個(gè)Document實(shí)例。
- 使用 Document.LoadFromFile() 方法加載 Word 文檔。
- 使用 Document.FindAllString() 方法查找指定文本。
- 調(diào)用TextSelection.GetAsOneRange().CharacterFormat.TextColor 屬性,循環(huán)遍歷所有指定文本,并更改其字體顏色
- 使用 Document.SaveToFile() 方法保存結(jié)果文檔。
完整代碼
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForText
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè)Document實(shí)例
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;
}
//保存結(jié)果文檔
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())
'創(chuàng)建一個(gè)Document實(shí)例
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
'保存結(jié)果文檔
document.SaveToFile("更改特定文本字體顏色.docx", FileFormat.Docx)
End Sub
End Class
End Namespace效果圖

到此這篇關(guān)于利用C#實(shí)現(xiàn)在Word中更改字體顏色的文章就介紹到這了,更多相關(guān)C#更改Word字體顏色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C# CefSharp Python采集某網(wǎng)站簡(jiǎn)歷并且自動(dòng)發(fā)送邀請(qǐng)短信的方法
這篇文章主要給大家介紹了關(guān)于如何使用C# CefSharp Python采集某網(wǎng)站簡(jiǎn)歷并且自動(dòng)發(fā)送邀請(qǐng)短信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-03-03
C#中DropDownList動(dòng)態(tài)生成的方法
這篇文章主要介紹了C#中DropDownList動(dòng)態(tài)生成的方法,實(shí)例分析了C#中DropDownList的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖
這篇文章主要為大家詳細(xì)介紹了C#如何使用selenium組件實(shí)現(xiàn)操作瀏覽器并且截圖,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考一下2024-01-01
C#函數(shù)式編程中的標(biāo)準(zhǔn)高階函數(shù)詳解
這篇文章主要介紹了C#函數(shù)式編程中的標(biāo)準(zhǔn)高階函數(shù)詳解,本文講解了何為高階函數(shù)、Map、 Filter、Fold等內(nèi)容,需要的朋友可以參考下2015-01-01
C#使用Dictionary<string, string>拆分字符串與記錄log方法
這篇文章介紹了Dictionary<string, string>拆分字符串與記錄log的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#實(shí)現(xiàn)鐘表程序設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)鐘表程序設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

