利用C#實現(xiàn)在Word中更改字體顏色
在日常工作中,我們有時會需要修改字體的顏色來突出文本重點,讓讀者更容易抓住文章要點。在今天這篇文章中,我將為大家介紹如何以編程方式,在Word更改字體顏色。本文將分為兩部分分別介紹如何實現(xiàn)此操作。以下是我整理的步驟及方法,并附上C#/VB.NET代碼供大家參考。
更改段落字體顏色
更改特定文本字體顏色
程序環(huán)境
本次測試時,在程序中引入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 文檔中段落字體顏色的步驟:
- 創(chuàng)建一個Document實例。
- 使用 Document.LoadFromFile() 方法加載 Word 文檔。
- 使用 Document.Sections[sectionIndex] 屬性獲取所需的節(jié)。
- 使用 Section.Paragraphs[paragraphIndex] 屬性獲取要更改字體顏色的所需段落。
- 創(chuàng)建一個 ParagraphStyle 實例。
- 使用 ParagraphStyle.Name 和 ParagraphStyle.CharacterFormat.TextColor 屬性設置樣式名稱和字體顏色。
- 使用 Document.Styles.Add() 方法將樣式添加到文檔中。
- 使用 Paragraph.ApplyStyle() 方法將樣式應用于段落。
- 使用 Document.SaveToFile() 方法保存結果文檔。
完整代碼
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForParagraph
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個Document實例
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);
//保存結果文檔
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)建一個Document實例
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)
'保存結果文檔
document.SaveToFile("更改段落字體顏色.docx", FileFormat.Docx)
End Sub
End Class
End Namespace效果圖

更改特定文本字體顏色
以下是更改 Word 文檔中特定文本字體顏色的步驟:
- 創(chuàng)建一個Document實例。
- 使用 Document.LoadFromFile() 方法加載 Word 文檔。
- 使用 Document.FindAllString() 方法查找指定文本。
- 調用TextSelection.GetAsOneRange().CharacterFormat.TextColor 屬性,循環(huán)遍歷所有指定文本,并更改其字體顏色
- 使用 Document.SaveToFile() 方法保存結果文檔。
完整代碼
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ChangeFontColorForText
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個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())
'創(chuàng)建一個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#實現(xiàn)在Word中更改字體顏色的文章就介紹到這了,更多相關C#更改Word字體顏色內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的方法
這篇文章主要給大家介紹了關于如何使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-03-03
C#使用Dictionary<string, string>拆分字符串與記錄log方法
這篇文章介紹了Dictionary<string, string>拆分字符串與記錄log的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

