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 fromthis 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.
importcom.spire.pdf.*;importjava.io.*;publicclassmergePDF{publicstaticvoidmain(String[]args)throwsException{StringoutputFile="output/mergeFilesByStream.pdf";FileInputStreamstream1=newFileInputStream(newFile("C:\\Users\\Administrator\\Desktop\\Sample1.pdf"));FileInputStreamstream2=newFileInputStream(newFile("C:\\Users\\Administrator\\Desktop\\Sample2.pdf"));FileInputStreamstream3=newFileInputStream(newFile("C:\\Users\\Administrator\\Desktop\\Sample3.pdf"));InputStream[]streams=newFileInputStream[]{stream1,stream2,stream3};//Merge files by streamPdfDocumentBasedoc=PdfDocument.mergeFiles(streams);//Save the filedoc.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.
importcom.spire.pdf.*;importjava.io.IOException;publicclassmergePDF{publicstaticvoidmain(String[]args)throwsIOException{//Pdf document listString[]files=newString[]{"Sample1.pdf","Sample2.pdf","Sample3.pdf",};StringoutputFile="output/MergeDocument.pdf";//Open pdf documentsPdfDocument[]docs=newPdfDocument[files.length];PdfDocumentdoc=newPdfDocument();for(inti=0;i<files.length;i++){docs[i]=newPdfDocument();docs[i].loadFromFile(files[i]);}//Append documentdocs[0].appendPage(docs[1]);//import pagesfor(inti=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.
importcom.spire.pdf.*;importjava.io.IOException;publicclasssplitPDF{publicstaticvoidmain(String[]args)throwsIOException{PdfDocumentdoc=newPdfDocument();doc.loadFromFile("MergeDocument.pdf");//Split every page of the PDF into a separate filedoc.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.
importcom.spire.pdf.*;importjava.io.IOException;importcom.spire.pdf.graphics.PdfMargins;importjava.awt.geom.Point2D;publicclasssplitPDF{publicstaticvoidmain(String[]args)throwsIOException{//Load the PDF filePdfDocumentdoc=newPdfDocument();doc.loadFromFile("MergeDocument.pdf");//Create a new PDF filePdfDocumentnewDoc1=newPdfDocument();PdfPageBasepage;//Add 2 pages to the new PDF, and draw the content of page 1-2 of the original PDF to the new added pagesfor(inti=0;i<2;i++){page=newDoc1.getPages().add(doc.getPages().get(i).getSize(),newPdfMargins(0));doc.getPages().get(i).createTemplate().draw(page,newPoint2D.Float(0,0));}//Save the filenewDoc1.saveToFile("Output/Doc1.pdf");//Create another PDF filePdfDocumentnewDoc2=newPdfDocument();//Add 4 pages to the new PDF, and draw the content of page 3-6 of the original PDF to the new added pagesfor(inti=2;i<6;i++){page=newDoc2.getPages().add(doc.getPages().get(i).getSize(),newPdfMargins(0));doc.getPages().get(i).createTemplate().draw(page,newPoint2D.Float(0,0));}//Save the document to filenewDoc2.saveToFile("Output/Doc2.pdf");}}
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse