pdf转图片
package com.....util; import lombok.extern.slf4j.Slf4j; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.ArrayList; import java.util.List; @Slf4j public class PdfUtil { //经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大,一般电脑显示分辨率为96 public static final float DEFAULT_DPI = 105; //默认转换的图片格式为jpg public static final String DEFAULT_FORMAT = "jpg"; public static void main(String[] args) throws IOException { PdfUtil.pdfToImage("D:\\test.pdf", "D:\\test"); } public static void pdfToImage(String pdfPath, String imgPath) { try { //图像合并使用参数 // 总宽度 int width = 0; // 保存一张图片中的RGB数据 int[] singleImgRGB; int shiftHeight = 0; //保存每张图片的像素值 BufferedImage imageResult = null; //利用PdfBox生成图像 PDDocument pdDocument = PDDocument.load(new File(pdfPath)); PDFRenderer renderer = new PDFRenderer(pdDocument); //循环每个页码 for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) { BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB); int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); //计算高度和偏移量 //使用第一张图片宽度; width = imageWidth; //保存每页图片的像素值 imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB); //这里有高度,可以将imageHeight*len,我这里值提取一页所以不需要 singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width); // 写入流中 imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写图片 File f = new File(imgPath, "test_" + i + ".png"); if (!f.exists()) { f.createNewFile(); } ImageIO.write(imageResult, DEFAULT_FORMAT, f); } pdDocument.close(); } catch (Exception e) { e.printStackTrace(); } //OVER } public static List<byte[]> pdfToImage(String pdfPath) { List<byte[]> result = new ArrayList<>(); try { //图像合并使用参数 // 总宽度 int width = 0; // 保存一张图片中的RGB数据 int[] singleImgRGB; int shiftHeight = 0; //保存每张图片的像素值 BufferedImage imageResult = null; //利用PdfBox生成图像 PDDocument pdDocument = PDDocument.load(new File(pdfPath)); PDFRenderer renderer = new PDFRenderer(pdDocument); //循环每个页码 for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) { BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB); int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); //计算高度和偏移量 //使用第一张图片宽度; width = imageWidth; //保存每页图片的像素值 imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB); //这里有高度,可以将imageHeight*len,我这里值提取一页所以不需要 singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width); // 写入流中 imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写图片 ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(imageResult, DEFAULT_FORMAT, out); result.add(out.toByteArray()); } pdDocument.close(); } catch (Exception e) { e.printStackTrace(); } return result; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2021-03-30 RocketMQ(三)——————javaAPI (9.延迟消息)
2021-03-30 RocketMQ(三)——————javaAPI (8.重试机制)
2021-03-30 RocketMQ(三)——————javaAPI (7.事务消息)
2021-03-30 RocketMQ(三)——————javaAPI (6.顺序消费)
2021-03-30 RocketMQ(三)——————javaAPI (5.过滤消息)
2021-03-30 RocketMQ(三)——————javaAPI (1.2.3.4 接收方式)
2021-03-30 RocketMQ(三)——————javaAPI (4.发送批量消息)