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

}
相关推荐
jxf_jxfcsdn1 小时前
python读取pdf文档
开发语言·python·pdf
蜗牛沐雨1 小时前
如何生成美观且内容稳定的PDF文档:从基础到进阶的全方案解析
人工智能·pdf·tensorflow
Jamence2 小时前
国产开源PDF解析工具MinerU
人工智能·pdf·aigc
伟贤AI之路8 小时前
清华大学:DeepSeek与AI幻觉(31页PDF)
人工智能·pdf
d3soft15 小时前
deepseek清华大学第二版 如何获取 DeepSeek如何赋能职场应用 PDF文档 电子档(附下载)
ai·pdf·教程·deepseek·赋能职场
hello_simon15 小时前
【Word转PDF】在线Doc/Docx转换为PDF格式 免费在线转换 功能强大好用
职场和发展·pdf·word·学习方法·word转pdf·石墨文档·word转换
快乐的二进制鸭18 小时前
uniapp实现app的pdf预览
pdf·uni-app
三月七(爱看动漫的程序员)20 小时前
与本地电脑PDF文档对话的PDF问答程序
前端·人工智能·chrome·gpt·搜索引擎·pdf·知识图谱
ceffans21 小时前
PDF文档中文本解析
c++·windows·pdf
CodeCraft Studio1 天前
文档处理控件TX Text Control系列教程:使用 .NET C# 从 PDF 文档中提取基于模板的文本
pdf·c#·.net