MS Word is a powerful tool that allows you to customize and format your professional documents with a fantastic set of features. You may find yourself in situations where you want to merge multiple Word documents, such as combining all the receipts in a single file or combining the different software requirement documents for easier management. Furthermore, combining the files simplifies the process of sharing them. In this article, you will learn how to merge multiple MS Word documents using C++.
- C++ API to Merge Word Documents
- Combine Word Documents using C++
- Merge Word Documents with Additional Options
- Get a Free License
C++ API to Merge Word Documents
Aspose.Words for C++ is a native C++ library that allows you to create, change and convert Microsoft Word documents. In addition, it also supports combining multiple Word documents into a single file. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Words.Cpp
Merge Word Documents using C++
Merging Word documents with Aspose.Words for C++ API is a breeze. You can load and merge them with just a few lines of code. The following is the image showing the two sample Word files that we will combine in this article.
The following are the steps for merging Word documents using C++.
- Load the destination document using the Document class.
- Load the source document using the Document class.
- Use the Document->AppendDocument (System::SharedPtr<Aspose::Words::Document> srcDoc, Aspose::Words::ImportFormatMode importFormatMode) method of the destination document instance to merge both documents.
- Set the styling preference for importing the source document using the ImportFormatMode enum.
- Save the merged Word document using Document->Save (System::String fileName, Aspose::Words::SaveFormat saveFormat) method.
The following is the sample code to merge Word documents.
The following images compare the merged documents generated by using ImportFormatMode::KeepSourceFormatting and ImportFormatMode::UseDestinationStyles modes.
Merge Word Documents with Additional Options
Aspose.Words for C++ API provides the ImportFormatOptions class for customizing the merging of Word files. The following are the options provided by the ImportFormatOptions class.
- IgnoreHeaderFooter: Specifies whether to ignore the formatting of headers/footers content when the ImportFormatMode::KeepSourceFormatting mode is used.
- IgnoreTextBoxes: Specifies whether to ignore the source formatting of textboxes when the ImportFormatMode::KeepSourceFormatting mode is used.
- KeepSourceNumbering: Specifies how to import the numbering when it conflicts in source and destination documents.
- SmartStyleBehavior: Specifies how to import styles when they have the same names in source and destination documents.
The following are the steps to merge multiple Word documents with additional options:
- Load the destination document using the Document class.
- Load the source document using the Document class.
- Create an instance of the ImportFormatOptions class and set the desired options.
- Merge the documents using Document->AppendDocument ( System::SharedPtr<Aspose::Words::Document> srcDoc, Aspose::Words::ImportFormatMode importFormatMode, System::SharedPtr<Aspose::Words::ImportFormatOptions> importFormatOptions) method.
- Save the destination Word document using Document->Save (System::String fileName, Aspose::Words::SaveFormat saveFormat) method.
The following is the sample code to merge Word documents with additional options.
The following images compare the merged documents generated by setting the IgnoreHeaderFooter option to true and false.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to merge multiple Word documents using C++. Furthermore, you have learned how to use additional options for customizing the merging of Word Files. Aspose.Words for C++ API provides a bunch of features for working with Word files. You can explore the API in detail by using the official documentation. If you have any questions, please feel free to contact us on the forum.