How Java merges and splits PDF documents
This article mainly introduces Java how to merge, split PDF documents, the article is very detailed, has a certain reference value, interested friends must read!
Tools: Free Spire.PDF for Java (Free Edition)
Jar file acquisition and import:
Method 1: Download the package through the official website. After downloading, extract the file and import the Spire.Pdf.jar file under lib folder into java program.
Method 2: Import to maven project via maven repository installation.
Java code examples
I. Merger
1. Merge PDF documents
import com.spire.pdf.*; import java.io.*; public class Merge2 { public static void main(String[] args) throws Exception { String outputFile = "output/mergeFilesByStream.pdf"; FileInputStream stream1 = new FileInputStream(new File("sample1.pdf")); FileInputStream stream2 = new FileInputStream(new File("sample2.pdf")); FileInputStream stream3 = new FileInputStream(new File("sample3.pdf")); //Load PDF sample documents InputStream[] streams = new FileInputStream[]{stream1, stream2, stream3}; //Merge PDF documents PdfDocumentBase doc = PdfDocument.mergeFiles(streams); //Save document doc.save(outputFile); doc.close(); }}
Before consolidation:
After consolidation:
2. Merge PDF pages
import com.spire.pdf.*; import com.spire.pdf.graphics.PdfBlendMode; import com.spire.pdf.graphics.PdfTemplate; import java.awt.geom.Point2D; public class MergePages { public static void main(String[] args) { //Load two PDF documents PdfDocument pdf1 = new PdfDocument(); pdf1.loadFromFile("test1.pdf"); PdfDocument pdf2 = new PdfDocument(); pdf2.loadFromFile("test2.pdf"); //Draw template according to the first page of pdf1 PdfTemplate template = pdf1.getPages().get(0).createTemplate(); //Get the second page of pdf2 PdfPageBase page = pdf2.getPages().get(1); //Set transparency and overlap styles page.getCanvas().setTransparency(1f,1f, PdfBlendMode.Normal); //Write the contents of the first page of pdf1 to the specified position on the second page of pdf2 page.getCanvas().drawTemplate(template,new Point2D.Float(0,350)); //Save pdf2 pdf2.saveToFile("MergePages.pdf",FileFormat.PDF); pdf2.dispose(); } }
Before consolidation:
After consolidation:
II. Split
The test documentation is as follows:
1. Split by page
import com.spire.pdf.*; public class SplitPDF1 { public static void main(String[] args) { //Load PDF document to be split PdfDocument doc = new PdfDocument(); doc.loadFromFile("test.pdf"); //Call split() to split PDF documents into separate documents per page doc.split("output/splitDocument-{0}.pdf", 0); doc.close(); }}
Split result:
2. Split by specified page range
import com.spire.pdf.*; import com.spire.pdf.graphics.PdfMargins; import java.awt.geom.Point2D; public class SplitPDF2 { public static void main(String[] args) { //Load PDF document to be split PdfDocument doc = new PdfDocument(); doc.loadFromFile("test.pdf"); //Create the first PDF document 1 PdfDocument newpdf1 = new PdfDocument(); PdfPageBase page; //Split pages 1 and 2 of the original PDF document and save it to newpdf1 for(int i = 0;i