翻譯|使用教程|編輯:顏馨|2023-04-18 11:44:11.053|閱讀 160 次
概述: 本文介紹如何在 Word 中更改字體顏色,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
如果要強調Word文檔中的特定段落或文本,可以更改其字體顏色。本文將演示如何在 C# 中更改 Word 中的字體顏色,并使用 Spire.Doc for .NET 庫 VB.NET。
首先,您需要將 Spire.Doc for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。DLL 文件可以通過 NuGet 安裝。
PM> Install-Package Spire.Doc
以下是更改 Word 文檔中段落的字體顏色的步驟:
創建文檔實例。
【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 文檔中特定文本的字體顏色的步驟:
創建文檔實例。
【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
申請臨時許可證
如果您想從生成的文檔中刪除評估消息,或擺脫功能限制,請為自己申請 30 天試用許可證。
以上便是在 Word 中更改字體顏色的教程,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn