How to Change Word Font Color in WPF

2016-02-19 03:30:22 Written by  support iceblue
Rate this item
(0 votes)

By default, the font color of a word document is black. To achieve a more distinctive visual effect, we can change it to red, green or any other colors we like.

This article describes how to change word font color with Spire.Doc for WPF in C#, VB.NET.

At first, please download Spire.Doc and install it correctly, then add Spire.Doc. Wpf.dll and Spire.License.dll from the installation folder as reference.

Below is the screenshot of the original word document:

How to Change Word Font Color in WPF

Detail steps:

Use namespace:

using System.Drawing;
using System.Windows;
using Spire.Doc;
using Spire.Doc.Documents;

Step 1: Initialize a new instance of Document class and load the sample document from file.

Document document = new Document();
document.LoadFromFile("Story.docx");

Step 2: Get its first section and first paragraph (the Title), then set text color for paragraph 1.

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);

Step 3: Get the second paragraph and 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);

Step 4: Save and launch the file.

document.SaveToFile("FontColor.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("FontColor.docx");

Output:

How to Change Word Font Color in WPF

Full codes:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile("Story.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("FontColor.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("FontColor.docx");
        }



    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports System.Windows

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			'Load Document
			Dim document As New Document()
			document.LoadFromFile("Story.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("FontColor.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("FontColor.docx")
		End Sub



	End Class
End Namespace

Additional Info

  • tutorial_title: Change Word Font Color in WPF
Last modified on Friday, 24 September 2021 09:50