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;
    }

}
相关推荐
伊织code2 小时前
pdfminer.six
python·pdf·图片·提取·文本·pdfminer·pdfminer.six
HAPPY酷6 小时前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
代码AI弗森1 天前
PDF OCR + 大模型:让文档理解不止停留在识字
pdf·ocr
小周同学:2 天前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
Kyln.Wu2 天前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
迪尔~4 天前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel
忆~遂愿4 天前
Python实战教程:PDF文档自动化编辑与图表绘制全攻略
python·pdf·自动化
Rust语言中文社区4 天前
简单好用的在线工具:轻松把图片添加到 PDF
pdf
鲁班AI4 天前
pdf怎么转换成ppt?AI工具与传统方法深度对比
人工智能·pdf·powerpoint
大山运维4 天前
免费专业PDF文档扫描效果生成器
pdf