C++: Merge Word Documents

Merging Word documents can be useful when you want to consolidate information from multiple files. For example, if you have a series of reports that relate to a specific project or topic, you may want to merge them into one document for easier access, distribution, and archiving purposes. In this article, we will demonstrate how to merge Word documents into one 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

Merge Word Documents by Inserting Files in C++

The Document->InsertTextFromFile(LPCWSTR_S fileName, FileFormat fileFormat) method provided by Spire.Doc for C++ enables developers to merge Word documents by inserting the contents of other Word documents into the original Word document. Using this method, the inserted contents will begin on a new page. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Insert another document into the loaded Word document using the Document->InsertTextFromFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • Save the result document to a specific location using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Specify the input file paths
	wstring inputFile_1 = L"Sample1.docx";
	wstring inputFile_2 = L"Sample2.docx";
	//Specify the output file path
	wstring outputFile = L"MergeWordDocumentsByInsertingFile.docx";

	//Initialize an instance of the Document class
	intrusive_ptr<Document> doc = new Document();
	//Load a Word document
	doc->LoadFromFile(inputFile_1.c_str());

	//Insert another document into the loaded document
	doc->InsertTextFromFile(inputFile_2.c_str(), FileFormat::Auto);

	//Save the result document to a specific location
	doc->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
	doc->Close();
}

C++: Merge Word Documents

Merge Word Documents by Cloning Contents in C++

If you want to merge documents without starting from a new page, you can clone the contents of other documents and add them to the end of the original document. This approach can avoid creating unwanted section breaks. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load the first Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Get the last section of the first Word document using the Document->GetLastSection() method.
  • Initialize an instance of the Document class.
  • Load the second Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Iterate through all sections of the second Word document, and then iterate through all child objects of each section.
  • Clone each child object using the DocumentObject->Clone() method.
  • Add the cloned child object to the end of the last section of the first Word document using the Section->GetBody()->GetChildObjects()->Add(intrusive_ptr<IDocumentObject> entity) method.
  • Save the first Word document to a specific location using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Specify the input file paths
	wstring inputFile_1 = L"Sample1.docx";
	wstring inputFile_2 = L"Sample2.docx";
	//Specify the output file path
	wstring outputFile =  L"MergeWordDocumentsByCloningContents.docx";

	//Initialize an instance of the Document class
	intrusive_ptr<Document> document1 = new Document();
	//Load the first Word document
	document1->LoadFromFile(inputFile_1.c_str(), FileFormat::Auto);
	//Get the last section of the first Word document
	intrusive_ptr<Section> lastSection = document1->GetLastSection();

	//Initialize an instance of the Document class
	intrusive_ptr<Document> document2 = new Document();
	//Load the second Word document
	document2->LoadFromFile(inputFile_2.c_str(), FileFormat::Auto);

	//Get the number of sections of the second Word document
	int sectionCount = document2->GetSections()->GetCount();
	
	//Iterate through the sections of the second Word document
	for (int i = 0; i < sectionCount; i++)
	{
		intrusive_ptr<Section> section = document2->GetSections()->GetItemInSectionCollection(i);
		int sectionChildObjectsCount = section->GetBody()->GetChildObjects()->GetCount();
		//Iterate through the child objects in each section of the second Word document
		for (int j = 0; j < sectionChildObjectsCount; j++)
		{
			intrusive_ptr<DocumentObject> documentObject = section->GetBody()->GetChildObjects()->GetItem(j);
			//Clone each child object
			intrusive_ptr<DocumentObject> clonedObject = documentObject->Clone();
			//Add each cloned child object to the end of the last section of the first Word document
			lastSection->GetBody()->GetChildObjects()->Add(clonedObject);
		}
	}

	//Save the first Word document to a specific location
	document1->SaveToFile(outputFile.c_str(), FileFormat::Docx);
	document1->Close();
	document2->Close();
}

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