How to Merge Multiple PDF files into a Single PDF in Java
Tags: java, pdf, combine, merge
Published: 2023-06-06 08:32:18

If you are working on a Java project that involves handling PDF files, you may need to merge multiple PDF files into a single document. In this article, we will demonstrate how to achieve this using Java programming language. We will show you how to combine multiple PDF files into a single PDF from the following two aspects:

- [Merge Multiple PDFs from file into a Single PDF](#load-the-pdfs-from-file-and-then-merge-them-to-a-new-pdf)
- [Merge Multiple PDFs from stream into a Single PDF](#load-the-pdfs-from-stream-and-then-merge-them-to-a-new-pdf)

**Part 1: Understanding the Spire.PDF Library**

To merge PDF files in Java, we will make use of the [Spire.PDF library](https://www.e-iceblue.com/Introduce/pdf-for-java.html). Spire.PDF for Java is a PDF API that enables Java applications to read, write and save PDF documents without using Adobe Acrobat. It provides a wide range of features for manipulating PDF files, including merging multiple PDF files into a single document.

Before we can use the Spire.PDF to merge PDF files, we need to add its dependency to our Java project. We can do this by adding the following dependency to our Maven project:

```java 
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>9.5.6</version>
    </dependency>
</dependencies>
```
**Part 2: Writing the Java Code**

Once we have added the Spire.PDF for java dependency to our project, we can start writing the Java code to merge multiple PDF files. Combining PDFs together with Spire.PDF is very simple. Below are steps to combine two PDF documents and then merge them into one.

###Load the PDFs from file and then merge them to a new PDF

- Get the paths of the documents to be merged and store them in a String array. 
- Merge the selected PDF files using **PdfDocument.mergeFiles()** method.
- Save the PDF document using **PdfDocumentBase.save()** method.

```java
import com.spire.pdf.*;

public class mergePDF {

    public static void main(String[] args) throws Exception {

        //Get the paths of the documents to be merged
        String[] files = new String[] {
                "D:\\sample.pdf",
                "D:\\sample1.pdf"};

        //Merge documents and return an object of PdfDocumentBase
        PdfDocumentBase pdf = PdfDocument.mergeFiles(files);

        //Save the result to a PDF file
        pdf.save("MergedPDF.pdf", FileFormat.PDF);

    }
}
```

###Load the PDFs from stream and then merge them to a new PDF
Loading PDFs from stream and merging them into a new PDF is a convenient and efficient way to handle multiple PDF documents without having to save them to disk. This approach can be particularly useful when dealing with large or sensitive files that you do not want to store on your local drive. Spire.PDF also supports to load the PDFs from stream and then combine them to a new PDF file.

- Get the paths of the PDF streams and then store them into a **FileInputStream** array.
- Merge the selected PDF files using **PdfDocument.mergeFiles()** method.
- Save the PDF document using **PdfDocumentBase.save()** method. 

```java
import com.spire.pdf.*;
import java.io.*;

public class mergePDFbyStream {

    public static void main(String[] args) throws Exception {

        FileInputStream stream1 = new FileInputStream(new File("sample.pdf"));
        FileInputStream stream2 = new FileInputStream(new File("sample1.pdf"));

        InputStream[] streams = new FileInputStream[]{stream1, stream2};

        //Merge these documents and return an object of PdfDocumentBase
        PdfDocumentBase pdf = PdfDocument.mergeFiles(streams);

        //Save the result to a PDF file
        pdf.save("MergedPDF.pdf", FileFormat.PDF);

    }
}
```

###Conclusion
That's it! You have successfully merged multiple PDF files into a single document using Java programming language and the Spire.PDF library. Overall, loading PDFs from file or stream and merging them into a new PDF can be a powerful technique for managing multiple PDF documents in a secure and efficient manner.

###Related topics:
[Split a PDF File into Multiple PDFs](https://www.e-iceblue.com/Tutorials/JAVA/Spire.PDF-for-JAVA/Program-Guide/Document-Operation/Split-PDF-Files-in-Java.html)
[Compress PDF Documents](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Document-Operation/Compress-PDF-document-in-Java.html)
[Duplicate a Page in PDF in Java](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Document-Operation/Duplicate-a-Page-in-PDF-in-Java.html)
[Convert PDF to PDF/A](https://www.e-iceblue.com/Tutorials/JAVA/Spire.PDF-for-JAVA/Program-Guide/Conversion/Convert-PDF-to-PDF/A-in-Java.html)
[Convert PDF to Excel](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Conversion/Convert-PDF-to-Excel-in-Java.html)