• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java实现pdf按页转换为图片

java 搞代码 4年前 (2022-01-05) 71次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了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 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-600"); if(imgfile.exists()){ * if(imgfile.createNewFile()) { System.out.println("创建图片:"+ * "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg-600"); } else { * System.out.println("创建图片失败!"); } } */ FileOutputStream out = new FileOutputStream(picturepath+"/" + i + ".png-600"); /** 输出到文件流 */ 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); /<mark style="color:transparent">来源gaodaimacom搞#^代%!码&网</mark>** 如果要在转图片之后删除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实现pdf按页转换为图片的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java实现pdf按页转换为图片
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址