PDF转成图片

使用开源库Apache PDFBox将PDF转换为图片

依赖

html 复制代码
<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>fontbox</artifactId>
	<version>2.0.4</version>
</dependency>
<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>pdfbox</artifactId>
	<version>2.0.4</version>
</dependency>
java 复制代码
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import javax.imageio.ImageIO;

import cn.hutool.core.lang.UUID;
import com.zxzx.ssm.common.constant.SsmConstant;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

/**
 * @author admin
 */
public class PDFToImageConverter {

    /**
     * 按照页数返回图片-转成BASE64
     */
    public static List<String> pdfToPng(File pdfFile) {
        List<String> list = new ArrayList<>();
        try (PDDocument doc = PDDocument.load(pdfFile)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                File file = new File(SsmConstant.TEMP_PIC + "/" + pdfFile.getName() + "_" + (i + 1) + ".png");
                ImageIO.write(image, "png", file);
                InputStream inputStream = Files.newInputStream(file.toPath());
                byte[] byteArray = IOUtils.toByteArray(inputStream);
                String s = Base64.getEncoder().encodeToString(byteArray);
                list.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 转换到同一张图片
     */
    public static String pdfToPngCollect(File file) {
        String outPicPath = SsmConstant.TEMP_PIC + UUID.fastUUID() + ".png";
        try (PDDocument doc = PDDocument.load(file)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            List<BufferedImage> imageList = new ArrayList<>();
            int height = 0;
            int weight = 0;
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                height += image.getHeight();
                weight = Math.max(weight, image.getWidth());
                imageList.add(image);
            }
            BufferedImage mergedImage = new BufferedImage(weight, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = mergedImage.createGraphics();
            int accHeight = 0;
            for (BufferedImage image : imageList) {
                g.drawImage(image, 0, accHeight, null);
                accHeight += image.getHeight();
            }
            g.dispose();
            File outputfile = new File(outPicPath);
            ImageIO.write(mergedImage, "jpg", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outPicPath;
    }

}
相关推荐
shouchaobao18 小时前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
南风微微吹1 天前
2026年最新国考《行测》《申论》历年真题及答案PDF电子版(2000-2025年)
pdf·国考
q***01771 天前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
嗯、.1 天前
使用Itext9生成PDF水印,兼容不同生成引擎的坐标系(如: Skia、OpenPDF)
java·pdf·itextpdf·openpdf·坐标变换矩阵
拓端研究室1 天前
专题:2025AI产业全景洞察报告:企业应用、技术突破与市场机遇|附920+份报告PDF、数据、可视化模板汇总下载
大数据·人工智能·pdf
南风微微吹1 天前
2026年新大纲普通话考试真题题库50套PDF电子版
pdf·普通话
JHC0000002 天前
Python PDF 相关操作
开发语言·python·pdf
CodeCraft Studio2 天前
ABViewer 16全新发布:3D可视化、PDF转DWG、G-code生成全面升级
pdf
诸神缄默不语3 天前
如何用Python处理文件:Word导出PDF & 如何用Python从Word中提取数据:以处理简历为例
python·pdf·word
i***66503 天前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf