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并发中的Fork/Join 框架机制详解

    Java并发中的Fork/Join 框架机制详解

    本文主要介绍了 Java 并发框架中的 Fork/Join 框架的基本原理和其使用的工作窃取算法(work-stealing)、设计方式和部分实现源码,感兴趣的朋友跟随小编一起看看吧
    2021-07-07
  • SpringMVC配置与使用详细介绍

    SpringMVC配置与使用详细介绍

    Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过把Model,View,Controller分离,将web层进行职责解耦,把复杂的web应用分成逻辑清晰的几部分,简化开发,减少出错,方便组内配合
    2022-07-07
  • SpringBoot中的自定义Starter解读

    SpringBoot中的自定义Starter解读

    这篇文章主要介绍了SpringBoot中的自定义Starter解读,启动器模块其实是一个空的jar文件,里面没有什么类、接口,仅仅是提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库,需要的朋友可以参考下
    2023-12-12
  • Maven修改运行环境配置代码实例

    Maven修改运行环境配置代码实例

    这篇文章主要介绍了Maven修改运行环境配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 一篇文章带你入门java泛型

    一篇文章带你入门java泛型

    这篇文章主要介绍了java泛型基础知识及通用方法,从以下几个方面介绍一下java的泛型: 基础, 泛型关键字, 泛型方法, 泛型类和接口,感兴趣的可以了解一下
    2021-08-08
  • Spring Cloud @RefreshScope 原理及使用

    Spring Cloud @RefreshScope 原理及使用

    这篇文章主要介绍了Spring Cloud @RefreshScope 原理及使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

    MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

    这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • Java Volatile关键字实现原理过程解析

    Java Volatile关键字实现原理过程解析

    这篇文章主要介绍了Java Volatile关键字实现原理过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • nacos修改druid配置自动刷新后服务异常的问题

    nacos修改druid配置自动刷新后服务异常的问题

    这篇文章主要介绍了nacos修改druid配置自动刷新后服务异常的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-06-06
  • springBoot项目中使用@Value取值出现的问题及解决

    springBoot项目中使用@Value取值出现的问题及解决

    这篇文章主要介绍了springBoot项目中使用@Value取值出现的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论