package com.hefeng.demo.controller;
import java.io.File;
import java.io.IOException;
import java.util.*;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
public class PdfFileUtil {
/**
* pdf合并拼接
* @Title:mulFile2One
* @Description: TODO
* @date 2019年9月22日 上午10:05:37
* @author yqwang
* @param files 文件列表
* @param targetPath 合并到
* @return
* @throws IOException
*/
public static File mulFile2One(List<File> files,String targetPath) throws IOException{
// pdf合并工具类
PDFMergerUtility mergePdf = new PDFMergerUtility();
for (File f : files) {
if(f.exists() && f.isFile()){
// 循环添加要合并的pdf
mergePdf.addSource(f);
}
}
// 设置合并生成pdf文件名称
mergePdf.setDestinationFileName(targetPath);
// 合并pdf
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
return new File(targetPath);
}
public static void main(String[] args) throws IOException {
// List<File> files = new ArrayList<File>();
// 方式一 按照文件名称排序 a-z或者1-9
// String path = "/Users/admin/Documents/books/Spring/springboot";
// List<File> filesList = filesList(path);
// File f = mulFile2One(filesList, "狂神说springboot.pdf");
// 方式二 kotlin
List<File> files = new ArrayList<File>();
files.add(new File("/Users/admin/Documents/books/00000001.pdf"));
files.add(new File("/Users/admin/Documents/books/Struts_in_Action_中文版.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/01.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/02.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/03.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/04.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/05.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/06.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/07.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/08.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/09.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/10.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/11.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/12.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/13.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/14.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/15.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/16.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/17.pdf"));
// files.add(new File("/Users/admin/Documents/books/Spring/springboot/18.pdf"));
File f = mulFile2One(files, "/Users/admin/Documents/books/Struts_in_Action_中文版(封面).pdf");
System.out.println(f.length());
}
/**
* 按 文件名称 排序
* @param filePath
* @return
*/
public static List<File> filesList (String filePath) {
List<File> filesList = new ArrayList<File>();
File file = new File(filePath);
File[] files = file.listFiles();
List fileList = Arrays.asList(files);
Collections.sort(fileList, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
if (o1.isDirectory() && o2.isFile())
return -1;
if (o1.isFile() && o2.isDirectory())
return 1;
return o1.getName().compareTo(o2.getName());
}
});
for (File file1 : files) {
if (file1.getName().startsWith(".DS")) {
continue;
}
// System.out.println(file1.getAbsoluteFile());
filesList.add(file1);
}
return filesList;
}
/**
* 按 文件日期 排序
* 按 文件修改日期:递增
* @param filePath
*/
public static void orderByDate(String filePath) {
File file = new File(filePath);
File[] files = file.listFiles();
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f1.lastModified() - f2.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;//如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
}
public boolean equals(Object obj) {
return true;
}
});
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getName());
System.out.println(new Date(files[i].lastModified()));
}
}
/**
* 按 文件大小 排序
* 按照递增的顺序排列
* @param filePath
*/
public static void orderByLength(String filePath) {
File file = new File(filePath);
File[] files = file.listFiles();
List<File> fileList = Arrays.asList(files);
Collections.sort(fileList, new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f1.length() - f2.length();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;//如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
}
public boolean equals(Object obj) {
return true;
}
});
for (File file1 : files) {
if (file1.isDirectory()) continue;
System.out.println(file1.getName() + ":" + file1.length());
}
}
}