import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;

 

 

public static void combinPdf(String[] pdfFilenames, String targetFilename) 
               throws Exception { 
       PdfReader reader = null; 
       Document document = new Document(); 
       PdfCopy pdfCopy = new PdfCopy(document, new FileOutputStream(targetFilename)); //创建一个新文件
       int pageCount = 0; 
       document.open(); 
       for (int i = 0; i < pdfFilenames.length; ++i) { 
           System.out.println(pdfFilenames[i]);
           reader = new PdfReader(pdfFilenames[i]); 
           pageCount = reader.getNumberOfPages(); 
           for (int j = 1; j <= pageCount; ++j) { 
             pdfCopy.addPage(pdfCopy.getImportedPage(reader, j)); 
           } 
       } 
       document.close(); 
    }

 

//测试
          public static void main(String[] args) throws InterruptedException {
              String newFileName = \"D:/新/合成后的新文件.pdf\";//合成后的文件路径
              String fillTemplate1 = \"D:1.pdf\";//需要合并的文件路径
              String fillTemplate2 = \"D:2.pdf\";
              String[] st = {fillTemplate1,fillTemplate2};
              try {
                  combinPdf(st,newFileName);
                  
              } catch (Exception e) {
                  e.printStackTrace();
              }
                      
          }

 

收藏 打印