Java使用icepdf将pdf文件按页转成图片

所属分类: 软件编程 / java 阅读数: 32
收藏 0 赞 0 分享

本文实例为大家分享了Java使用icepdf将pdf文件按页转成图片的具体代码,供大家参考,具体内容如下

Maven icepdf包,这里过滤掉jai-core

<dependency>
 <groupId>org.icepdf.os</groupId>
 <artifactId>icepdf-core</artifactId>
 <version>6.1.2</version> 
 <exclusions>
  <exclusion>
   <groupId>javax.media</groupId>
   <artifactId>jai-core</artifactId>
  </exclusion>
 </exclusions>
</dependency>

代码如下

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import org.apache.commons.logging.LogFactory;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;


public class test20160929 {
 public static final String FILETYPE_JPG = "jpg";

 /**
  * 
  * 将指定的pdf文件转换为指定路径的图片
  * 
  * @param filepath 原文件路径,例如d:/test/test.pdf
  * 
  * @param imagepath 图片生成路径,例如 d:/test/
  * 
  * @param zoom 缩略图显示倍数,1表示不缩放,0.3则缩小到30%
  * 
  */
 public static void tranfer(String filepath, String imagepath, float zoom) throws PDFException, PDFSecurityException, IOException {
  Document document = null;
  float rotation = 0f;
  document = new Document();
  document.setFile(filepath);
  int maxPages = document.getPageTree().getNumberOfPages();

  for (int i = 0; i < maxPages; i++) {
   BufferedImage img = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom);
   Iterator iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);
   ImageWriter writer = (ImageWriter) iter.next();
   File outFile = new File(imagepath + new File(filepath).getName() + "_" + new DecimalFormat("000").format(i) + "." + FILETYPE_JPG);
   FileOutputStream out = new FileOutputStream(outFile);
   ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
   writer.setOutput(outImage);
   writer.write(new IIOImage(img, null, null));
  }
  System.out.println("转换完成");
 }

 public static void main(String[] args) throws PDFException, PDFSecurityException, IOException {
  tranfer("d:/test/test.pdf", "d:/test/", 1);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Spring Boot 配置 IDEA和DevTools 热部署的方法

这篇文章主要介绍了Spring Boot 配置 IDEA和DevTools 热部署的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot使用Redis缓存的实现方法

这篇文章主要介绍了SpringBoot使用Redis缓存的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot中自定义参数绑定步骤详解

这篇文章主要介绍了SpringBoot中自定义参数绑定步骤详解,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Java实现abc字符串排列组合

这篇文章主要为大家详细介绍了JAVA实现abc字符串的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java中后台线程实例解析

这篇文章主要介绍了Java中后台线程实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

ehcache模糊批量移除缓存的方法

本篇文章主要介绍了ehcache模糊批量移除缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java多线程join方法实例代码

这篇文章主要介绍了Java多线程join方法实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现字符串排列组合问题

这篇文章主要为大家详细介绍了java实现字符串排列组合问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java排列组合字符串的方法

这篇文章主要介绍了Java排列组合字符串的方法
收藏 0 赞 0 分享

Java语言中的自定义类加载器实例解析

这篇文章主要介绍了Java语言中的自定义类加载器实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多