java实现pdf按页转换为图片
程序员文章站
2024-03-03 22:05:52
本文实例为大家分享了java实现pdf按页转换为图片的具体代码,供大家参考,具体内容如下
本程序是利用jacob.jar包实现的,关于jacob.jar的配置见我上一篇文...
本文实例为大家分享了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,也可参考我上一篇文章。