How to merge multiple PDF files into one PDF in Java

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

Merge multiple PDFs in a file into a single PDF
Merge multiple PDFs in a stream into a single PDF
Part 1: Get to know the Spire.PDF library

To merge PDF files in Java, we will use 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 extensive functionality for manipulating PDF files, including merging multiple PDF files into a single document.

Before we use Spire.PDF to merge PDF files, we need to add its dependencies to our Java project. We can achieve this by adding the following dependencies to the 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>

Part 2: Writing Java Code

Once we add the Spire.PDF for java dependency to our project, we can start writing Java code to merge multiple PDF files. Combining PDFs with Spire.PDF is easy. Here are the steps to merge two PDF documents and then merge them into one.

Load PDFs from files and merge them into new PDFs

Gets the path of the documents to be merged and stores it in a String array.
Merge selected PDF files using the PdfDocument.mergeFiles() method.
Use the PdfDocumentBase.save() method to save the PDF document.

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 PDFs from streams and merge them into a new PDF

Loading PDFs from streams and merging them into new PDFs is a convenient and efficient way to work with multiple PDF documents without saving them to disk. This method is particularly useful when working with large or sensitive files that you don't want to store on your local drive. Spire.PDF also supports loading PDFs from streams and then combining them into a new PDF file.

Get the paths to the PDF streams and store them into a FileInputStream array.
Merge selected PDF files using the PdfDocument.mergeFiles() method.
Use the PdfDocumentBase.save() method to save the PDF document.

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

    }
}

in conclusion

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

If you liked this article, please like, comment and share.

Guess you like

Origin blog.csdn.net/qq_52010446/article/details/131465735