zoukankan      html  css  js  c++  java
  • 合并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;
        }
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    jenkins+pytest+ allure运行多个py文件测试用例
    jenkins发送测试报告邮件
    appium+python 存在多个类时,不用每次都初始化解决办法
    allure报告定制(pytest+jenkins)
    UVa202
    UVa1588
    UVa1587
    OpenJ_Bailian3377
    OpenJ_Bailian 1852
    UVa227
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/7886455.html
Copyright © 2011-2022 走看看