码迷,mamicode.com
首页 > 其他好文 > 详细

合并PDF

时间:2017-11-24 00:15:13      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:doc   str   span   length   merger   row   int   static   parent   

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

标签:doc   str   span   length   merger   row   int   static   parent   

原文地址:http://www.cnblogs.com/lovedaodao/p/7886455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!