Overview

Merging PDF files is a common requirement for developers working with document processing. In this blog post, we’ll explore how to merge multiple PDF files into a single document using GroupDocs.Merger for Node.js via Java. This library allows you to combine PDF files programmatically without needing any third-party software, streamlining your workflow and enhancing productivity.

In our step-by-step guide, you’ll learn how to:

  • Initialize the merger class with a source PDF file.
  • Add additional PDF files to the merger.
  • Save the combined PDF file.

How to merge PDF files in Node.js

To merge PDF files in Node.js, you first need to instantiate the Merger class with the path to the source PDF file. After that, you can add other PDF files that you want to include in the merged document, and finally save the resulting PDF file.

Steps to merge PDF files:

  1. Load the source PDF file - Create an instance of the Merger class using the path of the first PDF.
  2. Add other PDF files - Use the join method to include additional PDF files in the merger.
  3. Save the resulting PDF file - Call the save method to output the final merged document to your desired location.

Here’s an illustrative code snippet demonstrating these steps:

// Import the GroupDocs.Merger for Node.js library
const GroupDocs = require("groupdocs-merger");

// Function to merge PDF files
async function mergePdf(groupdocs, inputFilePath) {
    // Create a new Merger instance with the source PDF file
    const merger = new groupdocs.merger.Merger(inputFilePath);
    
    // Define the output file path for the merged PDF
    const outputPath = `${groupdocs.outputFolder}/MergePdf.pdf`;
    
    console.log(`Merged to ${outputPath}`);
    
    // Add another PDF file to merge
    merger.join("path/to/your/secondPDF.pdf");
    
    // Save the merged PDF to the specified output path
    await merger.save(outputPath);
}

// Example usage
mergePdf(GroupDocs, "path/to/your/firstPDF.pdf");

Code example for merging PDFs

In the code example provided above, we perform the following major steps:

  • Import the necessary library: This step ensures that GroupDocs.Merger is available in your application.
  • Instantiate the Merger class: The first PDF file’s path is passed to the Merger constructor to initialize the merging process.
  • Join additional PDFs: Each additional PDF file is joined using the join method. This method allows you to combine multiple PDF documents into a single file.
  • Save the final document: By calling the save method, we write the new merged PDF to disk.

Using bookmarks while merging PDFs

If you want to maintain bookmarks in the merged PDF, you can configure the merging process to include this feature using certain options.

The example below demonstrates how to merge PDF files while keeping bookmarks:

// Import the GroupDocs.Merger for Node.js library
const GroupDocs = require("groupdocs-merger");

// Function to merge PDFs with bookmarks
async function mergePdfWithBookmarks(groupdocs, inputFilePath) {
    // Create a new Merger instance
    const merger = new groupdocs.merger.Merger(inputFilePath);
    
    // Define output file path
    const outputPath = `${groupdocs.outputFolder}/MergePdfWithBookmarks.pdf`;
    
    // Create options to use bookmarks
    const pdfJoinOptions = {
        useBookmarks: true // Enable bookmarks
    };

    console.log(`Merged to ${outputPath}`);
    
    // Add second PDF with bookmarks
    merger.join("path/to/your/secondPDF.pdf", pdfJoinOptions);
    
    // Save the merged PDF to the specified output path
    await merger.save(outputPath);
}

// Example usage
mergePdfWithBookmarks(GroupDocs, "path/to/your/firstPDF.pdf");

In this updated example, we specify options to retain bookmarks when merging PDFs. The addition of useBookmarks: true ensures the bookmarks from the original files are retained in the merged document.

Conclusion

Merging PDF files in Node.js using GroupDocs.Merger for Node.js via Java is a straightforward process that can significantly streamline your document management tasks. Whether you need to combine various documents for reporting or send multiple files as a single attachment, this library provides the necessary tools to perform these tasks programmatically, without the need for third-party software.


See also

Don’t forget that you can download a free trial from releases.groupdocs.com and acquire a temporary license here. A temporary license allows you to try our library without any restrictions.

Examples

To facilitate your understanding of merging PDF files using GroupDocs.Merger for Node.js via Java, here are a couple of practical examples that showcase the library’s capabilities in action.

Basic PDF Merging Example

In this example, we will merge two simple PDF files into one.

// Import the GroupDocs.Merger for Node.js library
const GroupDocs = require("groupdocs-merger");

// Function to merge two PDF files
async function mergeTwoPdfFiles(groupdocs) {
    // Define paths for the source PDF files
    const firstPdfPath = "path/to/your/firstPDF.pdf";
    const secondPdfPath = "path/to/your/secondPDF.pdf";
    
    // Create a new Merger instance for the first PDF
    const merger = new groupdocs.merger.Merger(firstPdfPath);
    
    // Merge the second PDF into the first one
    merger.join(secondPdfPath);
    
    // Define the output path for the merged PDF
    const outputPath = `${groupdocs.outputFolder}/MergedTwoPdfFiles.pdf`;
    
    // Save the result into the specified output path
    await merger.save(outputPath);
    console.log(`Merged files saved to: ${outputPath}`);
}

// Example usage
mergeTwoPdfFiles(GroupDocs);

Merging with Multiple Files Example

In this scenario, we will merge three PDF files into a single document.

// Import the GroupDocs.Merger for Node.js library
const GroupDocs = require("groupdocs-merger");

// Function to merge multiple PDF files
async function mergeMultiplePdfFiles(groupdocs) {
    // Define paths for the source PDF files
    const pdfPaths = [
        "path/to/your/firstPDF.pdf",
        "path/to/your/secondPDF.pdf",
        "path/to/your/thirdPDF.pdf"
    ];
    
    // Create a new Merger instance with the first PDF
    const merger = new groupdocs.merger.Merger(pdfPaths[0]);
    
    // Loop through and merge all additional PDFs
    for (let i = 1; i < pdfPaths.length; i++) {
        await merger.join(pdfPaths[i]);
    }
    
    // Define the output path for the merged PDF
    const outputPath = `${groupdocs.outputFolder}/MergedMultiplePdfFiles.pdf`;
    
    // Save the result into the specified output path
    await merger.save(outputPath);
    console.log(`Merged multiple files saved to: ${outputPath}`);
}

// Example usage
mergeMultiplePdfFiles(GroupDocs);

These examples show you how to efficiently merge one or more PDF documents using GroupDocs.Merger for Node.js via Java. The flexibility of the library allows developers to customize the merging process according to their specific needs.

See also

Free Trial Download and Temporary License Information

You can get started with GroupDocs.Merger for Node.js via Java by downloading a free trial from releases.groupdocs.com. A free trial allows you to explore the library’s features without limitations.

If you wish to test the library without restrictions, you can acquire a temporary license by visiting this link. The temporary license enables you to utilize our library fully during your evaluation period.