合并多个PDF为一个PDF

it2022-05-05  137

1.导入依赖 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.16</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-tools</artifactId> <version>2.0.16</version> </dependency> 2.工具类 public class PDFUtils { public static void main(String[] args) throws Exception{ Long s = System.currentTimeMillis(); // System.out.println(pdfToImage("d:/3333.pdf")); List<String> pdfList = new ArrayList<>(); pdfList.add("d:/3333.pdf"); pdfList.add("d:/3333.pdf"); pdfList.add("d:/3333.pdf"); pdfList.add("d:/3333.pdf"); pdfList.add("d:/3333.pdf"); String save = "d:/hb.pdf"; mergePDF(pdfList,save); File f = new File(save); System.out.println(f.exists()); System.out.println(System.currentTimeMillis()-s); } /** * 合并多个PDF为一个PDF * @param pdfList PDF文件列表 * @param mergePDFPath 合并后的PDF完整路径 */ public static void mergePDF(List<String> pdfList,String mergePDFPath){ try { PDFMergerUtility PDFmerger = new PDFMergerUtility(); PDFmerger.setDestinationFileName(mergePDFPath); for(String str :pdfList) { File file = new File(str); PDFmerger.addSource(file); } PDFmerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); log.info(pdfList.size()+"个PDF合并成功"); }catch (Exception e){ e.printStackTrace(); } }

}


最新回复(0)