zoukankan      html  css  js  c++  java
  • 把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;
        }
    }
    
    

      读后有收获,小礼物走一走,请作者喝咖啡。

    赞赏支持

  • 相关阅读:
    WDSL文件中的XML元素
    Cookie跨域setDomain和setPath
    Cookie/Session机制详解
    Java服务端对Cookie的简单操作
    Nginx配置详解 http://www.cnblogs.com/knowledgesea/p/5175711.html
    如何在mac里面,把xcode代码同步到 tfs 的 git库(新git库)
    如何在mac里面,把xcode代码同步到 tfs 的 git库(克隆git篇)
    Centos7 安装 MySql
    如何在Centos里面,把.net core程序设为开机自启动
    Typescript编译设置
  • 原文地址:https://www.cnblogs.com/east7/p/15023547.html
Copyright © 2011-2022 走看看