DEV Community 👩‍💻👨‍💻

Alexis
Alexis

Posted on

Java Combine or merge PDF files into a Single PDF

In our daily life, you may need to combine two or more PDF files into a single PDF file. Spire.PDF for Java 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.

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. If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.

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

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

    }
}
Enter fullscreen mode Exit fullscreen mode

merge PDF

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

    }
}
Enter fullscreen mode Exit fullscreen mode

merge pdf by streams

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.
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");
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

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 for more features to operate the PDF files.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.