概述

合併 Microsoft Word 文件是一項常見任務,對於許多應用程式尤其在文檔管理系統中至關重要。無論您是在整合報告文件、合併專案文件,還是準備法律文件,以程式方式合併文件的能力都可以節省時間並提高效率。

在本文中,我們將探索如何使用 GroupDocs.Merger for Node.js via Java 庫在 Node.js 中合併 DOCX 文件。我們將提供逐步的說明和代碼示例,以演示如何無縫實現這一點。

如何合併 DOCX 文件

在 Node.js 中合併 DOCX 文件可以輕鬆地通過 GroupDocs.Merger for Node.js via Java API 實現。以下是將多個 DOCX 文件合併為單個文檔的主要步驟:

  1. 初始化 Merger: 使用第一個源 DOCX 文件的路徑創建 Merger 類的實例。
  2. 加入其他文檔: 使用 Merger 類的 join 方法來添加其他您希望合併的 DOCX 文件。
  3. 保存結果: 調用 save 方法以在指定的輸出路徑中存儲合併的文檔。

以下是演示如何合併多個 DOCX 文件的代碼片段:

// 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(`合併至 ${outputPath}`);
    
    // Step 3: Join additional DOCX files
    merger.join("path/to/secondfile.docx"); // 在此處添加您的第二個文件路徑
    
    // Step 4: Save the merged document
    return merger.save(outputPath);
}

module.exports = mergeDocx;

使用預定合規模式合併 Word 文檔

在某些情況下,您可能需要在確保符合特定標準的情況下合併 Word 文檔。GroupDocs.Merger for Node.js via Java 庫允許您在合併文檔時設置合規模式。

主要步驟:

  1. 初始化 Merger: 照常創建實例。
  2. 設置合規模式: 定義您需要的合規等級(例如,ISO/IEC 29500:2008 嚴格)。
  3. 合併並保存: 考慮合規性合併文檔並保存結果。

這是如何在代碼中實現的:

// 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(`合併至 ${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); // 在此處添加您的第二個文件路徑
    return merger.save(outputPath);
}

module.exports = mergeWordDocumentsWithPredefinedComplianceMode;

處理多個 DOCX 文件

您可以通過遍歷文件路徑的數組來輕鬆處理多個 DOCX 文件進行合併。這樣可以消除冗餘代碼,並使您的應用更高效。

合併多個文件的步驟:

  1. 創建文件路徑的數組: 設定一個包含要合併的 DOCX 文件路徑的數組。
  2. 初始化 Merger: 從第一個文件開始。
  3. 循環遍歷數組: 使用循環將每個文件加入 Merger 實例。
  4. 保存合併的文檔: 保存最終的輸出文件。

示例代碼:

// 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(`合併至 ${outputPath}`);
    return merger.save(outputPath);
}

module.exports = mergeMultipleDocx;

參考鏈接

如需進一步閱讀和擴展您的技能,請查看以下有用的鏈接:

下載免費試用版

您可以從 releases.groupdocs.com 下載 GroupDocs.Merger for Node.js via Java 的免費試用版。此外,您還可以在 Temporary License 獲取臨時許可證,允許您在試用期間無限制使用該庫。

範例

以下是一些實用的範例,演示如何有效地利用 GroupDocs.Merger for Node.js via Java SDK 來合併 DOCX 文件:

範例 1:基本的 DOCX 合併

此範例顯示如何輕鬆地將兩個 DOCX 文件合併為一個。

// 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(`合併的文檔已保存於: ${outputPath}`);
}

// Call the merge function
mergeTwoDocxFiles("path/to/firstfile.docx", "path/to/secondfile.docx", "path/to/outputFolder");

範例 2:合併多個 DOCX 文件

在此範例中,您可以看到如何使用文件路徑數組以更簡單的代碼合併多個文件。

// 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(`所有文檔已合併並保存於: ${outputPath}`);
}

// Call the function to merge multiple files
mergeMultipleDocx(["path/to/file1.docx", "path/to/file2.docx", "path/to/file3.docx"], "path/to/outputFolder");

範例 3:帶有合規模式的 DOCX 合併

此範例演示如何在確保符合特定文檔標準的情況下合併文檔。

// 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(`合併的文檔已帶有合規性保存於: ${outputPath}`);
}

// Call the function
mergeWithCompliance("path/to/firstfile.docx", "path/to/secondfile.docx", "path/to/outputFolder");

參考鏈接

如需獲得更多信息和資源,請探索以下鏈接:

下載免費試用版

您可以從 releases.groupdocs.com 下載 GroupDocs.Merger for Node.js via Java 的免費試用版。此外,您還可以在 Temporary License 獲取臨時許可證,允許您在試用期間無限制使用該庫。