Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
This article will demonstrate how to merge multiple PDF documents using IronPDF Library 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.
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 methods for Rendering Charts in PDFs, working with PDF Forms, and even handling Digital Signatures Programmatically.
Before implementing, there are a few prerequisites that must be met to carry out the PDF creation process.
If all requirements are met, the installation of IronPDF for Java is quite simple and straightforward, even for Java novices.
For this article, JetBrains' IntelliJ IDEA will be used to install and run samples.
First, open JetBrains IntelliJ IDEA and create a new Maven project.
New Maven Project in IntelliJ
A new window will appear. Enter the name of the project and click on finish.
Name the Maven Project and click Finish
After you click Finish, a new project will open to a pom.xml to add Maven dependencies of IronPDF for Java.
The pom.xml file
Add the following dependencies in the pom.xml
file or you can download the JAR file from the following IronPDF Listing on Maven Central.
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR_DESIRED_VERSION_HERE</version>
</dependency>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR_DESIRED_VERSION_HERE</version>
</dependency>
Once you placed the dependencies in the pom.xml
file, a small icon will appear in the right top corner of the file.
Click the floating icon to install the 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.
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:
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;
// This class demonstrates how to create and merge two PDF documents using the IronPDF library.
public class Main {
public static void main(String[] args) throws IOException {
// Define the HTML content for the first PDF document
String htmlA = "<p> [PDF_1] </p>"
+ "<p> Hi this is the first PDF </p>";
// Define the HTML content for the second PDF document
String htmlB = "<p> [PDF_2] </p>"
+ "<p> This is the 2nd PDF </p>";
// Render the HTML content to create two separate PDF documents
PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
// Merge the two PDF documents into one
PdfDocument merged = PdfDocument.merge(pdfA, pdfB);
// Save the merged PDF document to the specified path
merged.saveAs(Paths.get("assets/merged.pdf"));
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;
// This class demonstrates how to create and merge two PDF documents using the IronPDF library.
public class Main {
public static void main(String[] args) throws IOException {
// Define the HTML content for the first PDF document
String htmlA = "<p> [PDF_1] </p>"
+ "<p> Hi this is the first PDF </p>";
// Define the HTML content for the second PDF document
String htmlB = "<p> [PDF_2] </p>"
+ "<p> This is the 2nd PDF </p>";
// Render the HTML content to create two separate PDF documents
PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
// Merge the two PDF documents into one
PdfDocument merged = PdfDocument.merge(pdfA, pdfB);
// Save the merged PDF document to the specified path
merged.saveAs(Paths.get("assets/merged.pdf"));
}
}
New PDF File Merger
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;
// This class demonstrates how to merge existing PDF files using the IronPDF library.
public class Main {
public static void main(String[] args) throws IOException {
// Load the existing PDF files from the specified paths
PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
// Merge the two PDF documents into one
PdfDocument merged = PdfDocument.merge(pdfA, pdfB);
// Save the merged PDF document to the specified path
merged.saveAs(Paths.get("assets/merged.pdf"));
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;
// This class demonstrates how to merge existing PDF files using the IronPDF library.
public class Main {
public static void main(String[] args) throws IOException {
// Load the existing PDF files from the specified paths
PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
// Merge the two PDF documents into one
PdfDocument merged = PdfDocument.merge(pdfA, pdfB);
// Save the merged PDF document to the specified path
merged.saveAs(Paths.get("assets/merged.pdf"));
}
}
Existing PDF Merger Output
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;
import java.util.ArrayList;
import java.util.List;
// This class demonstrates how to merge more than two PDF documents using the IronPDF library.
public class Main {
public static void main(String[] args) throws IOException {
// Create a list to hold the PDF documents
List<PdfDocument> pdfList = new ArrayList<>();
// Add existing PDF files to the list
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")));
// Merge all PDF documents in the list into one
PdfDocument merged = PdfDocument.merge(pdfList);
// Save the merged PDF document to the specified path
merged.saveAs(Paths.get("assets/merged.pdf"));
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
// This class demonstrates how to merge more than two PDF documents using the IronPDF library.
public class Main {
public static void main(String[] args) throws IOException {
// Create a list to hold the PDF documents
List<PdfDocument> pdfList = new ArrayList<>();
// Add existing PDF files to the list
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")));
// Merge all PDF documents in the list into one
PdfDocument merged = PdfDocument.merge(pdfList);
// Save the merged PDF document to the specified path
merged.saveAs(Paths.get("assets/merged.pdf"));
}
}
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 PDF Files in Java Using IronPDF and for similar tutorials on how to Create PDFs from HTML and Format PDFs with IronPDF, Explore Our Comprehensive Documentation.
IronPDF for Java is free for development purposes but Requires a Commercial License for Use in Production Environments.
IronPDF for Java is a powerful library that enables developers to create new PDF documents, convert various file formats to PDF, and merge multiple PDF files into a single document. It offers a simple and intuitive API for ease of use.
To install IronPDF for Java, ensure Java and a Java IDE like IntelliJ are installed. Then, download IronPDF and add it as a dependency in your Maven project. You can find the necessary dependencies on the IronPDF official website or Maven Central.
The prerequisites include installing Java and setting its path in the environment variables, installing a Java IDE like Eclipse or IntelliJ, downloading the IronPDF library, and integrating Maven with your IDE.
You can merge two PDF files by using the PdfDocument class to render or load the two PDF documents and then merge them using the merge method. Finally, save the merged PDF document to the desired path.
Yes, IronPDF for Java can merge more than two PDF documents by adding them to a list and using the merge method to combine them into a single document.
IronPDF for Java is free for development purposes, but a commercial license is required for use in production environments.
To create a new PDF document, use the PdfDocument class to render HTML content into a PDF. This API allows you to define the content and save it as a PDF file.
IronPDF for Java is supported by popular Java IDEs such as IntelliJ IDEA and Eclipse, which facilitate easy integration and development.
More tutorials and comprehensive documentation on using IronPDF for Java can be found on the IronPDF official website, covering topics like merging PDFs, creating PDFs from HTML, and formatting PDFs.