The merger of multiple MS Word documents could be needed in various scenarios such as reducing the number of documents, keeping similar kinds of content in a single file (i.e. invoices), and etc. Many online applications let you merge two or more MS Word documents, however, you may need to implement document merging features within your .NET applications. For such a case, in this article, you will learn how to merge MS Word DOCX documents in C#.
- C# API for Merging MS Word Documents
- Merge MS Word DOCX Documents using C#
- Additional Options to Merge MS Word Documents
Merge Word Documents with C# API
Aspose.Words for .NET is a popular word processing API that lets you create, manipulate, and play with MS Word document formats within C# applications. Along with a wide range of document generation features, the API allows you to merge MS Word documents quite easily. You can download the API or install it from NuGet.
PM> Install-Package Aspose.Words
Merge MS Word DOCX Documents using C#
Merging MS Word documents using Aspose.Words for .NET is as easy as pie. Just load the documents and merge them by calling a single method. Below are the two MS Word documents that you will merge in the following code samples.
The following are the steps to merge two MS Word documents.
- Load the destination Word document using the Document class.
- Load the source Word document using the Document class.
- Merge source Word document into destination document using Document.AppendDocument(Document, ImportFormatMode) method.
- Set ImportFormatMode in the AppendDocument method to specify the formatting of the source document.
- Save the destination Word document using Document.Save(String, SaveFormat) method.
The following code sample shows how to merge MS Word documents (DOCX) using C#.
The following is the screenshot of the merged MS Word document.
Combine MS Word DOCX with Additional Options
Aspose.Words for .NET also provides some additional options to customize the appearance of the source document after performing the merger. The ImportFormatOptions class provides various customization options such as IgnoreHeaderFooter, IgnoreTextBoxes, etc.
The following are the steps to merge two MS Word documents with additional options.
- Load the destination Word document using the Document class.
- Load the source Word document using the Document class.
- Create an instance of ImportFormatOptions class and set the desired options.
- Merge source Word document into destination document using Document.AppendDocument(Document, ImportFormatMode, ImportFormatOptions) method.
- Save the destination Word document using Document.Save(String, SaveFormat) method.
The following code sample shows how to merge two MS Word documents with additional options using C#.
Conclusion
In this article, you have seen how to merge MS Word DOCX documents using C#. Furthermore, you have learned how to use the additional options to customize the document merging feature. You can explore more about Aspose.Words for .NET using the documentation. In case you would have any questions, you can contact us via our forum.