温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用java怎么将pdf按页转换为图片

发布时间:2021-02-22 15:57:27 来源:亿速云 阅读:251 作者:Leah 栏目:编程语言

本篇文章为大家展示了使用java怎么将pdf按页转换为图片,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

Java可以用来干什么

Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。

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;
 }
 });
 }
}

上述内容就是使用java怎么将pdf按页转换为图片,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI