How to Merge Two Word DocX Documents Together using Java

Merging DocX documents is a common task performed in offices everywhere. With the inclusion of our DocX Merging API, you can easily build a solution for that need into your website/application as a cloud service — and you can do it with minimal coding legwork. To help you take advantage of our API, we’ve provided ready-to-run code examples in Java to structure your API call.

Before we begin, let’s outline the parameters you’ll need to satisfy for this API. Those include:

1. Your first, second, third, etc. DocX document to perform the operation on

2. Your Cloudmersive API key — obtainable by registering a free account on our website

Once you have those parameters ready, let’s begin installing the Maven SDK. Add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Now add one to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Finally, include the import classes and copy in the remaining code below:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.MergeDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
MergeDocumentApi apiInstance = new MergeDocumentApi();
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on (more than 2 can be supplied).
try {
byte[] result = apiInstance.mergeDocumentDocx(inputFile1, inputFile2);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling MergeDocumentApi#mergeDocumentDocx");
e.printStackTrace();
}

No additional code needed — you’re all done. With your API key, you’ll get a limit of 800 API calls per month, including access to dozens of additional file conversion/administration APIs & many other useful services.

--

--

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
Cloudmersive

Cloudmersive

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