java中pdf转图片

 方法一:

​​​​​​​使用apache中pdfbox:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

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




public class PdfToImg {
    public static void main(String[] args) {
        String filePath = "F:/111.pdf";
        List imageList = pdfToImagePath(filePath);
        Iterator iterator = imageList.iterator();
        while (iterator.hasNext()) {

            System.out.println(iterator.next());
        }
        System.out.println(imageList);
    }

    public static List pdfToImagePath(String filePath) {
        List list = new ArrayList<>();
        String fileDirectory = filePath.substring(0, filePath.lastIndexOf("."));//获取去除后缀的文件路径

        String imagePath;
        File file = new File(filePath);
        try {
            File f = new File(fileDirectory);
            if (!f.exists()) {
                f.mkdir();
            }
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                // 方式1,第二个参数是设置缩放比(即像素)
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
                // 方式2,第二个参数是设置缩放比(即像素)
                BufferedImage images = renderer.renderImage(i, 1.25f);  //第二个参数越大生成图片分辨率越高,转换时间也就越长
                imagePath = fileDirectory + "/" + i + ".jpg";
                ImageIO.write(images, "PNG", new File(imagePath));
                list.add(imagePath);
                return list;
            }
            doc.close();              //关闭文件,不然该pdf文件会一直被占用。
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

涉及jar包:pdfbox-2.0.24.jar下载地址:https://pdfbox.apache.org/download.html

方法二: 

使用icepdf:

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

/**
 * PDF 转 图片
 */
public class IcePdf {
    public static void pdf2Pic(String pdfPath, String path) throws Exception {
        Document document = new Document();
        document.setFile(pdfPath);
        //缩放比例
        float scale = 2.5f;
        //旋转角度
        float rotation = 0f;

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage)
                    document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                String imgName = i + ".png";
                System.out.println(imgName);
                File file = new File(path + imgName);
                ImageIO.write(rendImage, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
        }
        document.dispose();
    }
    public static void main(String[] args) throws Exception {
        String filePath = "D:\\Code\\pdf2pic\\src\\main\\resources\\检测报告.pdf";
        pdf2Pic(filePath, "D:\\Code\\pdf2pic\\src\\main\\resources\\");
    }
}  

你可能感兴趣的:(java)