Merge Word Documents

Merge Word Documents online in Java

In a distributed team environment, various members of the team may work on certain modules of the document, that need to be combined to produce a consolidated version. This operation can be performed using a variety of applications but manual steps to merge word documents can be a tedious activity. So in order to have a more viable solution, we are going to discuss the details of how to combine word documents using Java SDK.

Merge Documents API

Aspose.Words Cloud SDK for Java enables you to introduce Word document creation, manipulation, and transformation capabilities within Java applications. It also provides the feature to combine word documents to generate a single unified output. Now in order to use the SDK, please add the following details in your pom.xml file of maven build type.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.cloud/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words-cloud</artifactId>
        <version>22.5.0</version>
    </dependency>
</dependencies>

After the installation, we need to register a free account over Aspose.Cloud dashboard using GitHub or Google account or simply Sign Up and obtain your Client Credentials.

Combine Word Documents in Java

Please follow the instructions given below to combine Word documents using a Java code snippet.

  • The first step is to create an object of WordsApi class while passing Client ID and Client Secret details as arguments
  • Secondly, create an object of DocumentEntry which takes the document to be merged and then set the value of the setImportFormatMode(..) method as KeepSourceFormatting
  • Now create an object of ArrayList and add the DocumentEntry object inside it
  • Then create an object of DocumentEntryList which takes the ArrayList object as an argument
  • Last but not the least, create an object of AppendDocumentOnlineRequest which take the source Word file and DocumentEntryList object as arguments
  • Finally, call the appendDocumentOnline(..) method of API to merge the documents and save the output in Cloud storage

Merge Word Documents using cURL Commands

The cURL commands can also be used to access REST APIs on any platform. So in this section, we are going to discuss the details on how to merge word documents using cURL commands. Now the first step is to generate JSON Web Token (JWT), so please execute the following command in the terminal application.

 curl -v "https://api.aspose.cloud/connect/token" \
 -X POST \
 -d "grant_type=client_credentials&client_id=a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4&client_secret=d87269aade6a46cdc295b711e26809af" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 -H "Accept: application/json"

Once we have the JWT Token, please execute the following command to merge word documents already available in cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/ComparisonResult.docx/appendDocument?destFileName=MergedFile.docx" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"DocumentEntries\":[{\"Href\":\"SampleMailMergeTemplate.docx\",\"ImportFormatMode\":\"KeepSourceFormatting\"}],\"ApplyBaseDocumentHeadersAndFootersToAppendingDocuments\":true}"

Conclusion

We have discussed the details of how to combine word documents in Java as well as using cURL commands. Please note that the complete source code of SDK can be downloaded from GitHub. Furthermore, in order to explore the API capabilities, you may consider accessing it via the swagger interface.

In case you have any further queries or you face any difficulty, please visit the free support forum.

We highly recommend going through the following blogs