處理PDF

  1. import  java.io.FileOutputStream;  
  2. import  java.io.IOException;  
  3. import  java.util.ArrayList;  
  4.   
  5. import  com.lowagie.text.Document;  
  6. import  com.lowagie.text.DocumentException;  
  7. import  com.lowagie.text.pdf.PdfCopy;  
  8. import  com.lowagie.text.pdf.PdfImportedPage;  
  9. import  com.lowagie.text.pdf.PdfReader;  
  10.   
  11.   
  12. public  class  pdfOperate   
  13. {  
  14.     private  static  final  int  N = 3 ;  
  15.       
  16.     public  static  void  main(String[] args)  
  17.     {  
  18.         String[] files = {"C://a.pdf""C://b.pdf" };  
  19.         String savepath = "C://temp.pdf" ;  
  20.         mergePdfFiles(files, savepath);  
  21.           
  22.         partitionPdfFile("C://a.pdf" );  
  23.     }  
  24.       
  25.     public  static  void  mergePdfFiles(String[] files, String savepath)  
  26.     {  
  27.         try    
  28.         {  
  29.             Document document = new  Document(new  PdfReader(files[0 ]).getPageSize(1 ));  
  30.               
  31.             PdfCopy copy = new  PdfCopy(document, new  FileOutputStream(savepath));  
  32.               
  33.             document.open();  
  34.               
  35.             for (int  i=0 ; i<files.length; i++)  
  36.             {  
  37.                 PdfReader reader = new  PdfReader(files[i]);  
  38.                   
  39.                 int  n = reader.getNumberOfPages();  
  40.   
  41.                 for (int  j=1 ; j<=n; j++)  
  42.                 {  
  43.                     document.newPage();   
  44.                     PdfImportedPage page = copy.getImportedPage(reader, j);  
  45.                     copy.addPage(page);  
  46.                 }  
  47.             }  
  48.               
  49.             document.close();  
  50.   
  51.         } catch  (IOException e) {  
  52.             e.printStackTrace();  
  53.         } catch (DocumentException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57.   
  58.     public  static  void  partitionPdfFile(String filepath)  
  59.     {  
  60.         Document document = null ;  
  61.         PdfCopy copy = null ;  
  62.           
  63.         try    
  64.         {  
  65.             PdfReader reader = new  PdfReader(filepath);  
  66.               
  67.             int  n = reader.getNumberOfPages();  
  68.               
  69.             if (n < N)  
  70.             {  
  71.                 System.out.println("The document does not have "  + N + " pages to partition !" );  
  72.                 return ;  
  73.             }  
  74.               
  75.             int  size = n/N;           
  76.             String staticpath = filepath.substring(0 , filepath.lastIndexOf("//" )+1 );              
  77.             String savepath = null ;  
  78.             ArrayList<String> savepaths = new  ArrayList<String>();  
  79.             for (int  i=1 ; i<=N; i++)  
  80.             {  
  81.                 if (i < 10 )  
  82.                 {  
  83.                     savepath = filepath.substring(filepath.lastIndexOf("//" )+1 , filepath.length()-4 );  
  84.                     savepath = staticpath + savepath + "0"  + i + ".pdf" ;  
  85.                     savepaths.add(savepath);                      
  86.                 }  
  87.                 else   
  88.                 {  
  89.                     savepath = filepath.substring(filepath.lastIndexOf("//" )+1 , filepath.length()-4 );  
  90.                     savepath = staticpath + savepath + i + ".pdf" ;  
  91.                     savepaths.add(savepath);  
  92.                 }  
  93.             }             
  94.               
  95.             for (int  i=0 ; i<N-1 ; i++)  
  96.             {  
  97.                 document = new  Document(reader.getPageSize(1 ));  
  98.                 copy = new  PdfCopy(document, new  FileOutputStream(savepaths.get(i)));             
  99.                 document.open();  
  100.                 for (int  j=size*i+1 ; j<=size*(i+1 ); j++)  
  101.                 {  
  102.                     document.newPage();   
  103.                     PdfImportedPage page = copy.getImportedPage(reader, j);  
  104.                     copy.addPage(page);  
  105.                 }  
  106.                 document.close();  
  107.             }  
  108.               
  109.               
  110.             document = new  Document(reader.getPageSize(1 ));  
  111.             copy = new  PdfCopy(document, new  FileOutputStream(savepaths.get(N-1 )));  
  112.             document.open();  
  113.             for (int  j=size*(N-1 )+1 ; j<=n; j++)  
  114.             {  
  115.                 document.newPage();   
  116.                 PdfImportedPage page = copy.getImportedPage(reader, j);  
  117.                 copy.addPage(page);  
  118.             }  
  119.             document.close();  
  120.   
  121.         } catch  (IOException e) {  
  122.             e.printStackTrace();  
  123.         } catch (DocumentException e) {  
  124.             e.printStackTrace();  
  125.         }  
  126.     }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章