How to Change Word Font Color in C#, VB.NET
Word Font Color is one kind of font formats to change text color in documents. By default, the color is black. However, the font color will be changed in order to make the special or important contents (for example, notices in instruction document) more obviously so that readers will pay more attention on the contents.
Spire.Doc for .NET, the professional .NET Word component, enables users to set font color by using C#, VB.NET. And this guide presents the method how to easily change font color in a Word document via Spire.Doc for .NET. The following screenshot is the result after changing.
- Load the document which you want to change font color at first.
- Secondly, get paragraph 1(the title) in section 1.
- Thirdly, declare a new ParagraphStyle. Give it a name. Set CharacterFormat property of ParagraphStyle. Then, apply this ParagraphStyle for paragraph 1.
- Fourthly, set another ParagraphStyle for paragraph 2 as step 3.
Code as following:
using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; namespace SetTextColor { class TextColor { static void Main(string[] args) { //Load Document Document document = new Document(); document.LoadFromFile(@"E:\Work\Documents\WordDocuments\A GOOD MAN IS HARD TO FIND.docx"); //Set Text Color for Paragraph 1(the Title) Section section = document.Sections[0]; Paragraph p1 = section.Paragraphs[0]; ParagraphStyle s1 = new ParagraphStyle(document); s1.Name = "TitleTextColor"; s1.CharacterFormat.TextColor = Color.RosyBrown; document.Styles.Add(s1); p1.ApplyStyle(s1.Name); //Set Text Color for Paragraph 2 Paragraph p2 = section.Paragraphs[1]; ParagraphStyle s2 = new ParagraphStyle(document); s2.Name = "BodyTextColor"; s2.CharacterFormat.TextColor = Color.DarkBlue; document.Styles.Add(s2); p2.ApplyStyle(s2.Name); //Save and Launch document.SaveToFile("TextColor.docx", FileFormat.Docx); System.Diagnostics.Process.Start("TextColor.docx"); } } }
Imports System.Drawing Imports Spire.Doc Imports Spire.Doc.Documents Namespace SetTextColor Friend Class TextColor Shared Sub Main(ByVal args() As String) 'Load Document Dim document As New Document() document.LoadFromFile("E:\Work\Documents\WordDocuments\A GOOD MAN IS HARD TO FIND.docx") 'Set Text Color for Paragraph 1(the Title) Dim section As Section = document.Sections(0) Dim p1 As Paragraph = section.Paragraphs(0) Dim s1 As New ParagraphStyle(document) s1.Name = "TitleTextColor" s1.CharacterFormat.TextColor = Color.RosyBrown document.Styles.Add(s1) p1.ApplyStyle(s1.Name) 'Set Text Color for Paragraph 2 Dim p2 As Paragraph = section.Paragraphs(1) Dim s2 As New ParagraphStyle(document) s2.Name = "BodyTextColor" s2.CharacterFormat.TextColor = Color.DarkBlue document.Styles.Add(s2) p2.ApplyStyle(s2.Name) 'Save and Launch document.SaveToFile("TextColor.docx", FileFormat.Docx) System.Diagnostics.Process.Start("TextColor.docx") End Sub End Class End Namespace
Spire.Doc, a stand-alone Word component, enables developers/programmers to fast generate, load, write, modify and save Word documents on their .NET, WPF, Silverlight applications.