DEV Community

AnnaGraz
AnnaGraz

Posted on

How to Merge Multiple PDF files into a Single PDF in Java

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:

Part 1: Understanding the Spire.PDF Library

To merge PDF files in Java, we will make use of the Spire.PDF library. 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:

<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>
Enter fullscreen mode Exit fullscreen mode

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.
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);

    }
}
Enter fullscreen mode Exit fullscreen mode

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.
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);

    }
}
Enter fullscreen mode Exit fullscreen mode

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
Compress PDF Documents
Duplicate a Page in PDF in Java
Convert PDF to PDF/A
Convert PDF to Excel

Top comments (0)