Merge MS Word Documents using C++

Merge Word Documents

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

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.

Word documents to merge

The following are the steps for merging Word documents using C++.

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.

Merged Word document generated using ImportFormatOptions::KeepSourceFormatting
Output generated using ImportFormatMode::KeepSourceFormatting
Merged Word document generated using ImportFormatOptions::UseDestinationStyles
Output generated using ImportFormatMode::UseDestinationStyles

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:

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.

Merged Word document generated by setting the IgnoreHeaderFooter option to true
Output generated by setting the IgnoreHeaderFooter option to true
Merged Word document generated by setting the IgnoreHeaderFooter option to false
Output generated by setting the IgnoreHeaderFooter option to 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.

See Also