合并PDF
Posted 亲爱的阿道君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了合并PDF相关的知识,希望对你有一定的参考价值。
public static List<File> mergePdf(List<String> filePath) throws Exception { List<File> list = new ArrayList<File>(); long m10 = 10 * 1024 * 1024; long legth = 0; File file = null; PDFMergerUtility mergePdf = new PDFMergerUtility(); for (int i = 0; i < filePath.size(); i++) { file = new File(filePath.get(i)); if (file != null) { if (i == (filePath.size() - 1)) { mergePdf.addSource(file); String mergePdfPath = file.getParentFile() + "\\" + (new Date().getTime()) + ".pdf"; mergePdf.setDestinationFileName(mergePdfPath); mergePdf.mergeDocuments(); list.add(new File(mergePdfPath)); } if (file.length() >= m10) {// 单个文件大于10M,不合并 mergePdf = new PDFMergerUtility(); mergePdf.addSource(file); String mergePdfPath = file.getParentFile() + "\\" + (new Date().getTime()) + ".pdf"; mergePdf.setDestinationFileName(mergePdfPath); mergePdf.mergeDocuments(); list.add(new File(mergePdfPath)); continue; } if ((file.length() + legth) >= m10) {// 合并pdf String mergePdfPath = file.getParentFile() + "\\" + (new Date().getTime()) + ".pdf"; mergePdf.setDestinationFileName(mergePdfPath); mergePdf.mergeDocuments(); list.add(new File(mergePdfPath)); legth = file.length(); mergePdf = new PDFMergerUtility(); mergePdf.addSource(file); } else { legth += file.length(); mergePdf.addSource(file); } } } return list; }
以上是关于合并PDF的主要内容,如果未能解决你的问题,请参考以下文章
使用在线软件合并pdf论文文件安全吗?不妨使用简单的python代码