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;
        }
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    65 进程互斥锁的优化实现
    Linux多线程编程
    互斥锁和自旋锁
    64 进程互斥锁的初步实现(下)
    63 进程互斥锁的初步实现(中)
    Linux中断子系统
    62 进程互斥锁的初步实现(上)
    61 进程互斥锁的详细设计
    Linux进程调度的时机
    嵌入式领域linux作为实时操作系统的缺点(转)
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/7886455.html
Copyright © 2011-2022 走看看