PDF查询文件页数 将文件转PNG图片

1.在pom文件中添加依赖

<dependency>

   <groupId>org.apache.pdfbox</groupId>

   <artifactId>fontbox</artifactId>

   <version>2.0.16</version>

</dependency>

<dependency>

   <groupId>org.apache.pdfbox</groupId>

   <artifactId>pdfbox</artifactId>

   <version>2.0.16</version>

</dependency>

2.工具类

package net.longjin.comm.utils;

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;
import java.io.IOException;

/**
 * PDF操作工具类
 */
public class PDFUtils {

    private static final Logger log = LoggerFactory.getLogger(PDFUtils.class);

    /**
     * 查询文件页数
     * @param filePath
     * @return
     */
    public static int getPDFPages(String filePath){
        int pages = 0;
        try {
            File file = new File(filePath);
            PDDocument pdDocument = PDDocument.load(file);
            pages = pdDocument.getNumberOfPages();
            pdDocument.close();
            log.info(filePath+"PDF总页数:" + pages);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pages;
    }


    public static void main(String[] args) {
//        System.out.println(getPDFPages("d:/ceshi.pdf"));
        pdf2Image("d:/ceshi.pdf","d:");
    }

    /***
     * PDF文件转PNG图片,全部页数
     *
     * @param PdfFilePath pdf完整路径
     * @param dstImgFolder 图片存放的目录
     * @return
     */

    public static String pdf2Image(String PdfFilePath, String dstImgFolder) {
        int dpi = 100;//dpi越大转换后越清晰,相对转换速度越慢
        File file = new File(PdfFilePath);
        PDDocument pdDocument;
        try {
            int dot = file.getName().lastIndexOf('.');
            String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
            StringBuffer imgFilePath = null;
            if (createDirectory(dstImgFolder)) {
                pdDocument = PDDocument.load(file);
                PDFRenderer renderer = new PDFRenderer(pdDocument);
                /* dpi越大转换后越清晰,相对转换速度越慢 */
                String imgFilePathPrefix = dstImgFolder + "/" + imagePDFName;
                imgFilePath = new StringBuffer();
                imgFilePath.append(imgFilePathPrefix);
                imgFilePath.append("_");
                imgFilePath.append(String.valueOf(1));
                imgFilePath.append(".png");
                File dstFile = new File(imgFilePath.toString());
                BufferedImage image = renderer.renderImageWithDPI(0, dpi);
                ImageIO.write(image, "png", dstFile);
                log.info(PdfFilePath+">>>PDF文档转PNG图片成功!>>>"+imgFilePath.toString());
                pdDocument.close();
                return imgFilePath.toString();
            } else {
                log.info(PdfFilePath+"PDF文档转PNG图片失败:" + "创建" + dstImgFolder + "失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean createDirectory(String folder) {
        File dir = new File(folder);
        if (dir.exists()) {
            return true;
        } else {
            return dir.mkdirs();
        }

    }
}

3.运行如下

发布了99 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_39643007/article/details/96304519