/** * * 多个pdf文件合并为一个pdf文件 * @Classname MulFileToOneUtils * @Description TODO * @Date 2023/6/14 0014 下午 2:01 * @Created by Administrator */ public class MulFileToOneUtils { public static File MulFileToOne(List<File> files, String targetPath) throws Exception { PDFMergerUtility mergePdf = new PDFMergerUtility(); for (File f : files) { if(f.exists() && f.isFile()){ // 循环添加要合并的pdf mergePdf.addSource(f); } } // 设置合并生成pdf文件名称 mergePdf.setDestinationFileName(targetPath); // 合并pdf mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); return new File(targetPath); } public static void main(String[] args) throws IOException { List<File> files = new ArrayList(); File file = new File("C:\\Users\\Administrator\\Desktop\\费用管理打印表格"); File[] tempList = file.listFiles(); //获取该文件夹下的文件(文件都是PDF) for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile() && tempList[i].getName().endsWith("pdf")) { files.add(tempList[i]); } } try { File f = MulFileToOne(files, "C:\\Users\\Administrator\\Desktop\\test.pdf"); System.out.println(f.length()); } catch (Exception e){ e.printStackTrace(); } } }
标签:files,JAVA,合并,mergePdf,File,pdf,PDF,new,tempList From: https://www.cnblogs.com/jthr/p/17851874.html