博客
关于我
pdf转图片(qq:1197852132)
阅读量:692 次
发布时间: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/

你可能感兴趣的文章
wxWidgets源码分析(3) - 消息映射表
查看>>
wxWidgets源码分析(5) - 窗口管理
查看>>
wxWidgets源码分析(8) - MVC架构
查看>>
wxWidgets源码分析(9) - wxString
查看>>
[梁山好汉说IT] 梁山好汉和抢劫银行
查看>>
[源码解析] 消息队列 Kombu 之 基本架构
查看>>
[源码分析] 消息队列 Kombu 之 启动过程
查看>>
wx.NET CLI wrapper for wxWidgets
查看>>
Silverlight for linux 和 DLR(Dynamic Language Runtime)
查看>>
ASP.NET MVC Action Filters
查看>>
Powershell中禁止执行脚本解决办法
查看>>
OO_Unit2 多线程电梯总结
查看>>
git clone 出现fatal: unable to access ‘https://github 错误解决方法
查看>>
04_Mysql配置文件(重要参数)
查看>>
python 加密算法及其相关模块的学习(hashlib,RSA,random,string,math)
查看>>
JavaSE总结
查看>>
Python IO编程
查看>>
CSS入门总结
查看>>
使用 TortoiseGit 时,报 Access denied 错误
查看>>
基于 HTML5 WebGL 的污水处理厂泵站自控系统
查看>>