C++: Change Font Color in Word

Changing the font color in a Word document can be an effective way to emphasize important points. For example, if you are creating a report that contains crucial data, changing the font color of the data text to a brighter color can make it stand out from other text and quickly grab your reader's attention. Another benefit of changing the font color is that it can enhance the visual appearance and readability of the document. For instance, when preparing marketing materials, changing the font color of headings and subheadings to a different font color than the rest of the text can help create a clear information hierarchy, making the materials more attractive and easier to read. In this article, we will demonstrate how to change the font color in a Word document in C++ using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Change the Font Color of a Paragraph in Word in C++

To change the font color of a specific paragraph in a Word document, you can create a custom paragraph style with a specific font color, then add the style to the document and apply it to the paragraph you want to modify. The detailed steps are as follows:

  • Create an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile() method.
  • Access a specific section in the document by its index using the Document->GetSections()->GetItem(int index) method.
  • Access the paragraph you want to modify by its index using the Section->GetParagraphs()->GetItem(int index) method.
  • Create an instance of the ParagraphStyle class to define a custom paragraph style.
  • Set the name and font color of the paragraph style using the ParagraphStyle->SetName() and ParagraphStyle->GetCharacterFormat()->SetTextColor() methods.
  • Add the custom paragraph style to the document using the Document->GetStyles()->Add() method.
  • Apply the custom paragraph style to the specific paragraph using the Paragraph->ApplyStyle() method.
  • Save the modified document using the Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace Spire::Common;

int main()
{
    //Create a Document instance
    Document* document = new Document();
    //Load a Word document
    document->LoadFromFile(L"Sample.docx");

    //Get the first section
    Section* section = document->GetSections()->GetItem(0);

    //Change the font color of the first Paragraph
    Paragraph* p1 = section->GetParagraphs()->GetItem(0);
    ParagraphStyle* style1 = new ParagraphStyle(document);
    style1->SetName(L"Color1");
    style1->GetCharacterFormat()->SetTextColor(Color::GetRosyBrown());
    document->GetStyles()->Add(style1);
    p1->ApplyStyle(style1);

    //Change the font color of the second Paragraph
    Paragraph* p2 = section->GetParagraphs()->GetItem(1);
    ParagraphStyle* style2 = new ParagraphStyle(document);
    style2->SetName(L"Color2");
    style2->GetCharacterFormat()->SetTextColor(Color::GetDarkGreen());
    document->GetStyles()->Add(style2);
    p2->ApplyStyle(style2);

    //Save the result document
    document->SaveToFile(L"ChangeFontColorForParagraph.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Change Font Color in Word

Change the Font Color of a Specific Text in Word in C++

To change the font color of a specific text in a Word document, you need to search for the text in the document, then change the font color of its all occurrences. The detailed steps are as follows:

  • Create an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile() method.
  • Find the text that you want to change the font color of using the Document->FindAllString() method.
  • Iterate through all occurrences of the text and change the font color of each occurrence using the TextSelection->GetAsOneRange()->GetCharacterFormat()->SetTextColor() method.
  • Save the result document using the Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace Spire::Common;
using namespace std;

int main()
{
	//Create a Document instance
	Document* document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Sample.docx");

	//Find the text that you want to change the font color of
	vector<TextSelection*> selection = document->FindAllString(L"Spire.Doc for C++", false, true);

	//Change the font color of all occurrences of the text
	for (auto text : selection)
	{
		text->GetAsOneRange()->GetCharacterFormat()->SetTextColor(Color::GetRed());
	}

	//Save the result document
	document->SaveToFile(L"ChangeFontColorForCertainText.docx", FileFormat::Docx2013);
    	document->Close();
	delete document;
}

C++: 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.