Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Java merge and split PDF files

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

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();}}
Enter fullscreen modeExit fullscreen mode

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();}}
Enter fullscreen modeExit fullscreen mode

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();}}
Enter fullscreen modeExit fullscreen mode

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp