翻譯|使用教程|編輯:顏馨|2023-04-18 11:44:11.053|閱讀 160 次
概述: 本文介紹如何在 Word 中更改字體顏色,歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時(shí)適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
如果要強(qiáng)調(diào)Word文檔中的特定段落或文本,可以更改其字體顏色。本文將演示如何在 C# 中更改 Word 中的字體顏色,并使用 Spire.Doc for .NET 庫 VB.NET。
首先,您需要將 Spire.Doc for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目中的引用。DLL 文件可以通過 NuGet 安裝。
PM> Install-Package Spire.Doc
以下是更改 Word 文檔中段落的字體顏色的步驟:
創(chuàng)建文檔實(shí)例。
【C#】
using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace ChangeFontColorForParagraph { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.LoadFromFile("Sample.docx"); //Get the first section Section section = document.Sections[0]; //Change text color of the first Paragraph 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); //Change text color of the second Paragraph 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); //Save the result document document.SaveToFile("ChangeParagraphTextColor.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()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Change text color of the first Paragraph Dim p1 As Paragraph = section.Paragraphs(0) Dim s1 As ParagraphStyle = New ParagraphStyle(document) s1.Name = "Color1" s1.CharacterFormat.TextColor = Color.RosyBrown document.Styles.Add(s1) p1.ApplyStyle(s1.Name) 'Change text color of the second Paragraph Dim p2 As Paragraph = section.Paragraphs(1) Dim s2 As ParagraphStyle = New ParagraphStyle(document) s2.Name = "Color2" s2.CharacterFormat.TextColor = Color.DarkBlue document.Styles.Add(s2) p2.ApplyStyle(s2.Name) 'Save the result document document.SaveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx) End Sub End Class End Namespace
以下是更改 Word 文檔中特定文本的字體顏色的步驟:
創(chuàng)建文檔實(shí)例。
【C#】
using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace ChangeFontColorForText { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.LoadFromFile("Sample.docx"); //Find the text that you want to change font color for TextSelection[] text = document.FindAllString("Spire.Doc for .NET", false, true); //Change the font color for the searched text foreach (TextSelection seletion in text) { seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red; } //Save the result document document.SaveToFile("ChangeCertainTextColor.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()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Find the text that you want to change font color for Dim text As TextSelection() = document.FindAllString("Spire.Doc for .NET", False, True) 'Change the font color for the searched text For Each seletion As TextSelection In text seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red Next 'Save the result document document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx) End Sub End Class End Namespace
申請(qǐng)臨時(shí)許可證
如果您想從生成的文檔中刪除評(píng)估消息,或擺脫功能限制,請(qǐng)為自己申請(qǐng) 30 天試用許可證。
以上便是在 Word 中更改字體顏色的教程,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn