How to Merge PDF Files in Java

Java Merge PDF Files into a Single PDF

IronPDF for Java enables merging of multiple PDF documents into a single file using the PdfDocument.merge() method, supporting both two-document merging and batch operations with PDF collections.

Quickstart: Merge PDF Files in Java

  1. Add IronPDF dependency to your pom.xml:

    <dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
    </dependency>
    <dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
    </dependency>
    XML
  2. Create PDF documents from HTML:

    PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
    PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
    PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
    PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
    JAVA
  3. Merge PDFs and save: ```java :title=MergeExample.java PdfDocument merged = PdfDocument.merge(pdfA, pdfB); merged.saveAs(Paths.get("assets/merged.pdf"));

PDF is a Portable Document Format, an electronic form of a document containing text and graphics. It displays the same content and layout across all operating systems, devices, or software applications.

Java is a high-level programming language that, like PDF data, is also platform-independent. This makes it easy to move between different computer systems. However, working with source PDF files and input streams can be challenging in Java. IronPDF, a Java library, is well suited to manipulate and work with existing PDF files easily.

In this how-to guide, you'll learn how to install the IronPDF Java library and merge multiple PDF documents. Whether you're consolidating reports, combining scanned documents, or assembling multi-part forms, IronPDF provides a straightforward API that handles the complexities of PDF manipulation internally.

What Is IronPDF and Why Use It for PDF Merging?

IronPDF is a Java library for creating, reading, and editing single or multiple PDF documents. It allows users to create all the PDF files from scratch, including the content appearance using HTML rendering, as well as metadata such as title and author name. The library also allows merging multiple PDF files, making it easy to combine content into one PDF destination file path. It does not require any third-party libraries, external frameworks, or platform integration for working with PDF files or PDF iterator objects. It also provides cross-platform support. It is designed specifically for Java 8+, Kotlin, and Scala running on Windows, Linux, and cloud platforms.

IronPDF handles PDF merging operations effectively because it maintains document integrity, preserves formatting, handles complex layouts, and processes large files efficiently. The library's merge functionality supports various scenarios including appending documents sequentially, inserting pages at specific positions, and combining PDFs with different page sizes or orientations.

What Are the Prerequisites for Merging PDFs in Java?

To merge multiple PDF files, you will need the following:

  1. Any Java-supported IDE (NetBeans, Eclipse, IntelliJ, etc.). We will be using IntelliJ here to merge multiple PDFs.
  2. A Maven project running in the IDE.
  3. Java Development Kit (JDK) version 8 or higher installed on your system.
  4. Basic understanding of Java programming and Maven dependency management.

How Do I Install IronPDF for PDF Merging?

The first thing we need to merge PDF files is the IronPDF Java library. There are three ways to download and install IronPDF in any project.

  1. You can add the IronPDF dependency in the pom.xml file of a Maven project and use the Maven command-line tool or an IDE to download the library directly from the central repository.
  2. Another way is to visit the Maven website and download the latest version of IronPDF. You can download it from here directly.
  3. You can also visit the IronPDF website to download directly through this link.

In each case, the following dependency code is added to the pom.xml file. For detailed installation instructions and license key implementation, visit the Using License Keys guide.

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
</dependency>
XML

One more dependency required to merge PDFs is Slf4j-simple. You can also add it to the pom.xml file using the following code or you can visit the Maven website here.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.5</version>
</dependency>
XML
Maven pom.xml showing IronPDF and SLF4J dependencies for PDF merging project setup

pom.xml dependencies

The following import statements are also required in the main.java file to use IronPDF functions for merging PDF files.

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

How Do I Merge Two PDF Files in Java Using IronPDF?

To merge PDF files, first we need to create PDF files and then convert them to a final merged PDF file. The following code sample will do just that:

String htmlA = "<p> [PDF_A] </p>"
        + "<p> [PDF_A] 1st Page </p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> [PDF_A] 2nd Page</p>";
String htmlB = "<p> [PDF_B] </p>"
        + "<p> [PDF_B] 1st Page </p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> [PDF_B] 2nd Page</p>";

// Create PdfDocument objects using the HTML content
PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);

// Merge the two PDF documents into a single document
PdfDocument merged = PdfDocument.merge(pdfA, pdfB);
String htmlA = "<p> [PDF_A] </p>"
        + "<p> [PDF_A] 1st Page </p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> [PDF_A] 2nd Page</p>";
String htmlB = "<p> [PDF_B] </p>"
        + "<p> [PDF_B] 1st Page </p>"
        + "<div style='page-break-after: always;'></div>"
        + "<p> [PDF_B] 2nd Page</p>";

// Create PdfDocument objects using the HTML content
PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);

// Merge the two PDF documents into a single document
PdfDocument merged = PdfDocument.merge(pdfA, pdfB);
JAVA

The above code creates two strings containing HTML markup. The HTML content for each variable spans two pages. Then, IronPDF's renderHtmlAsPdf method is called to convert both HTML strings into individual PDF documents. For more advanced HTML to PDF conversion options, including custom fonts and styling, see the HTML to PDF tutorial.

The method to merge PDF files together is PdfDocument.merge. This method is called above to merge the two PDF documents into a single one. The result is a new PdfDocument produced by appending the content of the second PdfDocument to the end of the first one.

How Do I Save the Merged PDF Document?

To save the merged PDF file to your desired destination file path, simply use the following line:

merged.saveAs(Paths.get("assets/merged.pdf"));
merged.saveAs(Paths.get("assets/merged.pdf"));
JAVA

The output of the merged PDF file is shown below:

PDF viewer showing merged 4-page document with thumbnail navigation panel

Merge Multiple PDF Documents

How Can I Merge More Than Two PDF Files at Once?

To merge more than two PDF documents, first we will create a collection containing the required PdfDocument objects, and then pass the list as a single argument to the PdfDocument.merge method. The code goes as follows:

import java.util.List;
import java.util.ArrayList;

public static void main(String[] args) throws IOException {
    String htmlA = "<p> [PDF_A] </p>"
            + "<p> [PDF_A] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_A] 2nd Page</p>";
    String htmlB = "<p> [PDF_B] </p>"
            + "<p> [PDF_B] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_B] 2nd Page</p>";
    String htmlC = "<p> [PDF_C] </p>"
            + "<p> [PDF_C] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_C] 2nd Page</p>";
    String htmlD = "<p> [PDF_D] </p>"
            + "<p> [PDF_D] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_D] 2nd Page</p>";

    // Creating PdfDocument objects
    PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
    PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
    PdfDocument pdfC = PdfDocument.renderHtmlAsPdf(htmlC);
    PdfDocument pdfD = PdfDocument.renderHtmlAsPdf(htmlD);

    // Add documents to a list
    List<PdfDocument> pdfs = new ArrayList<>();
    pdfs.add(pdfA);
    pdfs.add(pdfB);
    pdfs.add(pdfC);
    pdfs.add(pdfD);

    // Merge all documents into a single PDF
    PdfDocument merged = PdfDocument.merge(pdfs);

    // Save the merged PDF document
    merged.saveAs(Paths.get("assets/more_than_two_merged.pdf"));
}
import java.util.List;
import java.util.ArrayList;

public static void main(String[] args) throws IOException {
    String htmlA = "<p> [PDF_A] </p>"
            + "<p> [PDF_A] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_A] 2nd Page</p>";
    String htmlB = "<p> [PDF_B] </p>"
            + "<p> [PDF_B] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_B] 2nd Page</p>";
    String htmlC = "<p> [PDF_C] </p>"
            + "<p> [PDF_C] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_C] 2nd Page</p>";
    String htmlD = "<p> [PDF_D] </p>"
            + "<p> [PDF_D] 1st Page </p>"
            + "<div style='page-break-after: always;'></div>"
            + "<p> [PDF_D] 2nd Page</p>";

    // Creating PdfDocument objects
    PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
    PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
    PdfDocument pdfC = PdfDocument.renderHtmlAsPdf(htmlC);
    PdfDocument pdfD = PdfDocument.renderHtmlAsPdf(htmlD);

    // Add documents to a list
    List<PdfDocument> pdfs = new ArrayList<>();
    pdfs.add(pdfA);
    pdfs.add(pdfB);
    pdfs.add(pdfC);
    pdfs.add(pdfD);

    // Merge all documents into a single PDF
    PdfDocument merged = PdfDocument.merge(pdfs);

    // Save the merged PDF document
    merged.saveAs(Paths.get("assets/more_than_two_merged.pdf"));
}
JAVA

Four PDF documents are created above using the HTML render method. Next, we populate a new List collection with each of the PDFs, and then pass this list to the merge method as a single argument. As a result, these PDFs are merged into a single PDF document.

For more complex merging scenarios, such as merging specific pages or ranges from different PDFs, you can combine the merge functionality with page manipulation features. Learn more about page-level operations in the Split PDFs guide.

PDF viewer showing merged document with 8 pages, demonstrating successful multi-file PDF merge using Java IronPDF

More Than Two Merged PDF Files

Advanced Merging Options

Beyond basic merging, IronPDF offers additional capabilities for advanced PDF manipulation:

  1. Selective Page Merging: Extract specific pages from source PDFs before merging
  2. Insert at Position: Add PDFs at specific positions within an existing document
  3. Preserve Metadata: Maintain document properties and metadata during merge operations
  4. Handle Large Files: Efficiently process large PDF files without memory issues

Here's an example of selective page merging:

// Load existing PDFs
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("source.pdf"));
PdfDocument newPages = PdfDocument.fromFile(Paths.get("additional.pdf"));

// Extract specific pages (pages 2-4 from the new PDF)
PdfDocument selectedPages = newPages.extractPages(1, 3); // 0-indexed

// Merge the selected pages with the existing PDF
PdfDocument finalPdf = PdfDocument.merge(existingPdf, selectedPages);
finalPdf.saveAs(Paths.get("merged_selective.pdf"));
// Load existing PDFs
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("source.pdf"));
PdfDocument newPages = PdfDocument.fromFile(Paths.get("additional.pdf"));

// Extract specific pages (pages 2-4 from the new PDF)
PdfDocument selectedPages = newPages.extractPages(1, 3); // 0-indexed

// Merge the selected pages with the existing PDF
PdfDocument finalPdf = PdfDocument.merge(existingPdf, selectedPages);
finalPdf.saveAs(Paths.get("merged_selective.pdf"));
JAVA

What Are the Key Takeaways for Merging PDFs with IronPDF?

This article explained how to merge PDF files together using IronPDF for Java.

We first covered how to install IronPDF for Java using Maven, and then we showed a simple way to produce PDFs using the HTML rendering methods. Afterward, we saw how to merge two or more PDFs into a single PDF file.

IronPDF performs well and carries out all operations with speed and accuracy. It is an excellent option for working with PDF files in Java. Moreover, it is based on IronPDF for .NET capabilities.

The IronEngine for Java allows HTML/URL/String to PDF conversion with open standard document types such as HTML, CSS, JS, JPG, and PNG. It produces pixel-perfect PDF documents and is built from the latest technology. For deployment to cloud platforms, refer to our guides for AWS deployment, Azure deployment, or Google Cloud deployment.

You can get more information about how to use IronPDF for Java from our Code Examples pages. For additional PDF manipulation features, explore our tutorials on compressing PDFs, adding watermarks, or creating PDF forms.

IronPDF is free for development and can be licensed for commercial use. For more information about the license, visit the following link.

Frequently Asked Questions

How do I merge two PDF files in Java?

With IronPDF, you can merge two PDF files using the PdfDocument.merge() method. Simply create or load your PDF documents, then call PdfDocument.merge(pdfA, pdfB) and save the result using the saveAs() method.

What Java version is required for PDF merging?

IronPDF is designed specifically for Java 8 and higher, and also supports Kotlin and Scala. It works across Windows, Linux, and cloud platforms without requiring third-party libraries or external frameworks.

Can I merge more than two PDFs at once?

Yes, IronPDF supports batch operations for merging multiple PDFs. You can create a list of PDF objects and use the merge method to combine them all into a single document in one operation.

How do I add the PDF merge library to my Maven project?

Add IronPDF as a dependency in your pom.xml file by including the com.ironsoftware groupId with the ironpdf artifactId. This will give you access to all PDF manipulation features including merging.

Does merging PDFs preserve the original formatting?

Yes, IronPDF maintains document integrity when merging PDFs. It preserves formatting, handles complex layouts, and processes large files efficiently while keeping the original appearance of each document.

Can I create PDFs from HTML before merging them?

Absolutely! IronPDF allows you to create PDFs from HTML using the renderHtmlAsPdf() method. You can generate multiple PDFs from HTML content and then merge them together using the same merge functionality.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...

Read More
Ready to Get Started?
Version: 2025.12 just released