Combining Word files programmatically is a frequent requirement when building document‑centric applications, especially for generating reports or consolidating contracts. GroupDocs.Merger Cloud SDK for Java provides a simple, cloud‑based library that lets you merge DOCX files without dealing with low‑level file handling. In this guide you will learn how to combine multiple Word documents in Java, see a complete code example, explore equivalent cURL calls, and understand the required setup. By the end you’ll be ready to integrate document merging into your own Java projects.
Full Working Example to Combine Multiple Word Documents in Java
This example demonstrates how to combine multiple Word documents in Java using the Merger API.
import com.groupdocs.merger.cloud.client.ApiClient;
import com.groupdocs.merger.cloud.client.ApiException;
import com.groupdocs.merger.cloud.api.MergerApi;
import com.groupdocs.merger.cloud.model.FileInfo;
import com.groupdocs.merger.cloud.model.MergeOptions;
import java.util.ArrayList;
import java.util.List;
public class CombineWordDocuments {
public static void main(String[] args) {
// Initialize the API client with your credentials
ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
MergerApi mergerApi = new MergerApi(apiClient);
try {
// List of Word documents to combine
List<FileInfo> sourceFiles = new ArrayList<>();
sourceFiles.add(new FileInfo().setFilePath("input1.docx"));
sourceFiles.add(new FileInfo().setFilePath("input2.docx"));
sourceFiles.add(new FileInfo().setFilePath("input3.docx"));
// Configure merge options
MergeOptions mergeOptions = new MergeOptions();
mergeOptions.setOutputPath("merged_output.docx");
mergeOptions.setFileInfos(sourceFiles);
// Execute the merge operation
FileInfo result = mergerApi.merge(mergeOptions);
System.out.println("Merge completed successfully. Output file: " + result.getFilePath());
} catch (ApiException e) {
System.err.println("An error occurred while merging documents: " + e.getMessage());
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Merge Word Files via REST API Using cURL
If you prefer a pure REST approach, the same merge operation can be performed with cURL calls. The steps below show how to obtain an access token, upload source files, request the merge, and download the result.
Authenticate and obtain an access token:
curl -X POST "https://api.groupdocs.cloud/v2.0/auth/token" \
-H "Content-Type: application/json" \
-d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'
Upload each DOCX file to your cloud storage (replace YOUR_ACCESS_TOKEN with the token from the previous step):
curl -X PUT "https://api.groupdocs.cloud/v2.0/storage/file/input1.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" \
--data-binary @input1.docx
Repeat the upload command for input2.docx and input3.docx.
Create the merge request:
curl -X POST "https://api.groupdocs.cloud/v2.0/merger/merge" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"outputPath":"merged_output.docx",
"fileInfos":[
{"filePath":"input1.docx"},
{"filePath":"input2.docx"},
{"filePath":"input3.docx"}
]
}'
Download the merged document:
curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/merged_output.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o merged_output.docx
For more details on request parameters, see the official API documentation.
Understanding the Process of Combining Word Documents in Java
Below is a step‑by‑step breakdown of the code shown earlier.
- Create an ApiClient -
new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")establishes a connection to the GroupDocs cloud service.ApiClient apiClient = new ApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"); - Instantiate MergerApi -
new MergerApi(apiClient)gives access to merge‑related endpoints.MergerApi mergerApi = new MergerApi(apiClient); - Prepare source file list - Each
FileInfoobject points to a DOCX file stored in the cloud. The list is passed toMergeOptions.List<FileInfo> sourceFiles = new ArrayList<>(); sourceFiles.add(new FileInfo().setFilePath("input1.docx")); - Configure MergeOptions - Set the desired output path and attach the list of source files.
MergeOptions mergeOptions = new MergeOptions(); mergeOptions.setOutputPath("merged_output.docx"); mergeOptions.setFileInfos(sourceFiles); - Execute merge -
mergerApi.merge(mergeOptions)sends the request and returns aFileInfofor the merged document.FileInfo result = mergerApi.merge(mergeOptions);
For a full list of available properties, refer to the API reference.
Prerequisites and Setup for GroupDocs.Merger Cloud SDK
Before you can run the code, add the Maven dependency to your project and ensure you have a valid GroupDocs account.
Add the SDK to your pom.xml:
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger-cloud</artifactId>
<version>25.11</version>
</dependency>
Download the latest JARs and source from the download page.
You will also need Java 8 or higher and internet access for the cloud calls.
Create a GroupDocs account, obtain your Client ID and Client Secret, and keep them secure.
Conclusion
Merging DOCX files programmatically is now straightforward with the GroupDocs.Merger Cloud SDK for Java. The example code and cURL snippets illustrate two flexible ways to combine multiple Word documents in Java, whether you prefer a native library or direct REST calls. Remember to configure your credentials, handle large files by streaming if needed, and test the merge operation with representative documents. For production deployments you will need a paid license; pricing details are available on the product page and you can request a temporary license for evaluation from the temporary license page.
FAQs
How do I combine multiple Word documents in Java without writing custom file‑handling code?
Use the GroupDocs.Merger Cloud SDK for Java. The SDK abstracts all low‑level operations and lets you merge DOCX files with a few API calls.Is it possible to merge documents that are stored in different cloud folders?
Yes. Provide the full cloud file paths in theFileInfoobjects, and the merge operation will fetch each file from its location before combining them.What should I do if the merge fails with an ApiException?
Check the exception message for details such as missing files or permission issues. Ensure the file paths are correct and that your account has read/write access to the storage. Consult the official documentation for error codes.Do I need a separate license for each environment (development, staging, production)?
A single license covers all environments, but you must respect the usage limits defined in your license agreement. For testing you can use the temporary license linked above.
