利用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中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2:通過(guò)NuGet安裝。可通過(guò)以下2種方法安裝:
(1)可以在Visual Studio中打開“解決方案資源管理器”,鼠標(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#使用log4net結(jié)合sqlite數(shù)據(jù)庫(kù)實(shí)現(xiàn)記錄日志
因?yàn)榻Y(jié)構(gòu)化的數(shù)據(jù)庫(kù)存儲(chǔ)的日志信息,可以寫專門的軟件讀取歷史日志信息,通過(guò)各種條件篩選,可操作性極大增強(qiáng),有這方面需求的開發(fā)人員可以考慮,本文給大家介紹了C#使用log4net結(jié)合sqlite數(shù)據(jù)庫(kù)記錄日志,需要的朋友可以參考下2024-10-10C#+RedisSearch實(shí)現(xiàn)高性能全文搜索
Redis?Search是一個(gè)Redis模塊,它使用壓縮的倒排索引來(lái)實(shí)現(xiàn)快速的索引和低內(nèi)存占用,本文主要介紹了C#如何使用RedisSearch實(shí)現(xiàn)高性能全文搜索,希望對(duì)大家有所幫助2023-07-07C#使用ScrapySharp快速?gòu)木W(wǎng)頁(yè)采集數(shù)據(jù)
這篇文章介紹了使用ScrapySharp快速?gòu)木W(wǎng)頁(yè)采集數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C# 表達(dá)式樹Expression Trees的知識(shí)梳理
本篇文章主要介紹了表達(dá)式樹 Expression Trees的基礎(chǔ)知識(shí):Lambda 表達(dá)式創(chuàng)建表達(dá)式樹;API 創(chuàng)建表達(dá)式樹;編譯表達(dá)式樹;執(zhí)行表達(dá)式樹;修改表達(dá)式樹等等,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問(wèn)
本文詳細(xì)講解了C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問(wèn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03Unity UGUI的ContentSizeFitter內(nèi)容尺寸適應(yīng)器組件使用示例
這篇文章主要為大家介紹了Unity UGUI的ContentSizeFitter內(nèi)容尺寸適應(yīng)器組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08