java中pdf转图片,(多页pdf转成一张图或多张图),

来源:互联网 发布:淘宝内容营销是什么 编辑:程序博客网 时间:2024/05/21 22:44

1:icepdf的jar包下载地址

http://www.icesoft.org/java/downloads/icepdf-downloads.jsf


2:所需jar包



3,多页pdf转一张图代码如下,转换的图片会带有官方的水印。去水印的方法可以查看另一篇文章:icepdf去水印方法

package com.java.pdf;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;import org.icepdf.core.pobjects.Document;import org.icepdf.core.pobjects.Page;import org.icepdf.core.util.GraphicsRenderingHints;public class PdfToJpg {            public static void main(String[] args) {        String pdfFile = "E:\\pdf\\aa.pdf";        String outpath = "E:\\pdf\\aa.jpg";        pdf2multiImage(pdfFile,outpath,1000);    }    private static void pdf2multiImage(String pdfFile, String outpath, int maxPage) {          try {              InputStream is = new FileInputStream(pdfFile);              List<BufferedImage> piclist = new ArrayList<BufferedImage>();              Document document = new Document();              try {                  document.setFile(pdfFile);                  } catch (Exception ex) {              }              float scale = 2.5f;  //缩放比例            float rotation = 0f;  //旋转角度            for (int i = 0; i < document.getNumberOfPages(); i++) {                  BufferedImage image = (BufferedImage)                  document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX, rotation, scale);                  piclist.add(image);            }              document.dispose();             yPic(piclist, outpath);              is.close();          } catch (IOException e) {              e.printStackTrace();          }      }              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_RGB);              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);              ImageIO.write(imageResult, "jpg", outFile);// 写图片          } catch (Exception e) {              e.printStackTrace();          }      }      }


4:多页pdf转多页图片代码如下

public static void main(String[] args) {        String filePath = "E:\\pdf\\C20170613004001.pdf";          Document document = new Document();          try {              document.setFile(filePath);              } catch (Exception ex) {          }          float scale = 2.5f;          float rotation = 0f;          for (int i = 0; i < document.getNumberOfPages(); i++) {              BufferedImage image = (BufferedImage)              document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX, rotation, scale);              RenderedImage rendImage = image;              try {                  File file = new File("E:\\pdf\\imageCapture1_" + i + ".png");                  ImageIO.write(rendImage, "png", file);                  } catch (IOException e) {                  e.printStackTrace();              }              image.flush();          }          document.dispose();      }



原创粉丝点击