Knowledgebase (2311)
Children categories
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++.
- Add a Background Color to Word in C++
- Add a Gradient Background to Word in C++
- Add a Background Picture to Word in 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;
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();
}

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

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

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.
In MS Word, a Page Break is an important feature of page layout that helps you start a new page wherever you want. After inserting page breaks, all formatting of the previous page applies to the new page, which makes the whole document neat and well organized. In this article, you will learn how to programmatically add or remove page breaks in a Word document using Spire.Doc for C++.
- Insert a Page Break after a Specific Paragraph in Word in C++
- Insert a Page Break after a Specific Text in Word in C++
- Remove Page Breaks in a Word Document in 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
Insert a Page Break after a Specific Paragraph in Word in C++
Spire.Doc for C++ offers the Paragraph->AppendBreak(BreakType::PageBreak) method to insert a page break after a paragraph. Once inserted, a symbol indicating the page break will be shown. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document->LoadFromFile() method.
- Get a specified section using Document->GetSections()->GetItem(sectionIndex) method.
- Get a specified paragraph using Section->GetParagraphs()->GetItem(paragraphIndex) method.
- Add a page break to end of the paragraph using Paragraph->AppendBreak(BreakType::PageBreak) method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring input_path = L"Data\\";
std::wstring inputFile = input_path + L"Input.docx";
//Specify output file path and name
std::wstring output_path = L"Output\\";
std::wstring outputFile = output_path + L"InsertPageBreak.docx";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a Word document from disk
document->LoadFromFile(inputFile.c_str());
//Get the first section
intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(0);
//Get the 2nd paragraph in the section
intrusive_ptr <Paragraph> paragraph = section->GetParagraphs()->GetItemInParagraphCollection(1);
//Insert a page break after the paragraph
paragraph->AppendBreak(BreakType::PageBreak);
//Save the result document
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
document->Close();
}

Insert a Page Break after a Specific Text in Word in C++
In addition to inserting a page break after a paragraph, Spire.Doc for C++ also allows you to find a specified text and then insert the page break after the specified text. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document->LoadFromFile() method.
- Find a specified text using Document->FindString() method.
- Get the text range of the specified text using TextSelection->GetAsOneRange() method.
- Get the paragraph where the text range is located using TextRange->GetOwnerParagraph() method.
- Get the position index of the text range in the paragraph using Paragraph->GetChildObjects()->IndexOf() method.
- Initialize an instance of Break class to create a page break.
- Insert the page break after the specified text using Paragraph->GetChildObjects()->Insert() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring input_path = L"Data\\";
std::wstring inputFile = input_path + L"Input.docx";
//Specify output file path and name
std::wstring output_path = L"Output\\";
std::wstring outputFile = output_path + L"InsertPageBreakAfterText.docx";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a Word document from disk
document->LoadFromFile(inputFile.c_str());
//Find a specified text
intrusive_ptr<TextSelection> selection = document->FindString(L"infrastructure", true, true);
//Get the text range of the specified text
intrusive_ptr<TextRange> range = selection->GetAsOneRange();
//Get the paragraph where the text range is located
intrusive_ptr<Paragraph> paragraph = range->GetOwnerParagraph();
//Get the position index of the text range in the paragraph
int index = paragraph->GetChildObjects()->IndexOf(range);
//Create a page break
intrusive_ptr<Break> pageBreak = new Break(document, BreakType::PageBreak);
//Insert a page break after the specified text
paragraph->GetChildObjects()->Insert(index + 1, pageBreak);
//Save to result document
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
document->Close();
}

Remove Page Breaks in a Word Document in C++
Some mistakenly added page breaks can mess up the structure of your entire document, so it's quite necessary to remove them. The following are the steps to remove page breaks in a Word document.
- Create a Document instance.
- Load a Word document using Document->LoadFromFile() method.
- Traverse through each paragraph in the first section, and then traverse through each child object of a paragraph.
- Determine whether the child object type is a page break. If yes, remove the page break from the paragraph using Paragraph->GetChildObjects()->Remove() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main() {
//Specify input file path and name
std::wstring input_path = L"Data\\";
std::wstring inputFile = input_path + L"InsertPageBreak.docx";
//Specify output file path and name
std::wstring output_path = L"Output\\";
std::wstring outputFile = output_path + L"RemovePageBreaks.docx";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a Word document from disk
document->LoadFromFile(inputFile.c_str());
//Traverse through each paragraph in the first section of the document
for (int j = 0; j < document->GetSections()->GetItemInSectionCollection(0)->GetParagraphs()->GetCount(); j++)
{
intrusive_ptr<Paragraph> p = document->GetSections()->GetItemInSectionCollection(0)->GetParagraphs()->GetItemInParagraphCollection(j);
//Traverse through each child object of a paragraph
for (int i = 0; i < p->GetChildObjects()->GetCount(); i++)
{
intrusive_ptr <DocumentObject> obj = p->GetChildObjects()->GetItem(i);
//Determine whether the child object type is a page break
if (Object::CheckType<Break>(obj) && (Object::Dynamic_cast<Break>(obj))->GetBreakType() == BreakType::PageBreak)
{
//Remove the page break from the paragraph
p->GetChildObjects()->Remove(obj);
}
}
}
//Save the result document
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
document->Close();
}

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.
PDF is one of the most versatile, universal and feature-rich file formats in use today. Compared to Word, PDF documents are less likely to lose formatting when they’re opened on various devices. Additionally, PDF has absolute advantages over Word in terms of document security, archiving and transmission. These are some of the reasons why we should convert Word to PDF. In this article, you will learn how to convert Word to PDF and how to set conversion options in C++ using Spire.Doc for C++.
- Convert Doc or Docx to PDF
- Convert Word to PDF with Bookmarks
- Convert Word to PDF with Fonts Embedded
- Set Image Quality When Converting Word to PDF
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
Convert Doc or Docx to PDF in C++
The Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method provided by Spire.Doc for C++ allows to save Word as PDF, XPS, HTML, RTF, etc. If you just want to save your Word documents as regular PDFs without additional settings, follow the steps below.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Save the document to PDF using Doucment->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
document->Close();
}

Convert Word to PDF with Bookmarks in C++
Bookmarks can enhance the readability of a document. When generating PDF from Word, you may would like to preserve existing bookmarks of the Word document or create bookmarks from the headings. The following are the steps to convert Word to PDF with bookmarks.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Create a ToPdfParameterList object, which is used to set conversion options.
- Create bookmarks in PDF from the headings in Word using ToPdfParameterList.SetCreateWordBookmarksUsingHeadings() method.
- Save the document to PDF with bookmarks using Doucment->SaveToFile(LPCWSTR_S fileName, ToPdfParameterList* paramList) method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Create a ToPdfParameterList object
intrusive_ptr<ToPdfParameterList> parameters = new ToPdfParameterList();
//Create bookmarks from Word headings
parameters->SetCreateWordBookmarksUsingHeadings(true);
//Create bookmarks in PDF from existing bookmarks in Word
//parameters->SetCreateWordBookmarks(true);
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), parameters);
document->Close();
}

Convert Word to PDF with Fonts Embedded in C++
By embedding fonts used in a Word document into the PDF document, you ensure that the PDF document looks the same on any device that does not have the appropriate fonts installed. The steps to embed fonts in PDF during conversion are as follows.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Create a ToPdfParameterList object, which is used to set conversion options.
- Embed fonts in generated PDF using ToPdfParameterList.SetIsEmbeddedAllFonts() method.
- Save the document to PDF using Doucment->SaveToFile(LPCWSTR_S fileName, ToPdfParameterList* paramList) method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Create a ToPdfParameterList object
intrusive_ptr<ToPdfParameterList> parameters = new ToPdfParameterList();
//Embed fonts used in Word in generated PDF
parameters->SetIsEmbeddedAllFonts(true);
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), parameters);
document->Close();
}

Set Image Quality When Converting Word to PDF in C++
A document containing a large number of high-quality images will often be large in size. When you convert Word to PDF, you can decide whether to compress the image quality or not. The following are the detailed steps.
- Create a Document object.
- Load a sample Word file using Document->LoadFromFile() method.
- Set the image quality using Document->SetJPEGQuality() mehtod
- Save the document to PDF using Doucment->SaveToFile() method.
- C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.docx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Document object
intrusive_ptr<Document> document = new Document();
//Load a Word file
document->LoadFromFile(inputFilePath.c_str());
//Compress image to 40% of the original quality
document->SetJPEGQuality(40);
//Preserve original image quality
//document->SetJPEGQuality(100);
//Save the document to PDF
document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
document->Close();
}
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.