【Java】多个PDF合成一个PDF

    /**
     *将多个pdf文件合并为一个pdf文件
     * @param filePaths 被合并pdf路径+文件完整名
     * @param outPath   合并后输出路径
     * @param outName   合并后文件名
     * @param delete    合并后是否删除被合并文件(true-删除 false -不删除)
     * @throws IOException
     * @throws COSVisitorException
     */
   public static void pdfToOne(List<String> filePaths, String outPath, String  outName, boolean delete) throws IOException, COSVisitorException {
       String destionationFileName =outPath+File.separator+outName+".pdf";
       PDFMergerUtility pdfToOne = new PDFMergerUtility();
       pdfToOne.setDestinationFileName(destionationFileName);
        for (int i = 0; i<filePaths.size();i++){
            File file  = new File(filePaths.get(i));
            pdfToOne.addSource(file);

        }
       pdfToOne.mergeDocuments();
       //必须写在下面,上面不能删除
       if(delete){
           for (int i = 0; i<filePaths.size();i++){
                if(!filePaths.get(i).equals(destionationFileName)){
                    File file = new File(filePaths.get(i));
                    file.delete();
                }
           }
       }
   }

你可能感兴趣的:(java,pdf,开发语言)