Far from the original: http://illy.iteye.com/blog/856479
We sometimes need to merge multiple PDF files into one. GUI tools just don't say it,
This only discusses how to merge PDFs using Java programs. We just need to use the Itext.jar to get the job done.
ImportJava.io.FileOutputStream; Importjava.io.IOException; Importcom.lowagie.text.Document; Importcom.lowagie.text.DocumentException; Importcom.lowagie.text.pdf.PdfCopy; ImportCom.lowagie.text.pdf.PdfImportedPage; ImportCom.lowagie.text.pdf.PdfReader; Public classMergefile { Public Static voidMain (string[] args) {string[] files= {"E:\\1.pdf", "E:\\2.pdf", "E:\\3.pdf" }; String Savepath= "E:\\temp.pdf"; Mergepdffiles (Files, savepath); } /** * Close and PDF file * * @param files to fit and file number (absolute path such as {"E:\\1.pdf", "e:\\2.pdf", * "E:\\3.pdf"}) * @param new File * and the new files are definitely the path of the e:\\temp.pdf, please delete the files that are no longer used after use * @return Boolean to return true successfully, or false */ Public Static Booleanmergepdffiles (string[] files, String newfile) {BooleanRetValue =false; Document Document=NULL; Try{document=NewDocument (NewPdfreader (Files[0]). GetPageSize (1)); Pdfcopy Copy=NewPdfcopy (document,NewFileOutputStream (NewFile)); Document.open (); for(inti = 0; i < files.length; i++) {Pdfreader reader=NewPdfreader (Files[i]); intn =reader.getnumberofpages (); for(intj = 1; J <= N; J + +) {document.newpage (); Pdfimportedpage page=Copy.getimportedpage (Reader, J); Copy.addpage (page); }} RetValue=true; } Catch(Exception e) {e.printstacktrace (); } finally{document.close (); } returnRetValue; } }
Itextasian.jar Itext-1.3.jar
Java Merge Pdf,itext.jar