package com.emr.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Pdf2ImgUtil {
    /**
     * 使用pdfbox将整个pdf转换成图片
     *
     * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
     * @param filename    PDF文件名不带后缀名
     * @param type        图片类型 png 和jpg
     * @param picPath     存放图片的文件路径
     * @return List<String>  图片名数组
     */
    public static List<String> pdfToPic(String fileAddress, String filename, String type, String picPath) throws IOException {
        //long startTime = System.currentTimeMillis();
        // 将文件地址和文件名拼接成路径 注意：线上环境不能使用\\拼接
        File file = new File(fileAddress + "\\" + filename + ".pdf");
        List<String> picNameList = new ArrayList<>();
        PDDocument doc = null;
        try {
            // 写入文件
            doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                // dpi为144，越高越清晰，转换越慢
                BufferedImage image = renderer.renderImageWithDPI(i, 150); // Windows native DPI
                // 将图片写出到该路径下
                ImageIO.write(image, type, new File(picPath + "\\" + "FY_" + filename + "_" + String.format("%04d",i + 1)+ "." + type));
                picNameList.add("FY_" + filename + "_" + String.format("%04d",i + 1)+ "." + type);
            }

            //long endTime = System.currentTimeMillis();
            //System.out.println("共耗时：" + ((endTime - startTime) / 1000.0) + "秒");  //转化用时
            return picNameList;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    throw new RuntimeException("关闭PDDocument时发生错误", e);
                }
            }
        }

    }

    public static void main(String[] args) {
        //pdf2png("D://cost///467f76bec51b4853b640609b0b8feb35/", "35923-20180918", "jpg");
        //File file = new File("D://cost///467f76bec51b4853b640609b0b8feb35/");
        //System.out.println(file);

        //File file = new File("D://cost///467f76bec51b4853b640609b0b8feb35/");
        //File file1 = new File("C:\\Users\\user\\Desktop\\test//");
        //System.out.println(file);
        //System.out.println(file1);

        /*File folder = new File("D://cost///e8554937829b4624b20c89b427bae411/");
        File[] files = folder.listFiles(); // 获取路径下的所有文件和文件夹

        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    System.out.println(file.getName()); // 输出文件名
                }
            }
        }*/

        /*String extension = ".pdf"; // 设置你想要的文件后缀

        File folder = new File("D://cost///467f76bec51b4853b640609b0b8feb35/");
        String[] files = folder.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(extension);
            }
        });

        if (files != null) {
            for (String file : files) {
                System.out.println(file.substring(0, file.lastIndexOf(".")));
            }
        }*/

        //String pathName="\\\\192.168.2.22\\d\\厦门仙岳1期图像\\22盘\\01930\\35923-20180918-64efbec3f1e84472b013dd8f5383d448";
        //File file = new File("\\192.168.2.22\d\\厦门仙岳1期图像\\22盘\\01930\\35923-20180918-64efbec3f1e84472b013dd8f5383d448")



        /*List<String> fileNameList = new ArrayList<>();
        String extension = ".pdf"; // 设置你想要的文件后缀

        File folder = new File("D://cost///467f76bec51b4853b640609b0b8feb35/");
        String[] files = folder.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(extension);
            }
        });

        if (files != null) {
            for (String file : files) {
                String fileName = file.substring(0, file.lastIndexOf("."));
                fileNameList.add(fileName);
            }
        }

        System.out.println(fileNameList);*/

        /*String disDateStr ="2018-09-18";
       String disDateStr1 = disDateStr.substring(0, 4)  + disDateStr.substring(5, 7) + disDateStr.substring(8, 10);
        System.out.println(disDateStr1);*/
        //String fileUrl ="http://192.168.2.22:8080/emr_record\reloadD\00001\0000587004-20200101-9dace9a7161c4758a88ac0b05cdc1be3\000228.jpg";
        //System.out.println(fileUrl);

    }


    /**
     * 根据文件路径名获取pdf文件名
     *
     * @param pathName pdf所在的文件路径名
     * @return
     */
    public static List<String> getPdfName(String pathName) {

        List<String> fileNameList = new ArrayList<>();
        String extension = ".pdf"; // 设置你想要的文件后缀

        File folder = new File(pathName);
        String[] files = folder.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(extension);
            }
        });

        if (files != null) {
            String fileName = "";
            for (String file : files) {
                fileName = file.substring(0, file.lastIndexOf("."));
                fileNameList.add(fileName);
            }
        }
        return fileNameList;
    }


}
