News Category

C#/VB.NET: Change Font Color in Word

2022-03-25 07:48:00 Written by  Administrator
Rate this item
(0 votes)

If you want to emphasize a specific paragraph or text in your Word document, you can change its font color. This article will demonstrate how to change font color in Word in C# and VB.NET using Spire.Doc for .NET library.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Change Font Color of a Paragraph in C# and VB.NET

The following are the steps to change the font color of a paragraph in a Word document:

  • Create a Document instance.
  • Load the Word document using Document.LoadFromFile() method.
  • Get the desired section using Document.Sections[sectionIndex] property.
  • Get the desired paragraph that you want to change the font color of using Section.Paragraphs[paragraphIndex] property.
  • Create a ParagraphStyle instance.
  • Set the style name and font color using ParagraphStyle.Name and ParagraphStyle.CharacterFormat.TextColor properties.
  • Add the style to the document using Document.Styles.Add() method.
  • Apply the style to the paragraph using Paragraph.ApplyStyle() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
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);
        }
    }
}

C#/VB.NET: Change Font Color in Word

Change Font Color of a Specific Text in C# and VB.NET

The following are the steps to change the font color of a specific text in a Word document:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Find the text that you want to change font color of using Document.FindAllString() method.
  • Loop through all occurrences of the searched text and change the font color for each occurrence using TextSelection.GetAsOneRange().CharacterFormat.TextColor Property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
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);
        }
    }
}

C#/VB.NET: Change Font Color in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 01 June 2023 01:26