Merge MS Word files using Java REST API

Merge Word documents

Microsoft Word is available practically everywhere. It comes standard on many PCs and you can typically find it on your work computer, the computers at school, and your home PC. The MS Word application lets you create simple word-processing documents such as letters and reports and you can make them as basic or you can add color, use clip art, write in a variety of fonts and sizes, and use tables, borders, and bullet formatting. It also offers to use the templates for any type of document and mail merge from a database so that you can easily send out the letters to multiple people at a time.

You can align the text whether at the center, right, or left margins or justified. With one click, you may instantly check spelling and grammatical mistakes. Due to all such features, people generate a plethora of these documents on daily basis and for long-term archival, the documents need to be combined into a single large document. So in this article, we are going to learn the technique to merge MS Word files using Java REST API.

Word documents processing REST API

Aspose.Words Cloud is our award-winning REST-based API offering a plethora of options to create, edit and render MS Word as well as OpenOffice documents to a variety of supported formats. It also supports the feature to load multiple MS Word files and combine them into one single consolidated document. The exciting part about API is that you can load a variety of input formats including DOC, DOT, DOCX, DOTX, RTF, ODT, OTT, TXT, files. So you can easily merge them into a single consolidated resultant file either in DOCX or other supported formats.

cURL command to merge DOCX files

The client URL is a free command-line tool popular for data transfer to or from a network server, using one of the supported (HTTP, HTTPS, FTP, etc). It uses URL syntax to transfer data to and from servers. cURL is widely popular because of its ability to be flexible and complete complex tasks. The same cURL syntax can merge MS Word files over the command prompt. But before we proceed, the first step is to generate a JWT access token because our REST APIs are only accessible to authorized persons.

Please visit Aspose.Cloud dashboard and if you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

The following command shows how to create a JWT access token which is going to be used for MS Word files concatenation.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=c235e685-1aab-4cda-a95b-54afd63eb87f&client_secret=b8da4ee37494f2ef8da3c727f3a0acb9" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

In the following cURL command, the sample input file-sample.docx and test_multi_pages.docx are being merged. The final merged file is generated with the name Resultant.docx.

curl -v "https://api.aspose.cloud/v4.0/words/test_doc.docx/appendDocument?destFileName=Resultant.docx" \
-X PUT \
-d "{'DocumentEntries':[{'Href':'test_multi_pages.docx', 'ImportFormatMode':'KeepSourceFormatting'}]}" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer <JWT Token>"

For more information, please visit How to Obtain JWT token using a Client ID and Client Secret key.

Merge DOCX files using Java

Aspose.Words Cloud SDK for Java is specifically developed for Java programmers and is built on top of Aspose.Words Cloud REST API. All the features available in Cloud API are available in Java Cloud SDK.

In order to use Java Cloud SDK, the first step is to install Aspose.Words Cloud SDK for Java. The Cloud SDK is available for download over Maven and GitHub. Given below are the details on how to download and use Aspose.Words.jar in the Maven build project.

Add the following dependencies in your pom.xml file.

<repositories>
 <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>
<dependencies>
 <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words-cloud</artifactId>
        <version>21.4.0</version>
    </dependency>
 </dependencies>

The aspose-words-cloud-21.4.0.jar appears under the Maven Dependencies folder.

Given below are the steps to load and merge MS Word files:

  • First of all, specify Client ID and Client Secret information
  • Secondly, create an object of ApiClient class where we pass ClientID and ClientSecret as arguments to the constructor. The third argument is baseUrl and by default, it points to https://api.aspose.cloud/
  • Thirdly, create an object of WordsApi class that accepts the ApiClient object as an argument to the constructor
  • Now create an instance of DocumentEntry class and specify href of source Word document and set document import format as KeepSourceFormatting
  • Create an instance of DocumentEntryList class and pass DocumentEntry object (created in previous step) to addDocumentEntriesItem(…) method
  • The next step is to create an instance of AppendDocumentRequest class where we pass input Word document, DocumentEntryList object, and resultant merged file name as arguments to its constructor
  • Finally, call the method appendDocument(…) of WordsApi class to perform the conversion operation

Conclusion

We have learned about the steps to load input MS Word files and merge them into a single output document. Furthermore, in order to test the API, you may try using OpenAPI Specifications publicly exposed as a swagger and you can perform REST interactions directly from a web browser. Please note that our Cloud SDKs are open source and their complete source code can be downloaded from GitHub. We also recommend visiting product Documentation for details regarding other exciting features being offered by the API. Your feedback is very important to us. So, please feel free to contact us using our Support Forums.