Merge Documents

Merge Word Documents Online in Java

Microsoft Word is practically available everywhere including the work computers, the computers at school, and your home PC. We create letters, reports, etc in word format and use clip art, write in a variety of fonts and sizes. Over the period of time, we generate a plethora of documents and for long-term archival, these documents need to be combined. So in this article, we are going to learn the technique to merge documents in Java.

Merge Documents API

Aspose.Words Cloud provides the features to create, edit and render MS Word as well as OpenOffice documents and save them to a variety of supported formats. It also supports the feature to load and combine Word documents into one single consolidated document. So let’s add the following details to your pom.xml of maven build type so that Aspose.Words Cloud SDK for Java is added to the project.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.cloud/repo/</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.aspose</groupId>
         <artifactId>aspose-words-cloud</artifactId>
        <version>22.5.0</version>
</dependency>

In case you haven’t already, register a free account over Aspose.Cloud dashboard using GitHub or Google account or simply Sign Up and obtain your Client Credentials.

Merge Word Documents in Java

Please follow the instructions given below to merge documents:

  • 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. 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 the href of the source Word document and set the 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

Combine Word Documents using cURL Commands

The cURL commands are widely popular because of their ability to be flexible in completing complex tasks. We can also use cURL commands to combine word documents. Before proceeding further, we need to generate a JWT access token as the REST APIs are only accessible to authorized persons. So please execute the following command to generate the JWT access token.

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"

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

Once the JWT is created, please execute the following command to combine file-sample.docx and test_multi_pages.docx files. As a result, a final merged file will be 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>"

Conclusion

We have learned about the steps to merge word documents using Java code snippets. At the same time, we have also learned the details on how to combine word documents using cURL commands. You may also consider exploring the exciting features of API through the Swagger interface in a web browser.

Furthermore, our Cloud SDKs are open source and the 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.

We highly recommend visiting the following links to learn about: