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;
        }
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    6.1.1.1 属性类型之数据属性
    6.1 理解对象
    5.7.2.4 random() 方法
    5.7.2.3 舍入方法
    5.7.2.2 min()和max()方法
    5.7.2.1 Math对象
    5.7.1.4 window对象
    frontend2_s9_part2_fe_d48_form
    s10_part3_django_basic.md
    frontend1_s9_part2_d47_html
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/7886455.html
Copyright © 2011-2022 走看看