
pdf转图片(qq:1197852132)
发布日期:2021-05-20 15:07:29
浏览次数:11
分类:博客文章
本文共 2516 字,大约阅读时间需要 8 分钟。
前面已经把html转成pdf,但是用户可以下载图片格式的文件,所以我们必须把pdf转成图片格式,代码如下
package com.jit.platform.basics.util.pdf;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;import org.jpedal.PdfDecoder;class ImgPp{ BufferedImage img; int width ; int height; public BufferedImage getImg() { return img; } public void setImg(BufferedImage img) { this.img = img; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }public class PdfToImg { public static ListtoImgList(String pdfPath,String imgPath,String imgName) throws Exception{ PdfDecoder decode_pdf = new PdfDecoder(true); decode_pdf.openPdfFile(pdfPath); //file int start = 1, end = decode_pdf.getPageCount(); List list = new ArrayList (); for(int i=start;i list = new ArrayList (); int width = 0; int totalHeight = 0; for(int i=start;i list,int totalHeight,int width,String imgPath,String imgName) throws Exception { //构造一个类型为预定义图像类型之一的 BufferedImage。 宽度为第一只的宽度,高度为各个图片高度之和 BufferedImage tag = new BufferedImage(width, totalHeight, BufferedImage.TYPE_INT_RGB); //绘制合成图像 Graphics g = tag.createGraphics(); int tempHeight = 0; for (int i = 0; i < list.size(); i++) { ImgPp imgPp = list.get(i); g.drawImage(imgPp.getImg(), 0, tempHeight, width, imgPp.getHeight(), null); tempHeight+=imgPp.getHeight(); } // 释放此图形的上下文以及它使用的所有系统资源。 g.dispose(); // Save as new image ImageIO.write(tag, "png", new File(imgPath + imgName)); return imgName; } public static void main(String[] args) { String pdfPath = "D:\\many page.pdf"; String imgPath = "D:\\"; String imgName = "pdfbox_image"; try { String imgOne = toImgOne(pdfPath,imgPath,imgName); System.out.println("imgOne"+imgOne); /*List img2 = toImgList(pdfPath,imgPath,imgName); File[] fileArray = new File[img2.size()]; for (int i = 0; i < img2.size(); i++) { //System.out.println(img2.get(i)); File file = new File(img2.get(i)); fileArray[i] = file; } BatchDownloadAction.makeZip(imgPath,imgName , fileArray);*/ } catch (Exception e1) { e1.printStackTrace(); } } }
项目中遇到了用户还可以下载图片格式的文件,所以我们就需要把pdf转成图片,下面是我们完成的代码。
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月21日 23时57分29秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
RAFT 拜占庭将军 共识算法
2019-03-13
UE4 错误列表 error码(只记录我遇到的情况,持续添加,未完成)
2019-03-13
Android 架构组件 – 让天下没有难做的 App
2019-03-13
能解决数据可视化大屏需求的3款可视化工具
2019-03-13
如何在VSCode中定制JSON的IntelliSense
2019-03-13
椭圆曲线的定义
2019-03-13
多代理区块链框架客户端的操作
2019-03-13
RSA操作中的公钥和私钥的生成
2019-03-13
go语言中类的继承和方法的使用
2019-03-13
一些技术博客
2019-03-13
第01问:MySQL 一次 insert 刷几次盘?
2019-03-13
libvirtd:内部错误:Failed to apply firewall rule
2019-03-13
优先级队列2
2019-03-13
TiKV 源码解析系列文章(十三)MVCC 数据读取
2019-03-13
Android 开发常用的工具类(更新ing)
2019-03-13
EasyUI的简单介绍
2019-03-13
初次安装webpack之后,提示安装webpack-cli
2019-03-13