Overview
Merging Microsoft Word documents is a common task that can be crucial for many applications, especially in document management systems. Whether you are consolidating report files, combining project documents, or preparing legal papers, the ability to programmatically merge files can save time and increase efficiency.
In this article, we will explore how to merge DOCX files in Node.js using the GroupDocs.Merger for Node.js via Java library. We will provide step-by-step instructions and code examples to demonstrate how to achieve this seamlessly.
How to merge DOCX files
Merging DOCX files in Node.js can be accomplished easily with the GroupDocs.Merger for Node.js via Java API. Below are the key steps involved in merging multiple DOCX files into a single document:
- Initialize the Merger: Create an instance of the Merger class using the path of the first source DOCX file.
- Join Additional Documents: Use the join method of the Merger class to add other DOCX files you wish to merge.
- Save the Result: Call the save method to store the merged document in the specified output path.
Here is a code snippet demonstrating how to merge multiple DOCX files:
// Importing the necessary library
const groupdocs = require('groupdocs-merger');
// Function to merge DOCX files
async function mergeDocx(groupdocs, inputFilePath) {
// Step 1: Initialize the Merger with the first file
const merger = new groupdocs.merger.Merger(inputFilePath);
// Step 2: Specify the output path
const outputPath = `${groupdocs.outputFolder}/MergeDocx.docx`;
console.log(`Merged to ${outputPath}`);
// Step 3: Join additional DOCX files
merger.join("path/to/secondfile.docx"); // Add your second file path here
// Step 4: Save the merged document
return merger.save(outputPath);
}
module.exports = mergeDocx;
Merge Word documents with pre-defined Compliance mode
In some cases, you may need to merge Word documents while ensuring compliance with specific standards. The GroupDocs.Merger for Node.js via Java library allows you to set a compliance mode when merging documents.
Key Steps:
- Initialize the Merger: Create an instance as usual.
- Set Compliance Mode: Define the compliance level you need (e.g., ISO/IEC 29500:2008 Strict).
- Join and Save: Merge the documents while taking the compliance into account and save the result.
Here’s how to do this in code:
// Function to merge Word documents with Compliance mode
async function mergeWordDocumentsWithPredefinedComplianceMode(groupdocs, inputFilePath) {
// Step 1: Initialize the Merger
const merger = new groupdocs.merger.Merger(inputFilePath);
// Step 2: Set output path
const outputPath = `${groupdocs.outputFolder}/MergeWordDocumentsWithPredefinedComplianceMode.docx`;
console.log(`Merged to ${outputPath}`);
// Step 3: Define Compliance level
const wordJoinOptionsCompliance = groupdocs.merger.WordJoinCompliance.Iso29500_2008_Strict;
const wordJoinOptions = new groupdocs.merger.WordJoinOptions();
wordJoinOptions.Compliance = wordJoinOptionsCompliance;
// Step 4: Join additional files and save the merged document
merger.join("path/to/secondfile.docx", wordJoinOptions); // Add your second file path here
return merger.save(outputPath);
}
module.exports = mergeWordDocumentsWithPredefinedComplianceMode;
Handling multiple DOCX files
You can easily handle multiple DOCX files when merging them by iterating through an array of file paths. This eliminates the need for repetitive code and makes your application more efficient.
Steps to Merge Multiple Files:
- Create an Array of File Paths: Set up an array that contains the paths of the DOCX files to be merged.
- Initialize the Merger: Start with the first file.
- Loop Through the Array: Use a loop to join each file to the Merger instance.
- Save the Merged Document: Save the final output file.
Example code:
// Function to merge multiple DOCX files
async function mergeMultipleDocx(groupdocs, filePaths) {
// Step 1: Initialize the Merger with the first file
const merger = new groupdocs.merger.Merger(filePaths[0]);
// Step 2: Join the rest of the files in the array
for (let i = 1; i < filePaths.length; i++) {
merger.join(filePaths[i]);
}
// Step 3: Save the merged document
const outputPath = `${groupdocs.outputFolder}/MergedMultipleDocx.docx`;
console.log(`Merged to ${outputPath}`);
return merger.save(outputPath);
}
module.exports = mergeMultipleDocx;
See also
For further reading and to expand your skills, check out the following useful links:
Download a Free Trial
You can download a free trial of GroupDocs.Merger for Node.js via Java from releases.groupdocs.com. Additionally, you can acquire a temporary license at Temporary License, allowing you to use the library without any restrictions during the trial period.
Examples
Here are some practical examples demonstrating how to utilize the GroupDocs.Merger for Node.js via Java SDK to merge DOCX files efficiently:
Example 1: Basic DOCX Merge
This example shows how to easily merge two DOCX files into one.
// Importing the necessary library
const groupdocs = require('groupdocs-merger');
// Function to merge two DOCX files
async function mergeTwoDocxFiles(inputFile1, inputFile2, outputFolder) {
// Initialize the Merger with the first file
const merger = new groupdocs.merger.Merger(inputFile1);
// Specify the output path
const outputPath = `${outputFolder}/MergedDoc.docx`;
// Adding the second DOCX file to merge
merger.join(inputFile2);
// Saving the merged document
await merger.save(outputPath);
console.log(`Merged document saved at: ${outputPath}`);
}
// Call the merge function
mergeTwoDocxFiles("path/to/firstfile.docx", "path/to/secondfile.docx", "path/to/outputFolder");
Example 2: Merging Multiple DOCX Files
In this example, you can see how to merge multiple files with simpler code by using an array of file paths.
// Function to merge multiple DOCX files
async function mergeMultipleDocx(inputFiles, outputFolder) {
// Initialize the Merger with the first file
const merger = new groupdocs.merger.Merger(inputFiles[0]);
// Join the rest of the files in the array
for (let i = 1; i < inputFiles.length; i++) {
merger.join(inputFiles[i]);
}
// Specify the output path
const outputPath = `${outputFolder}/MergedAllDocx.docx`;
// Saving the merged document
await merger.save(outputPath);
console.log(`All documents merged and saved at: ${outputPath}`);
}
// Call the function to merge multiple files
mergeMultipleDocx(["path/to/file1.docx", "path/to/file2.docx", "path/to/file3.docx"], "path/to/outputFolder");
Example 3: Merging DOCX with Compliance Mode
This example demonstrates how to merge documents while ensuring compliance with specific document standards.
// Function to merge documents with Compliance mode
async function mergeWithCompliance(inputFile1, inputFile2, outputFolder) {
const merger = new groupdocs.merger.Merger(inputFile1);
// Compliance mode set to ISO/IEC 29500:2008 Strict
const wordJoinOptions = new groupdocs.merger.WordJoinOptions();
wordJoinOptions.Compliance = groupdocs.merger.WordJoinCompliance.Iso29500_2008_Strict;
const outputPath = `${outputFolder}/MergedWithCompliance.docx`;
// Join the second file with compliance options
merger.join(inputFile2, wordJoinOptions);
// Save the resulting document
await merger.save(outputPath);
console.log(`Merged document saved with compliance at: ${outputPath}`);
}
// Call the function
mergeWithCompliance("path/to/firstfile.docx", "path/to/secondfile.docx", "path/to/outputFolder");
See also
For more information and resources, explore the following links:
- GroupDocs.Merger Documentation
- API Reference
- GitHub Examples
- Releases Information
- How to integrate GroupDocs.Merger into your application
Download a Free Trial
You can download a free trial of GroupDocs.Merger for Node.js via Java from releases.groupdocs.com. Additionally, you can acquire a temporary license at Temporary License, which allows you to use the library without any restrictions during the trial period.