C++: Add Background Color or Picture to Word Documents

A background color or picture can help make a document more aesthetically pleasing and attention-grabbing. If you are creating a document for marketing, education, or presentation purposes, adding an attractive background color or picture would be very useful. In this article, we will demonstrate how to programmatically add background color or picture to Word documents 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

Add a Background Color to Word in C++

Adding a background color to a Word document is very straightforward using Spire.Doc for C++. You just need to set the document’s background type as color and then specify a color as the background. The detailed steps are as follows.

  • Initialize an instance of the Document class.
  • Load a Word document using Document->LoadFromFile() method.
  • Get the document's background using Document->GetBackground() method.
  • Set the background type as color using Background->SetType(BackgroundType::Color) method.
  • Set the background color using Background->SetColor() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

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

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Sample.docx");

	//Get the document's background
	intrusive_ptr <Background> background = document->GetBackground();
	//Set the background type as color
	background->SetType(BackgroundType::Color);
	//Set the background color
	background->SetColor(Color::GetAliceBlue());

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

C++: Add Background Color or Picture to Word Documents

Add a Gradient Background to Word in C++

To add a gradient background, you need to set the background type as gradient, specify the gradient color and then set the gradient shading variant and style. The detailed steps are as follows.

  • Initialize an instance of the Document class.
  • Load a Word document using Document->LoadFromFile() method.
  • Get the document's background using Document->GetBackground() method.
  • Set the background type as gradient using Background->SetType(BackgroundType::Gradient) method.
  • Specify two gradient colors using Background->GetGradient()->SetColor1() and Background->GetGradient()->SetColor2() methods.
  • Set gradient shading variant and style using Background->GetGradient()->SetShadingVariant() and Background->GetGradient()->SetShadingStyle() methods.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

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

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr <Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Sample.docx");

	//Get the document's background
	intrusive_ptr <Background> background = document->GetBackground();
	//Set the background type as gradient
	background->SetType(BackgroundType::Gradient);

	//Specify two gradient colors 
	background->GetGradient()->SetColor1(Color::GetWhite());
	background->GetGradient()->SetColor2(Color::GetLightBlue());

	//Set gradient shading variant and style
	background->GetGradient()->SetShadingVariant(GradientShadingVariant::ShadingDown);
	background->GetGradient()->SetShadingStyle(GradientShadingStyle::Horizontal);

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

C++: Add Background Color or Picture to Word Documents

Add a Background Picture to Word in C++

To add a background image to a Word document, you need to set the background type as picture, and then insert a picture as the background. The detailed steps are as follows.

  • Initialize an instance of the Document class.
  • Load a Word document using Document->LoadFromFile() method.
  • Get the document's background using Document->GetBackground() method.
  • Set the background type as picture using Background->SetType(BackgroundType::Picture) method.
  • Set the background picture using Background->SetPicture() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr <Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Sample.docx");

	//Get the document's background
	intrusive_ptr <Background> background = document->GetBackground();
	//Set the background type as picture
	background->SetType(BackgroundType::Picture);

	//Set the background picture
	background->SetPicture(L"background.png");

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

C++: Add Background Color or Picture to Word Documents

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.