Tổng quan
Gộp các tài liệu Microsoft Word là một nhiệm vụ phổ biến có thể rất quan trọng cho nhiều ứng dụng, đặc biệt trong các hệ thống quản lý tài liệu. Dù bạn đang hợp nhất các tệp báo cáo, kết hợp tài liệu dự án, hay chuẩn bị giấy tờ pháp lý, khả năng hợp nhất các tệp một cách lập trình có thể tiết kiệm thời gian và tăng hiệu suất.
Trong bài viết này, chúng ta sẽ khám phá cách gộp các tệp DOCX trong Node.js sử dụng thư viện GroupDocs.Merger for Node.js via Java. Chúng tôi sẽ cung cấp hướng dẫn từng bước và các ví dụ mã để chứng minh cách thực hiện điều này một cách liền mạch.
Cách gộp các tệp DOCX
Việc gộp các tệp DOCX trong Node.js có thể được thực hiện dễ dàng với API GroupDocs.Merger for Node.js via Java. Dưới đây là các bước chính liên quan đến việc gộp nhiều tệp DOCX thành một tài liệu duy nhất:
- Khởi tạo Merger: Tạo một thể hiện của lớp Merger bằng cách sử dụng đường dẫn của tệp DOCX nguồn đầu tiên.
- Tham gia các tài liệu bổ sung: Sử dụng phương thức join của lớp Merger để thêm các tệp DOCX khác mà bạn muốn gộp.
- Lưu kết quả: Gọi phương thức save để lưu tài liệu đã gộp vào đường dẫn đầu ra đã chỉ định.
Dưới đây là một đoạn mã mẫu minh họa cách gộp nhiều tệp 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(`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;
Gộp tài liệu Word với chế độ tuân thủ đã định sẵn
Trong một số trường hợp, bạn có thể cần gộp các tài liệu Word trong khi đảm bảo tuân thủ các tiêu chuẩn cụ thể. Thư viện GroupDocs.Merger for Node.js via Java cho phép bạn thiết lập một chế độ tuân thủ khi gộp tài liệu.
Các bước chính:
- Khởi tạo Merger: Tạo một thể hiện như thường lệ.
- Thiết lập chế độ tuân thủ: Định nghĩa mức độ tuân thủ bạn cần (ví dụ, ISO/IEC 29500:2008 Strict).
- Tham gia và lưu: Gộp các tài liệu trong khi xem xét chế độ tuân thủ và lưu kết quả.
Dưới đây là cách thực hiện điều này trong mã:
// 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;
Xử lý nhiều tệp DOCX
Bạn có thể dễ dàng xử lý nhiều tệp DOCX khi gộp chúng bằng cách lặp qua một mảng các đường dẫn tệp. Điều này loại bỏ sự cần thiết phải viết mã lặp lại và làm cho ứng dụng của bạn hiệu quả hơn.
Các bước để gộp nhiều tệp:
- Tạo một mảng các đường dẫn tệp: Thiết lập một mảng chứa các đường dẫn của các tệp DOCX cần gộp.
- Khởi tạo Merger: Bắt đầu với tệp đầu tiên.
- Lặp qua mảng: Sử dụng một vòng lặp để kết hợp từng tệp vào thể hiện Merger.
- Lưu tài liệu đã gộp: Lưu tệp đầu ra cuối cùng.
Mã ví dụ:
// 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;
Xem thêm
Để tìm hiểu thêm và mở rộng kỹ năng của bạn, hãy kiểm tra các liên kết hữu ích sau:
Tải xuống bản dùng thử miễn phí
Bạn có thể tải xuống bản dùng thử miễn phí của GroupDocs.Merger for Node.js via Java từ releases.groupdocs.com. Ngoài ra, bạn có thể mua giấy phép tạm thời tại Giấy phép tạm thời, cho phép bạn sử dụng thư viện mà không có bất kỳ hạn chế nào trong thời gian dùng thử.
Ví dụ
Dưới đây là một số ví dụ thực tế minh họa cách sử dụng SDK GroupDocs.Merger for Node.js via Java để gộp hiệu quả các tệp DOCX:
Ví dụ 1: Gộp DOCX cơ bản
Ví dụ này cho thấy cách dễ dàng gộp hai tệp DOCX thành một.
// 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");
Ví dụ 2: Gộp nhiều tệp DOCX
Trong ví dụ này, bạn có thể thấy cách gộp nhiều tệp với mã đơn giản hơn bằng cách sử dụng một mảng các đường dẫn tệp.
// 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");
Ví dụ 3: Gộp DOCX với chế độ tuân thủ
Ví dụ này minh họa cách gộp các tài liệu trong khi đảm bảo tuân thủ các tiêu chuẩn tài liệu cụ thể.
// 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");
Xem thêm
Để biết thêm thông tin và tài nguyên, hãy khám phá các liên kết sau:
- Tài liệu GroupDocs.Merger
- Tham khảo API
- Ví dụ trên GitHub
- Thông tin phát hành
- Cách tích hợp GroupDocs.Merger vào ứng dụng của bạn
Tải xuống bản dùng thử miễn phí
Bạn có thể tải xuống bản dùng thử miễn phí của GroupDocs.Merger for Node.js via Java từ releases.groupdocs.com. Ngoài ra, bạn có thể mua giấy phép tạm thời tại Giấy phép tạm thời, cho phép bạn sử dụng thư viện mà không có bất kỳ hạn chế nào trong thời gian dùng thử.