把PDF转换成指定后缀名的图片

  生活中难免遇到各种文件类型转换的问题,尤其是在办理一些证件的时候。例如,申请居住证积分的时候,把PDF版本的毕业证扫描件转换成jpg或者png等。下面提供一个工具,用于把PDF转换成指定后缀名的图片,需要的话,可以拿来即用。

package com.swagger.demo.controller;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class Pdf2Image {
    private final static Logger logger = LoggerFactory.getLogger(Pdf2Image.class);

    public static void main(String[] args) {
        // 文件存放目录
        String pathName = "D:\\testfiles";
        convertFileType(pathName);
    }

     public static void convertFileType(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File innerFile : files) {
                    if (innerFile.isDirectory()) {
                        logger.info("文件夹:" + innerFile.getAbsolutePath());
                        convertFileType(innerFile.getAbsolutePath());
                    } else {
                        logger.info("文件:" + innerFile.getAbsolutePath());
                        String fileName = innerFile.getName();
                        //获取文件的后缀名
                        String suffix = fileName.substring(fileName.lastIndexOf("."));
                        logger.info(fileName + ", suffix = " + suffix);
                        // 只处理PDF文件,当然,可以根据需要,自行更改待处理文件类型
                        if (".pdf".equals(suffix)) {
                            String imagePath = innerFile.getAbsolutePath();
                            imagePath = imagePath.substring(0, imagePath.lastIndexOf(".")).concat(".jpg");
                            pdfToImage(innerFile, imagePath);
                        }
                    }
                }
            }
        } else {
            logger.info("文件不存在!");
        }
    }

    /**
     * 把pdf转换成PNG、jpg等类型的图片
     *
     * @param file  pdf文件
     * @param imagePath 图片路径
     */
    public static String pdfToImage(File file, String imagePath) {
        logger.info("------------pdf转PNG---------------");
        long start = System.currentTimeMillis();
        try (PDDocument doc = PDDocument.load(file)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                // 第二个参数越大转换后图片分辨率越高,转换耗时也就越长
                BufferedImage image = renderer.renderImage(i, 2f);
                ImageIO.write(image, "JPG", new File(imagePath));
//                ImageIO.write(image, "PNG", new File(imagePathAndName));
            }
            logger.info(file.getName() + "转换完成,耗时:" + (System.currentTimeMillis() - start) + "ms。");
        } catch (Exception e) {
            logger.error("------pdf转PNG失败,异常信息:" + e.getMessage() + "---------");
        }
        return imagePath;
    }
}

posted @ 2021-07-17 15:25  楼兰胡杨  阅读(380)  评论(0编辑  收藏  举报