How to Merge Two PDF Files Using Java

1. Introduction

In this article, we will learn how to merge PDF files into a single document using Java. Merging PDF files is a common task in the IT industry and is often required in various applications such as document management systems, reporting and more.

In this article, we will demonstrate how to merge multiple PDF documents using IronPDF for Java. We will go through the process of setting up the environment, importing the library, reading the input files and merging them into a single document. By the end of this article, you will know exactly how to use IronPDF for Java to merge PDF files.

2. IronPDF for Java

IronPDF for Java is a powerful library that allows developers to create new PDF documents from scratch and convert various file formats to PDF documents. It also provides the ability to merge multiple PDF files into a single document.

IronPDF for Java is easy to use and has a simple and intuitive API that makes it easy for developers to create PDF files. It also supports various features like text and image editing, form filling and much more.

3. Prerequisites

Before we begin, there are a few prerequisites that must be met to carry out the PDF creation process.

  1. Java should be installed on your system and its path should be set in the environment variables. If you haven't installed Java yet, please refer to this link for instructions.
  2. A Java IDE such as Eclipse or IntelliJ should be installed. You can download Eclipse from this link and IntelliJ from this link.
  3. The IronPDF library for Java should be downloaded and added as a dependency in your project. You can visit the IronPDF website for instructions on how to do this.
  4. Maven should be installed and integrated with your IDE before starting with PDF conversion. For a tutorial on installing Maven and integrating it into your environment, please visit this link.

4. IronPDF for Java Installation

If all requirements are met, the installation of IronPDF for Java is quite simple and straightforward, even for Java novices.

For this article, we will use JetBrain's IntelliJ IDEA to install and run samples.

First, open JetBrains IntelliJ IDEA and create a new Maven project.

How to Merge Two PDF Files - Figure 1: New Maven Project in IntelliJ

Create an New IntelliJ Maven Project

A new window will appear. Enter the name of the project and click on finish.

How to Merge Two PDF Files - Figure 2: Name the Maven Project and click Finish

Give your new Maven Project a suitable name, and click Finish to complete the New Project Wizard.

After you click Finish, a new project will open to a pom.xml. We will use this to add Maven dependencies of IronPDF for Java.

How to Merge Two PDF Files - Figure 3: The pom.xml file

The pom.xml file

Add the following dependencies in the pom.xml file or you can download the JAR file from the following link.

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML

Once you placed the dependencies in the pom.xml file, a small icon will appear in the right top corner of the file.

How to Merge Two PDF Files - Figure 4: Click the floating icon to install the Maven dependencies automatically

Click on the Floating Maven Icon shown above to install the new Maven dependencies automatically.

Click on this icon to install the Maven dependencies of IronPDF for Java. This will only take a few minutes depending on your internet connection.

5. Merge Multiple PDF Documents

IronPDF allows you to merge multiple PDF documents into a single PDF document using a Java program. IronPDF provides several ways to merge PDF documents:

  1. Create two new PDF documents and merge them to create one PDF.
  2. Open input PDF files into a merged PDF.
  3. Merge more than two PDF documents.

5.1. Creating Two New PDF Documents and Merge Them Together

import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;

public class main {
    public static void main(String[] args) throws IOException {
        String htmlA = "<p> [PDF_1] </p>"
        + "<p> Hi this the the first PDF </p>";
        String htmlB = "<p> [PDF_2] </p>"
        + "<p> This the 2nd PDF </p>";

        PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
        PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA
How to Merge Two PDF Files - Figure 5: New PDF File Merger

The result of generating two new PDF documents from HTML and combining them together into a single PDF document. Obtain a license key to remove the watermarks.

5.2. Combine Existing Files into One PDF

IronPDF allows you to merge existing PDF files into one common PDF file. Just specify the list of PDF input files. IronPDF will merge all PDF files into a single PDF document and save it to the destination file. The output will contain the result of the successfully merged PDF files.

import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;

public class main {
    public static void main(String[] args) throws IOException {
        PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
        PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA
How to Merge Two PDF Files - Figure 6: Existing PDF Merger Output

The result of merging two existing PDF documents using the IronPDF for Java library.

5.3. Merging More Than Two PDF Documents

You can easily merge more than two PDF files using IronPDF for Java.

import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;

public class main {
 public static void main(String[] args) throws IOException {
    List<PdfDocument> pdfList = new ArrayList<>();
    pdfList.add(PdfDocument.fromFile(Paths.get("assets/1.pdf")));
    pdfList.add(PdfDocument.fromFile(Paths.get("assets/2.pdf"));
    pdfList.add(PdfDocument.fromFile(Paths.get("assets/3.pdf"));
    PdfDocument merged = PdfDocument.merge(pdfList);
    merged.saveAs(Paths.get("assets/merged.pdf"));
 }
}
JAVA

6. Conclusion

This article covers how to merge multiple PDF files using Java and the IronPDF library. By following the steps outlined in this article, you will be able to set up the environment, import the library, read the input files, and merge them into a single document.

For more information about merging two or more PDF files in Java and for similar tutorials on how to create and format PDFs using Java, read the Documentation pages.

IronPDF for Java is free for development purposes but requires a license for commercial use.