Java merge pdf files and java merge pdf files
Using java code to merge PDF files requires importing iText-2.1.7.jar packages
1 import java. io. fileOutputStream; 2 import java. io. IOException; 3 import com. lowagie. text. document; 4 import com. lowagie. text. extends entexception; 5 import com.lowagie.text=. export copy; 6 import com.lowagie.text=. pdfImportedPage; 7 import com.lowagie.text.pdf. pdfReader; 8 9 public class MergeFile {10 public static void main (String [] args) {11 String [] files = {"e :\\ 1.pdf"," e: \ 2.20.", "e: \ 3.20."}; 12 String savepath =" e: \ temp1_"; 13 mergePdfFiles (files, savepath ); 14}/* 15 ** combined PDF file ** @ param files to be combined with the file data group (for example, {"e: \ 1.20."," e: \ 2.20.", 16 * "e :\\ 3.20."}) * @ param newfile 17 * links the newly generated file to a path such as e :\\ temp, please delete the files that are no longer in use by yourself. * @ return boolean 18 * returns true if the production succeeds, otherwise, false 19 */20 21 public static boolean mergePdfFiles (String [] files, String newfile) {22 boolean retValue = false; 23 Document document = null; 24 try {25 document = new Document (new PdfReader (files [0]). getPageSize (1); 26 bytes copy = new bytes copy (document, new FileOutputStream (newfile); 27 document. open (); 28 for (int I = 0; I <files. length; I ++) {29 PdfReader reader = new PdfReader (files [I]); 30 int n = reader. getNumberOfPages (); 31 for (int j = 1; j <= n; j ++) {32 document. newPage (); 33 PdfImportedPage = copy. getImportedPage (reader, j); 34 copy. addPage (page); 35} 36} 37 retValue = true; 38} catch (Exception e) {39 e. printStackTrace (); 40} finally {41 document. close (); 42} 43 return retValue; 44} 45}