java合并pdf

一.开发准备

    下载pdfbox-app-1.7.1.jar包;下载地址:http://download.csdn.net/detail/yanning1314/4852276

二.简单小例子

      在开发中在根据实际情况进行修改,下面是主要API的应用

package com.shine.efcms.struts;
import java.io.File;  
import java.io.IOException;  
import org.apache.pdfbox.util.PDFMergerUtility;  

public class test {  
   
  private static String[] getFiles(String folder) throws IOException {  
      File _folder = new File(folder);  
      String[] filesInFolder;     
 
      if(_folder.isDirectory()){  
            filesInFolder = _folder.list();  
            return filesInFolder;  
       } else {  
            throw new IOException("Path is not a directory");  
       }  
    }  
 
    public static void main(String[] args) throws Exception {  
      
        //pdf合并工具类
        PDFMergerUtility mergePdf = new PDFMergerUtility();  

        String folder = "F:/test";  
        String destinationFileName = "mergedTest.pdf";  
          
        String[] filesInFolder = getFiles(folder);     
  
        for(int i = 0; i < filesInFolder.length; i++){ 
            //循环添加要合并的pdf存放的路径
            mergePdf.addSource(folder + File.separator + filesInFolder[i]);  
        }  
        
        //设置合并生成pdf文件名称
        mergePdf.setDestinationFileName(folder + File.separator + destinationFileName);  
        //合并pdf
        mergePdf.mergeDocuments();  
    }  
         
}  

 

posted @ 2017-01-03 18:17  独具匠心  阅读(5936)  评论(3编辑  收藏  举报