java使用PDFBox2.0将PDF生成图片
2016-06-06 15:44
766 查看
使用到包:commons-logging.jar、 pdfbox-2.0.1.jar、fontbox-2.0.1.jar import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; public class PDFBOXCreate { public static void main(String[] args)throws Exception { long start = System.currentTimeMillis(); String filepath = "D:/Users/ex-wangyide001/workspace/testsrc/1.pdf"; PDDocument document = new PDDocument(); File pdfFile = new File(filepath); document = PDDocument.load(pdfFile, (String)null); int size = document.getNumberOfPages(); List<BufferedImage> piclist = new ArrayList(); for(int i=0 ; i < size; i++){ BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i,130,ImageType.RGB); piclist.add(image); } document.close(); yPic(piclist,"D:/Users/ex-wangyide001/workspace/testsrc/PDFBox1.jpg"); /* FileOutputStream out = new FileOutputStream("D:/Users/ex-wangyide001/workspace/testsrc/yanglaoxian/PDFBox-"+i+".jpg"); ImageIO.write(image, "jpg", out); out.close();*/ long end = System.currentTimeMillis(); System.out.println(end-start); } /** * 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同 * * @param piclist * 文件流数组 * @param outPath * 输出路径 */ public static void yPic(List<BufferedImage> piclist, String outPath) {// 纵向处理图片 if (piclist == null || piclist.size() <= 0) { System.out.println("图片数组为空!"); return; } try { int height = 0, // 总高度 width = 0, // 总宽度 _height = 0, // 临时的高度 , 或保存偏移高度 __height = 0, // 临时的高度,主要保存每个高度 picNum = piclist.size();// 图片的数量 File fileImg = null; // 保存读取出的图片 int[] heightArray = new int[picNum]; // 保存每个文件的高度 BufferedImage buffer = null; // 保存图片流 List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB int[] _imgRGB; // 保存一张图片中的RGB数据 for (int i = 0; i < picNum; i++) { buffer = piclist.get(i); heightArray[i] = _height = buffer.getHeight();// 图片高度 if (i == 0) { width = buffer.getWidth();// 图片宽度 } height += _height; // 获取总高度 _imgRGB = new int[width * _height];// 从图片中读取RGB _imgRGB = buffer .getRGB(0, 0, width, _height, _imgRGB, 0, width); imgRGB.add(_imgRGB); } _height = 0; // 设置偏移高度为0 // 生成新图片 BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); for (int i = 0; i < picNum; i++) { __height = heightArray[i]; if (i != 0) _height += __height; // 计算偏移高度 imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 写入流中 } File outFile = new File(outPath); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(imageResult, "jpg", out);// 写图片 byte[] b = out.toByteArray(); FileOutputStream output = new FileOutputStream(outFile); output.write(b); out.close(); output.close(); } catch (Exception e) { e.printStackTrace(); } } }
PDFBox2.0 下载地址https://pdfbox.apache.org/download.cgi#20x
相关文章推荐
- java pdfbox2.0.0 获取表格和字
- java笔记
- Java解析XML格式串(JDOM解析)
- Java编程练习目录
- Java 学习之 多态性的理解
- JAVA设计模式之---工厂模式
- Ehcache 整合Spring 使用页面、对象缓存
- Java多线程初探——死锁
- java开发webservice简单实例
- [java]序列化框架性能对比(kryo、hessian、java、protostuff)
- SSH框架网上商城项目第24战之Struts2中处理多个Model请求的方法
- Java并发:volatile内存可见性和指令重排
- Java在已经存在的项目上创建WebService步骤
- [框架整合]spring+springmvc+mybatis(4)
- How Spring Boot Autoconfiguration Magic Works--转
- Eclipse快捷键
- eclipse点击一个变量使相同名称变量高亮显示的方法
- java--排序算法之冒泡排序
- javaIO流
- Spring 的BeanUtils.copyProperties在拷贝属性时忽略空值