This article shows how to use Spire.PDF for Java to programmatically merge many single PDF files into a whole PDF and split a PDF file into separate PDF files in Java applications.
Installing Spire.pdf.jar
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and manually add it as a dependency in your application.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.pdf</artifactId>
<version>4.4.5</version>
</dependency>
</dependencies>
Merge PDF documents by stream. Input the three PDF documents by stream and then use PdfDocument.mergeFiles(streams)methods to merge the PDF documents into one PDF document.
import com.spire.pdf.*;
import java.io.*;
public class mergePDF {
public static void main(String[] args) throws Exception {
String outputFile = "output/mergeFilesByStream.pdf";
FileInputStream stream1 = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\Sample1.pdf"));
FileInputStream stream2 = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\Sample2.pdf"));
FileInputStream stream3 = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\Sample3.pdf"));
InputStream[] streams = new FileInputStream[]{stream1, stream2, stream3};
//Merge files by stream
PdfDocumentBase doc = PdfDocument.mergeFiles(streams);
//Save the file
doc.save(outputFile);
doc.close();
}
}
Spire.PDF also supports to load the PDF documents from file and select the first PdfDocument for the purpose of merging the second and third PDF file to it.
import com.spire.pdf.*;
import java.io.IOException;
public class mergePDF {
public static void main(String[] args) throws IOException {
//Pdf document list
String[] files = new String[]
{
"Sample1.pdf",
"Sample2.pdf",
"Sample3.pdf",
};
String outputFile = "output/MergeDocument.pdf";
//Open pdf documents
PdfDocument[] docs = new PdfDocument[files.length];
PdfDocument doc = new PdfDocument();
for (int i = 0; i < files.length; i++) {
docs[i] = new PdfDocument();
docs[i].loadFromFile(files[i]);
}
//Append document
docs[0].appendPage(docs[1]);
//import pages
for (int i = 0; i < docs[2].getPages().getCount(); i = i + 1) {
docs[0].insertPage(docs[2], i);
}
//Save pdf file.
docs[0].saveToFile(outputFile);
doc.close();
}
}
Split every page of the PDF into a separate file by PdfDocument.split() method offered by Spire.PDF for Java.
import com.spire.pdf.*;
import java.io.IOException;
public class splitPDF {
public static void main(String[] args) throws IOException {
PdfDocument doc = new PdfDocument();
doc.loadFromFile("MergeDocument.pdf");
//Split every page of the PDF into a separate file
doc.split("output/splitDocument-{0}.pdf", 0);
doc.close();
}
}
Split the PDF into multiple files by a range of pages. We will split a single PDF file into two PDFs. One with 2 pages and the other with 4 pages.
import com.spire.pdf.*;
import java.io.IOException;
import com.spire.pdf.graphics.PdfMargins;
import java.awt.geom.Point2D;
public class splitPDF {
public static void main(String[] args) throws IOException {
//Load the PDF file
PdfDocument doc = new PdfDocument();
doc.loadFromFile("MergeDocument.pdf");
//Create a new PDF file
PdfDocument newDoc1 = new PdfDocument();
PdfPageBase page;
//Add 2 pages to the new PDF, and draw the content of page 1-2 of the original PDF to the new added pages
for(int i = 0;i<2;i++)
{
page = newDoc1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
}
//Save the file
newDoc1.saveToFile("Output/Doc1.pdf");
//Create another PDF file
PdfDocument newDoc2 = new PdfDocument();
//Add 4 pages to the new PDF, and draw the content of page 3-6 of the original PDF to the new added pages
for(int i = 2;i<6;i++)
{
page = newDoc2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
}
//Save the document to file
newDoc2.saveToFile("Output/Doc2.pdf");
}
}
Top comments (0)