Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active July 1, 2025 11:50
Show Gist options
  • Save GroupDocsGists/ba6a4245b1312d0f5a47b47659c14a7e to your computer and use it in GitHub Desktop.
Save GroupDocsGists/ba6a4245b1312d0f5a47b47659c14a7e to your computer and use it in GitHub Desktop.
How to merge Word documents in Java

Merge Word Documents in Java

In this snippet, you’ll discover how to merge Word documents, leveraging a concise Java example to efficiently combine multiple DOCX files. This functionality is essential for developers looking to merge Word documents seamlessly without compromising on quality or formatting.

πŸ“¦ Prerequisites

To begin, ensure you have the following in place:

  • Install GroupDocs.Merger SDK to simplify document handling in Java.
  • Activate your GroupDocs license key for unrestricted usage. You can get a free trial of GroupDocs.Merger and use a temporary license if needed.
  • This works for DOCX and DOC file types.

🧩 Key Capabilities

This guide demonstrates several powerful features, including:

  • Merge multiple DOCX files into a single consolidated document.
  • Control formatting and compliance using predefined options for each document.
  • Merge Word documents continuously, allowing you to avoid page breaks.
  • Support for various formats including DOC, DOCX, and more.
  • Efficiently save the merged file in your desired output format.

πŸ’» Code Examples

See the following examples for how to merge Word documents:

βœ… How to Use in Java

Follow these steps to merge Word documents successfully:

  1. Install the GroupDocs.Merger package via Maven.
  2. Load your source DOCX files using the Merger class.
  3. Configure the appropriate join options to suit your needs (e.g., continuous merging).
  4. Call the join method to add additional documents for merging.
  5. Save the output file to the designated output folder and format.
  6. Check the merged file to ensure that all contents and formatting are maintained.

πŸ“Ž Related Resources

πŸ“œ Conclusion

That’s it! Now you know how to merge Word documents using Java seamlessly. By following these steps, you can achieve efficient document management and streamline your workflow. Check out our documentation or grab a trial to explore more functionalities like combining PDFs or joining images for your project needs.

import com.groupdocs.merger.Merger;
import com.groupdocs.merger.examples.Constants;
import java.io.File;
/**
* This example demonstrates how to merge multiple DOCX files into single file.
* For more details about merging Microsoft Word Open XML Document (.docx) files please check this documentation article
* https://docs.groupdocs.com/merger/java/merge/word/
*/
public class MergeDocx
{
public static void run() throws Exception
{
String outputFolder = Constants.OutputPath;
String outputFile = new File(outputFolder, "merged.docx").getPath();
// Load the source DOCX file
Merger merger = new Merger(Constants.SAMPLE_DOCX);
{
// Add another DOCX file to merge
merger.join(Constants.SAMPLE_DOCX_2);
// Merge DOCX files ans save result
merger.save(outputFile);
}
System.out.print("\nDOCX files merge completed successfully. \nCheck output in "+ outputFolder);
}
}
import com.groupdocs.merger.Merger;
import com.groupdocs.merger.domain.options.WordJoinMode;
import com.groupdocs.merger.domain.options.WordJoinOptions;
import com.groupdocs.merger.examples.Constants;
import java.io.File;
/**
* This example demonstrates how merge Word documents without starting from a new page into single file.
* For more details about merging Microsoft Word Document (.doc) files please check this documentation article
* https://docs.groupdocs.com/merger/java/merge/word/
*/
public class MergeWordDocumentsWithoutStartingFromNewPage
{
public static void run() throws Exception
{
System.out.print("=======================================================================");
System.out.print("");
System.out.print("Example Basic Usage: MergeWordDocumentsWithoutStartingFromNewPage");
System.out.print("");
String outputFolder = Constants.OutputPath;
String outputFile = new File(outputFolder, "merged.doc").getPath();
// Load the source DOC file
Merger merger = new Merger(Constants.SAMPLE_DOC);
{
// Define Word join options
WordJoinOptions joinOptions = new WordJoinOptions();
joinOptions.setMode(WordJoinMode.Continuous);
// Add another DOC file to merge
merger.join(Constants.SAMPLE_DOC_2, joinOptions);
// Merge DOC files and save result
merger.save(outputFile);
}
System.out.print("\nDOC files merge completed successfully. \nCheck output in "+outputFolder );
}
}
import com.groupdocs.merger.Merger;
import com.groupdocs.merger.domain.options.WordJoinCompliance;
import com.groupdocs.merger.domain.options.WordJoinOptions;
import com.groupdocs.merger.examples.Constants;
import java.io.File;
/**
* This example demonstrates how merge Word documents with pre-defined Compliance mode into single file.
* For more details about merging Microsoft Word Document (.docx) files please check this documentation article
* https://docs.groupdocs.com/merger/java/merge/word/
*/
public class MergeWordDocumentsWithPredefinedComplianceMode
{
public static void run() throws Exception
{
System.out.print("=======================================================================");
System.out.print("");
System.out.print("Example Basic Usage: MergeWordDocumentsWithPredefinedComplianceMode");
System.out.print("");
String outputFolder = Constants.OutputPath;
String outputFile = new File(outputFolder, "merged.docx").getPath();
// Load the source DOCX file
Merger merger = new Merger(Constants.SAMPLE_DOCX);
{
// Define Word join options
WordJoinOptions joinOptions = new WordJoinOptions();
joinOptions.setCompliance(WordJoinCompliance.Iso29500_2008_Strict);
// Add another DOCX file to merge
merger.join(Constants.SAMPLE_DOCX_2, joinOptions);
// Merge DOCX files and save result
merger.save(outputFile);
}
System.out.print("\nDOCX files merge completed successfully. \nCheck output in " +outputFolder );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment