How to merge two Word DOCX files together in C# .NET Framework

Functioning like a virtual stapler, document merging is an excellent feature to have in an app. Today, we will not be trudging through all the code required to make this happen the old-fashioned way. No, today we will be taking a massive shortcut in the form of an API. Total time: five minutes.

Let’s begin by using NuGet to install our API Client. This is a breeze with the Package Manager console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 2.2.0

To call MergeDocumentDocx, we can use the following code. Be sure to provide inputFile1 (starting file) and inputFile2 (the file to be added).

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class MergeDocumentDocxExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new MergeDocumentApi();var inputFile1 = new System.IO.Stream(); // System.IO.Stream | First input file to perform the operation on.var inputFile2 = new System.IO.Stream(); // System.IO.Stream | Second input file to perform the operation on (more than 2 can be supplied).try{// Merge Two Word DOCX Togetherbyte[] result = apiInstance.MergeDocumentDocx(inputFile1, inputFile2);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling MergeDocumentApi.MergeDocumentDocx: " + e.Message );}}}}

Done! Before you pop the champagne to celebrate all the time you just saved, you may wish to take a look at some of our other functions in this same API. These range everywhere from converting between numerous popular formats to creating and editing document files.

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store