pdf转jpg

package msdev.card;/*
*文件名: PDFToImgUtil
*创建者: Pengirongz
*创建时间:2021-7-20 9:22
*描述: pdf转jpg
*/

import com.itextpdf.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class PDFToImgUtil {

/***
 * PDF文件转PNG/JPEG图片
 * @param file
 * @param imgFilePath图片存放的文件夹2
 * @param dpi越大转换后越清晰,相对转换速度越慢,一般电脑默认96dpi
 */
public static void pdf2ImageDemo(File file, String dstImgFolder, int dpi) {
    PDDocument pdDocument;
    try {
        String imgPDFPath = file.getParent();
        int dot = file.getName().lastIndexOf('.');
        // 获取图片文件名
        String imagePDFName = file.getName().substring(0, dot);
        String imgFolderPath = null;
        if (dstImgFolder.equals("")) {
            // 获取图片存放的文件夹路径
            imgFolderPath = imgPDFPath + File.separator + imagePDFName;
        } else {
            imgFolderPath = dstImgFolder + File.separator + imagePDFName;
        }
        if (createDirectory(imgFolderPath)) {
            pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            StringBuffer imgFilePath = null;
            String imgFilePathPrefix = imgFolderPath
                    + File.separator + imagePDFName;
            imgFilePath = new StringBuffer();
            imgFilePath.append(imgFilePathPrefix);
            imgFilePath.append("_");
            imgFilePath.append(String.valueOf(1));
            imgFilePath.append(".png");// PNG
            File dstFile = new File(imgFilePath.toString());
            BufferedImage image = renderer.renderImageWithDPI(0, dpi);
            ImageIO.write(image, "png", dstFile);// PNG
            System.out.println("PDF文档转JPG图片成功!");
        } else {
            System.out.println("PDF文档转JPG图片失败:"
                    + "创建" + imgFolderPath + "失败");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/***
 * PDF文件转PNG/JPEG图片
 * @param PdfFilePathpdf完整路径
 * @param imgFilePath图片存放的文件夹2
 * @param dpi越大转换后越清晰,相对转换速度越慢,一般电脑默认96dpi
 */
public static void pdf2ImageDemo(String PdfFilePath, String dstImgFolder, int dpi) {
    File file = new File(PdfFilePath);
    PDDocument pdDocument;
    try {
        String imgPDFPath = file.getParent();
        int dot = file.getName().lastIndexOf('.');
        // 获取图片文件名
        String imagePDFName = file.getName().substring(0, dot);
        String imgFolderPath = null;
        if (dstImgFolder.equals("")) {
            // 获取图片存放的文件夹路径
            imgFolderPath = imgPDFPath + File.separator + imagePDFName;
        } else {
            imgFolderPath = dstImgFolder + File.separator + imagePDFName;
        }

        if (createDirectory(imgFolderPath)) {
            pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            PdfReader reader = new PdfReader(PdfFilePath);
            int pages = reader.getNumberOfPages();// 获取PDF页数
            System.out.println("PDF page number is:" + pages);
            StringBuffer imgFilePath = null;
            for (int i = 0; i < pages; i++) {
                String imgFilePathPrefix = imgFolderPath
                        + File.separator + imagePDFName;
                imgFilePath = new StringBuffer();
                imgFilePath.append(imgFilePathPrefix);
                imgFilePath.append("_");
                imgFilePath.append(String.valueOf(i + 1));
                imgFilePath.append(".png");// PNG
                File dstFile = new File(imgFilePath.toString());
                BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                ImageIO.write(image, "png", dstFile);// PNG
            }
            System.out.println("PDF文档转JPG图片成功!");
        } else {
            System.out.println("PDF文档转JPG图片失败:"
                    + "创建" + imgFolderPath + "失败");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值