Java Combine or merge PDF files into a Single PDF
Tags: java, pdf, merge, combine
Published: 2022-07-27 08:57:00

In our daily life, you may need to combine two or more PDF files into a single PDF file. [Spire.PDF for Java](https://www.e-iceblue.com/Introduce/pdf-for-java.html) offers **PdfDocument.mergeFiles()** method to merge the PDF files easily. This article will demonstrate how to merge multiple PDF files in Java from the following three parts.

- [Merge Multiple PDFs from file and then merging them into a Single PDF](#merge-multiple-pdfs-into-a-single-pdf)
- [Merge the PDFs stream as input and merging them to a new PDF](#merge-the-pdfs-by-stream-and-combine-them-to-a-new-pdf)
- [Merge the Selected Pages from the Different PDFs and merging them into One PDF](#merge-the-selected-pages-from-the-different-pdfs-and-merging-them-into-one-pdf) 

###Install Spire.PDF for Java

First of all, you need to add the Spire.PDF.jar file as a dependency in your Java program. The JAR file can be downloaded from [this link](https://www.e-iceblue.com/Download/pdf-for-java.html). If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.

```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>8.7.0</version>
    </dependency>
</dependencies>
```
###Merge Multiple PDFs into a Single PDF

Combining PDFs together with Spire.PDF is very simple. Below are steps to combine three PDF documents into one.

- 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[] {
                "E:\\OLE\\sample.pdf",
                "E:\\OLE\\sample1.pdf",
                "E:\\OLE\\sample2.pdf"};

        //Merge these 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);

    }
}
``` 

![merge PDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qlqnul4wnfxarc9x7z7l.png)
###Merge the PDFs by stream and combine them to a new PDF

Spire.PDF also supports to load the PDF streams and then combine them to a new PDF file.
- Get the paths of the PDF streams and then store them into a InputStream 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("MergedPDF2.pdf", FileFormat.PDF);

    }
}
```

![merge pdf by streams](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48h41i8upzx6vzjfk46o.png)
###Merge the Selected Pages from the Different PDFs and merging them into One PDF

Spire.PDF for Java offers the **PdfDocument.insertPage()** method and the **PdfDocument.insertPageRange()** method to import a page or a page range from one PDF document to another. Here are the steps to combine the PDF pages from different PDF files into a new PDF.
- Get the paths of the documents to be merged and store them in a String array. 
- Create a **PdfDocument** object and load the source document.
- Create another **PdfDocument** object for generating a new document.
- Insert the selected page or page range of the source documents to the new document using **PdfDocument.insertPage()** method and **PdfDocument.insertPageRange()** method.
- Save the result file using **PdfDocument.saveToFile()** method.

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

public class mergePDF {

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

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

        //Create an array of PdfDocument
        PdfDocument[] pdf = new PdfDocument[files.length];

        //Loop through the documents
        for (int i = 0; i < files.length; i++)
        {
            //Load a specific document
            pdf[i] = new PdfDocument(files[i]);
        }

        //Create a PdfDocument object for generating a new PDF document
        PdfDocument doc = new PdfDocument();

        //Insert the selected pages from different documents to the new document
        doc.insertPage(pdf[0], 0);
        doc.insertPage(pdf[1],0);
        doc.insertPageRange(pdf[2], 0,1);

        //Save the document to a PDF file
        doc.saveToFile("MergedPDF3.pdf");
    }
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7uvfodib8n8n5ns6sj7.png)
###Conclusion

In this article, we have demonstrated how to combine multiple PDF files into a single PDF using Java. With Spire.PDF for Java, we could also split a single PDF file into many PDF files by pages. You can check the [PDF forum](https://www.e-iceblue.com/forum/spire-pdf-f7.html) for more features to operate the PDF files.