Java merge pdf files and java merge pdf files

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.