IronPDF How-Tos Java Merge PDFs Java Merge PDF Files into a Single PDF Darrius Serrant Updated:June 22, 2025 PDF is a Portable Document Format, which is an electronic form of a document containing text and graphics. It is an independent format that displays the same content and layout across all operating systems, devices, or software applications. Java is a high-level programming language which, 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 a challenging task 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. How to Merge PDF Files in Java Install Java library to perform merge on PDF files Utilize renderHtmlAsPdf method to generate multiple PDFs Merge PDFs with merge method in Java Use saveAs method to save the merged PDF document Merge more than two PDFs by creating a list of PDF objects and use merge method IronPDF: Java Library IronPDF is a Java library for creating, reading, and editing single or multiple PDF documents. It allows its 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 the 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. Prerequisites To merge multiple PDF files, you will need the following: Any Java-supported IDE (Netbeans, Eclipse, IntelliJ, etc). We will be using IntelliJ here to merge multiple PDFs. A Maven project running in the IDE. Install IronPDF 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. 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. Another way is to visit the Maven website and download the latest version of IronPDF. You can download it from here directly. 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. <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 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 Merge Two PDF Source 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. 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. Save Merged Multiple 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: Merge Multiple PDF Documents Merge More Than Two PDF Files 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. More Than Two Merged PDF Files Conclusion 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 very 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. You can get more information about how to use IronPDF for Java from our Code Examples pages. 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 can I merge multiple PDF files into a single document using Java? You can merge multiple PDF files in Java by using the IronPDF library. It allows you to combine PDFs by creating a list of PdfDocument objects and using the merge method to consolidate them into a single file. What steps are needed to install IronPDF for PDF manipulation in Java? To install IronPDF in a Java project, you need to set up a Maven project. Add the IronPDF dependency to your pom.xml file, which allows you to download the library from the central repository using Maven. How can I ensure that PDF formatting is preserved when merging documents in Java? IronPDF maintains the original formatting of PDFs during the merge process. Its advanced rendering engine ensures that the merged PDF retains all fonts, layouts, and styles of the original documents. What platforms are supported for running IronPDF in Java? IronPDF supports cross-platform operations, allowing you to run it on Windows, Linux, and Cloud platforms. It is compatible with Java 8+, Kotlin, and Scala. How do you save a PDF file after merging documents with IronPDF? After merging documents with IronPDF, you can save the resulting PDF file using the saveAs method. This method allows you to specify the destination path for the merged document. Can IronPDF handle the conversion of HTML content to PDF in Java? Yes, IronPDF can convert HTML content to PDF using the renderHtmlAsPdf method. This feature enables the conversion of HTML strings into high-quality PDF documents. Is IronPDF a suitable choice for Java developers seeking to merge PDFs without additional libraries? IronPDF is an excellent choice for Java developers as it does not require any third-party libraries. It is a standalone library that provides robust PDF creation and manipulation capabilities. What are the advantages of using IronPDF for PDF creation and merging in Java? IronPDF offers several advantages, including high-speed processing, accurate rendering, and the ability to convert HTML, CSS, and images into PDFs. It is also free for development use, with a license required for commercial deployment. Darrius Serrant Chat with engineering team now 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? Free Maven Download View Licenses