gitee java pdf转图片_java 将PDF 转化为图片

packagecom.mybook.demo;

importjava.awt.Image;

importjava.awt.Rectangle;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.RandomAccessFile;

importjava.lang.reflect.Method;

importjava.nio.MappedByteBuffer;

importjava.nio.channels.FileChannel;

importjava.security.AccessController;

importjava.security.PrivilegedAction;

importcom.sun.image.codec.jpeg.JPEGCodec;

importcom.sun.image.codec.jpeg.JPEGEncodeParam;

importcom.sun.image.codec.jpeg.JPEGImageEncoder;

importcom.sun.pdfview.PDFFile;

importcom.sun.pdfview.PDFPage;

publicclassPDFchangToImage {

publicstaticvoidmain(String[] args){

PDFchangToImage.changePdfToImg();

}

privatestaticvoidchangePdfToImg() {

try{

File file = newFile("D:\\work\\mybook\\Files\\深入浅出General System V3.3.pdf");

RandomAccessFile raf = newRandomAccessFile(file,"r");

FileChannel channel = raf.getChannel();

MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

PDFFile pdffile = newPDFFile(buf);

for(inti =1; i <= pdffile.getNumPages(); i++) {

PDFPage page = pdffile.getPage(i);

Rectangle rect = newRectangle(0,0, ((int) page.getBBox().getWidth()), ((int) page.getBBox().getHeight()));

Image img = page.getImage(rect.width, rect.height, rect,

null,// null for the ImageObserver

true,// fill background with white

true// block until drawing is done

);

BufferedImage tag = newBufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(img, 0,0, rect.width, rect.height,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 = newFileOutputStream("D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg");// 输出到文件流

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);

param2.setQuality(1f, false);// 1f是提高生成的图片质量

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

}

}

privatestaticvoidunmap(finalObject buffer) {

// TODO Auto-generated method stub

AccessController.doPrivileged(newPrivilegedAction() {

publicObject run() {

try{

Method getCleanerMethod = buffer.getClass().getMethod("cleaner",newClass[0]);

getCleanerMethod.setAccessible(true);

sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, newObject[0]);

cleaner.clean();

} catch(Exception e) {

e.printStackTrace();

}

returnnull;

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值