java实现pdf按页转换为图片

 更新时间:2018年12月19日 14:59:30   作者:huanshirenjian  
这篇文章主要为大家详细介绍了java实现pdf按页转换为图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现pdf按页转换为图片的具体代码,供大家参考,具体内容如下

本程序是利用jacob.jar包实现的,关于jacob.jar的配置见我上一篇文章,程序中可配置参数选择图片清晰图。

package core.util;
 
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
 
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
 
public class PDFchangToImage {
 public static int changePdfToImg(String instructiopath,String picturepath) {
 int countpage =0;
 try {
 //instructiopath ="D:/instructio/2015-05-16/Android 4编程入门经典.pdf"
 //picturepath = "D:/instructio/picture/2015-05-16/";
 
 File file = new File(instructiopath);
 RandomAccessFile raf = new RandomAccessFile(file, "r");
 FileChannel channel = raf.getChannel();
 MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
  0, channel.size());
 PDFFile pdffile = new PDFFile(buf);
 //创建图片文件夹
 File dirfile = new File(picturepath);
  if(!dirfile.exists()){
  dirfile.mkdirs();
 }
 //获得图片页数
 countpage = pdffile.getNumPages();
 for (int i = 1; i <= pdffile.getNumPages(); i++) {
 PDFPage page = pdffile.getPage(i);
 Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox()
  .getWidth()), ((int) page.getBBox().getHeight()));
 int n = 2;
 /** 图片清晰度(n>0且n<7)【pdf放大参数】 */
 Image img = page.getImage(rect.width * n, rect.height * n,
  rect, /** 放大pdf到n倍,创建图片。 */
  null, /** null for the ImageObserver */
  true, /** fill background with white */
  true /** block until drawing is done */
 );
 BufferedImage tag = new BufferedImage(rect.width * n,
  rect.height * n, BufferedImage.TYPE_INT_RGB);
 tag.getGraphics().drawImage(img, 0, 0, rect.width * n,
  rect.height * n, null);
 /**
  * File imgfile = new File("D:\\work\\mybook\\FilesNew\\img\\" +
  * i + ".jpg"); if(imgfile.exists()){
  * if(imgfile.createNewFile()) { System.out.println("创建图片:"+
  * "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else {
  * System.out.println("创建图片失败!"); } }
  */
 FileOutputStream out = new FileOutputStream(picturepath+"/" + i
  + ".png");
 /** 输出到文件流 */
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
 param2.setQuality(1f, true);
 /** 1f~0.01f是提高生成的图片质量 */
 encoder.setJPEGEncodeParam(param2);
 encoder.encode(tag);
 /** JPEG编码 */
 out.close();
 }
 channel.close();
 raf.close();
 unmap(buf);
 /** 如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法 */
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return countpage;
 
 }
 
 @SuppressWarnings("unchecked")
 public static void unmap(final Object buffer) {
 AccessController.doPrivileged(new PrivilegedAction() {
 public Object run() {
 try {
  Method getCleanerMethod = buffer.getClass().getMethod(
  "cleaner", new Class[0]);
  getCleanerMethod.setAccessible(true);
  sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod
  .invoke(buffer, new Object[0]);
  cleaner.clean();
 } catch (Exception e) {
  e.printStackTrace();
 }
 return null;
 }
 });
 }
}

如果需要将word转pdf,也可参考我上一篇文章。

相关文章

  • Java LocalDateTime获取时间信息、格式化、转换为数字时间戳代码示例

    Java LocalDateTime获取时间信息、格式化、转换为数字时间戳代码示例

    其实我们在Java项目中对日期进行格式化,主要是利用一些日期格式化类,下面这篇文章主要给大家介绍了关于Java LocalDateTime获取时间信息、格式化、转换为数字时间戳的相关资料,需要的朋友可以参考下
    2023-11-11
  • springboot中poi使用操作方法

    springboot中poi使用操作方法

    在项目中,有很多对excel的操作,大都数时候我们都会使用poi工具类,本文将介绍poi的一些使用方法,感兴趣的朋友跟随小编一起看看吧
    2023-08-08
  • 浅析Java中接口和抽象类的七大区别

    浅析Java中接口和抽象类的七大区别

    Java 是一门面向对象的编程语言,面向对象的编程语言有四大特征:抽象、封装、继承和多态。本文介绍的接口和抽象类就是面向对象编程中“抽象”的具体实现。本文也将为大家详细讲一下二者的区别,需要的可以参考一下
    2021-12-12
  • 一文搞懂Java创建线程的五种方法

    一文搞懂Java创建线程的五种方法

    本文主要为大家详细介绍一下Java实现线程创建的五种常见方式,文中的示例代码讲解详细,对我们学习有一定的帮助,感兴趣的可以跟随小编学习一下
    2022-06-06
  • Mybatis查询时数据丢失的问题及解决

    Mybatis查询时数据丢失的问题及解决

    Mybatis查询时数据丢失的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 浅析 ArrayList 和 LinkedList 有什么区别

    浅析 ArrayList 和 LinkedList 有什么区别

    ArrayList 和 LinkedList 有什么区别,是面试官非常喜欢问的一个问题。今天通过本文给大家详细介绍下,感兴趣的朋友跟随小编一起看看吧
    2020-10-10
  • SpringBoot使用Flyway进行数据库迁移的实现示例

    SpringBoot使用Flyway进行数据库迁移的实现示例

    Flyway是一个数据库迁移工具,它提供迁移历史和回滚的功能,本文主要介绍了如何使用Flyway来管理Spring Boot应用程序中的SQL数据库架构,感兴趣的可以了解一下
    2023-08-08
  • Java设计模式中的代理设计模式详细解析

    Java设计模式中的代理设计模式详细解析

    这篇文章主要介绍了Java设计模式中的代理设计模式详细解析,代理模式,重要的在于代理二字,何为代理,我们可以联想到生活中的例子,比如秘书、中介这类职业,我们可以委托中介去帮我们完成某些事情,而我们自己只需要关注我们必须完成的事情,需要的朋友可以参考下
    2023-12-12
  • springboot运行时新增/更新外部接口的实现方法

    springboot运行时新增/更新外部接口的实现方法

    这篇文章主要介绍了springboot运行时新增/更新外部接口的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Mybatis分页插件PageHelper的分页原理剖析

    Mybatis分页插件PageHelper的分页原理剖析

    这篇文章主要介绍了Mybatis分页插件PageHelper的分页原理剖析,PageHelper作为一个启动器,那么就和其他启动器加载一样,先读取spring.factories文件里面配置的类,转成Bean加载本系统中,然后执行他的前置后置处理方法,完成初始化,需要的朋友可以参考下
    2023-08-08

最新评论