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

}
相关推荐
东方巴黎~Sunsiny36 分钟前
EasyExcel导出excel再转PDF转图片详解
pdf·excel
aklry4 小时前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app
繁依Fanyi4 小时前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官
DevOpenClub10 小时前
PPT 转高精度 PDF API 接口
pdf·powerpoint
想太多会累i16 小时前
Spring Boot 使用Itext绘制并导出PDF
spring boot·pdf
zixingcai1 天前
Adobe Acrobat pro在一份PDF中插入空白页
pdf·adobe acrobat reader
課代表1 天前
AcroForm JavaScript Promise 对象应用示例: 异步加载PDF文件
开发语言·javascript·pdf·promise·对象
charlie1145141911 天前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6
东风西巷1 天前
MobiPDF:安卓设备上的专业PDF阅读与编辑工具
android·智能手机·pdf·软件需求
离别又见离别1 天前
java实现根据Velocity批量生成pdf并合成zip压缩包
java·pdf