Split and Merge PDF Files in Java
Tags: java, spirepdf, splitpdf, mergepdf
Published: 2019-01-19 07:36:28

---
title: Split and Merge PDF Files in Java
published: true
description: This blog post will show you how to split a large PDF to smaller files and how to combine multiple PDFs into one by using Spire.PDF for Java.
tags: java,spire.pdf, splitpdf,mergepdf
cover_image: https://thepracticaldev.s3.amazonaws.com/i/w1catuc41xsp1nt2pubh.jpg
---

As PDF file format is getting increasingly popular in business these days, office workers are often exposed to PDF documents and may also have the requirements to process the PDFs as well. For example, split a PDF containing multiple invoices into separate files, or combine several reports into a single PDF. 

This article is going to introduce how we can split a large PDF and merge multiple PDFs by using Spire.PDF for Java.  There are three pieces of code in the sections below illustrating the split and merge from the following three aspects.

* Split PDF to multiple single-page PDFs
* Split PDF to several files by page ranges 
* Merge multiple PDFs into one document 

Before you can compile the code snippets below, [download Spire.PDF for Java](https://www.e-iceblue.com/Download/pdf-for-java.html) and import the Spire.Pdf.jar and Spire.Common.jar into your project.

##Split PDF to Single-Page PDFs

Spire offers a quite straightforward mothed – split, to divide a multi-page PDF document into multiple single page based PDF documents. Simply load the source PDF file to a PdfDocument object, and then call split method to do the division. 

```java
//specify input file path and output directory
String inputFile= "C:/Users/Administrator/Desktop/test.pdf";
String outputDirectory = "C:/Users/Administrator/Desktop/Output/";

//create a PdfDocument instance
PdfDocument doc = new PdfDocument();

//load a PDF file
doc.loadFromFile(inputFile);

//split PDF to single-page PDFs
doc.split(outputDirectory + "output-{0}.pdf",1);
```
##Split PDF by Page Ranges

If you’d like the PDF be split by the selected page numbers, for example, the first page becomes one document, the remaining pages become another, then you’ll need to create two new PDF documents and insert the selected page(s) from the source document to the new ones. The following code snippet demonstrates the same. 
```java
//specify input file path and output directory
String inputFile= "C:/Users/Administrator/Desktop/test.pdf";
String outputDirectory = "C:/Users/Administrator/Desktop/Output/";

//create a PdfDocument instance
PdfDocument sourceDoc = new PdfDocument();

//load a source PDF file
sourceDoc.loadFromFile(inputFile);

//create two extra PdfDocument objects
PdfDocument newDoc_1 = new PdfDocument();
PdfDocument newDoc_2 = new PdfDocument();

//insert the first page from source file to the new PDF document
newDoc_1.insertPage(sourceDoc, 0);

//insert the rest pages from source file to the second new PDF document
newDoc_2.insertPageRange(sourceDoc,1,sourceDoc.getPages().getCount()-1);

//save the new PDF documents
newDoc_1.saveToFile(outputDirectory + "output-1.pdf");
newDoc_2.saveToFile(outputDirectory + "output-2.pdf");
```
##Merge PDFs

To accomplish this task, Spire offers another easy-to-use method named mergeFiles. Just get and store the input file paths in a String array, then pass it to the mergeFiles method as one parameter, and then save the returned object as a new PDF file. 

```java
//get the paths of the files that you want to merge
String[] filePaths = new String[]{
        "C:/Users/Administrator/Desktop/Source/input-1.pdf",
        "C:/Users/Administrator/Desktop/Source/input-2.pdf",
        "C:/Users/Administrator/Desktop/Source/input-3.pdf"
};

//specify output directory
String outputDirectory = "C:/Users/Administrator/Desktop/Output/";

//merge PDF files using mergeFiles method
PdfDocumentBase newDoc = PdfDocument.mergeFiles(filePaths);

//save the new PDF document
newDoc.save(outputDirectory + "Merged.pdf");

```