用iText分割和合并pdf文件

      可以使用iText将多个pdf文件合并成一个pdf文件,也可以使用iText将一个pdf文件分割成多个pdf文件。下面的代码是一个简单的例子。

 

Java代码   收藏代码
  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.     }  
  127. }  

 

 

      当然,可以做的复杂一些:可以做成有界面形式的,可以选择对哪些(个)文件进行操作,可以选择保存的文件名等等;也可以用命令行形式,在参数中选择是进行合并操作还是分割操作,并输入要操作的pdf文件名,已经保存的文件名,分割的数量等。还可以做的更复杂,例如,可以选择合并这些文件的哪些页,分割某个文件的哪些页等等。这只需要做一些相应的修改就可以了。

 

      附件为可能会用到的jar包,注意使用iText的不同版本时的问题。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值