java 将pdf多页转换成一张图片,支持指定页数

代码:/** * 将pdf中的maxPage页,转换成一张图片 * * @param pdfFile *pdf的路径 * @param outpath *输出的图片的路径[包括名称] * @param maxPage *pdf的页数 *【比如Pdf有3页,如果maxPage=2,则将pdf中的前2页转成图片,如果超过pdf实际页数,则按实际页数转换】 */private static void pdf2multiImage(String pdfFile, String outpath, int maxPage) {try {InputStream is = new FileInputStream(pdfFile);PDDocument pdf = PDDocument.load(is, true);List<PDPage> pages = pdf.getDocumentCatalog().getAllPages();List<BufferedImage> piclist = new ArrayList<BufferedImage>();int actSize = pages.size(); // pdf中实际的页数if (actSize < maxPage) maxPage = actSize;for (int i = 0; i < maxPage; i++) {piclist.add(pages.get(i).convertToImage());}yPic(piclist, outpath);is.close();} catch (IOException e) {e.printStackTrace();}}/** * 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同 * * @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[]>(); // 保存所有的图片的RGBint[] _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();}}调用:

public static void main(String[] args) {pdf2multiImage("f:/bak/aa.pdf", "f:/bak/bbbbb.jpg", 4);}转换图片,需要两个jar包:

commons-logging.jar

pdfbox-1.6.0.jar

『 不可能 』只存在於蠢人的字典里

java 将pdf多页转换成一张图片,支持指定页数

相关文章:

你感兴趣的文章:

标签云: