Java Merge PDF Files into a Single PDF
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>
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>
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;
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);
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"));
The output of the merged PDF file is shown below:
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"));
}
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.
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
What is IronPDF for Java?
IronPDF is a Java library that allows for creating, reading, and editing PDF documents. It supports merging multiple PDFs, HTML rendering, and does not require third-party libraries.
How do I install IronPDF in a Java project?
You can install IronPDF by adding its dependency in the 'pom.xml' file of a Maven project and use Maven to download it from the central repository.
What are the prerequisites for merging PDFs using IronPDF in Java?
To merge PDFs, you need a Java-supported IDE like IntelliJ, Netbeans, or Eclipse, and a Maven project setup.
Can IronPDF merge more than two PDF files?
Yes, IronPDF can merge more than two PDFs by creating a list of PdfDocument objects and using the merge method.
How do you save a merged PDF file using IronPDF?
You can save a merged PDF file using the 'saveAs' method, specifying the destination path for the merged document.
What Java versions does IronPDF support?
IronPDF is designed for Java 8+, Kotlin, and Scala, running on Windows, Linux, and Cloud platforms.
What methods are used to create PDFs in IronPDF?
PDFs can be created using the 'renderHtmlAsPdf' method which converts HTML content into PDF documents.
Is IronPDF free for development use?
Yes, IronPDF is free for development use, but a license is required for commercial use.
Do I need additional libraries to use IronPDF?
No additional libraries are required as IronPDF does not depend on third-party libraries for its operations.
Can IronPDF convert HTML, CSS, and images to PDF?
Yes, IronPDF can convert HTML, CSS, JavaScript, JPG, and PNG into pixel-perfect PDF documents.