Java PDF 转换图片

/ 代码

Java PDF 转换图片

Maven引入jar

<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itext7-core</artifactId>
	<version>7.0.5</version>
	<type>pom</type>
</dependency>

<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.13</version>
</dependency>

<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>pdfbox</artifactId>
	<version>2.0.15</version>
</dependency>

代码集成

创建PdfToImageUtils.java

    /**
     * Pdf转换图片
     *
     * @param pageNum       获取pdf第几页转换为图片,不填写转换全部图片
     * @param pdfFilePath   需要转换的pdf路径
     * @param imgFolderPath 输出路径
     */
    public static List<Map<String, Object>> pdfConvertImage(Integer pageNum, String pdfFilePath, String imgFolderPath) {
        try {
            File dir = new File(imgFolderPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            Map<String, Object> resultMap = new HashMap<String, Object>();
            PDDocument pdDocument = PDDocument.load(new File(pdfFilePath));
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            //总共有多少页
            if (ObjectUtil.isNull(pageNum)) {
                pageNum = pdDocument.getNumberOfPages();
            }
            for (int page = 1; page <= pageNum; page++) {
                long beginTime = System.currentTimeMillis();
                //转换指定页PDF为图片
                String imgFilePath = imgFolderPath + File.separator + page + ".jpg";
                //图片路径
                File dstFile = new File(imgFilePath);
                //100表示DPI
                BufferedImage image = renderer.renderImageWithDPI(page - 1, 100);
                ImageIO.write(image, "jpeg", dstFile);
                System.out.println("转换第" + page + "页耗时:" + (System.currentTimeMillis() - beginTime) + "ms");
                PdfReader reader = new PdfReader(pdfFilePath);
                Document document = new Document(reader.getPageSize(page));
                float w = document.getPageSize().getWidth();
                float h = document.getPageSize().getHeight();
                //图片高度
                resultMap.put("image_height", image.getHeight());
                //pdf高度
                resultMap.put("pdf_height", h);
                //图片宽度
                resultMap.put("image_width", image.getWidth());
                //pdf宽度
                resultMap.put("pdf_width", w);
                list.add(resultMap);
            }
            pdDocument.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取pdf页数
     *
     * @param pdfFilePath   需要转换的pdf路径
     */
    public static Integer pdfImageCount(String pdfFilePath) {
        try {
            PDDocument pdDocument = PDDocument.load(new File(pdfFilePath));
            //总共有多少页
            Integer pageNum = null;
            if (ObjectUtil.isNull(pageNum)) {
                pageNum = pdDocument.getNumberOfPages();
            }
            pdDocument.close();
            return pageNum;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }