博客
关于我
pdf转图片(qq:1197852132)
阅读量:691 次
发布时间:2019-03-17

本文共 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 List
toImgList(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转成图片,下面是我们完成的代码。

转载地址:http://gipez.baihongyu.com/

你可能感兴趣的文章
Java学习记录之ArrayList集合
查看>>
PHP之配置开发环境
查看>>
Shiro 的身份认证
查看>>
什么是信道编码?信道编码比较
查看>>
wordpress架站踩坑过程
查看>>
一个简单的游戏框架[汇总]
查看>>
NSNotification、delegate和KVO的区别
查看>>
Rhino简介
查看>>
防止用户重复提交表单的处理方法
查看>>
JS代码执行顺序
查看>>
免费好用的证件扫描仪-扫描全能王
查看>>
自定义拦截器
查看>>
自定义拦截器
查看>>
Eclipse 代码规范配置
查看>>
Kafka Producer机制优化-提高发送消息可靠性
查看>>
面试题5:(事务管理) ACID 是什么?
查看>>
ASCII码表
查看>>
10.Mybatis执行流程
查看>>
【车间调度】遗传算法求解混合流水车间调度最优问题【Matlab 017期】
查看>>
Oracle 一张表里面按照一个字段值将所有的数据按逗号拆分,变为多行数据
查看>>