介绍
本篇文章为大家展示了使用java怎么将pdf按页转换为图片,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
java可以用来干什么
java主要应用于:1。网页开发;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 =癉:/instructio/2015-05-16/Android 4编程入门经典.pdf" ,//picturepath =,“D:/instructio/图片/2015-05-16/?; , ,File File =, new 文件(instructiopath); ,RandomAccessFile raf =, new RandomAccessFile(文件,,“r"); ,FileChannel channel =, raf.getChannel (); ,MappedByteBuffer buf =, channel.map (FileChannel.MapMode.READ_ONLY, ,,0,channel.size ()); ,PDFFile PDFFile =, new PDFFile (buf); ,//创建图片文件夹 ,File dirfile =, new 文件(picturepath); 如果才能(! dirfile.exists ()) { dirfile.mkdirs才能(); ,} ,//获得图片页数=,,countpage  pdffile.getNumPages (); ,for (int 小姐:=,1;,小姐:& lt;=, pdffile.getNumPages();,我+ +),{ ,PDFPage page =, pdffile.getPage(我); ,Rectangle rect =, new 矩形(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, 矩形才能,,/* *,放pdf到大n倍,创建图片只*/空,才能,/* *,null for 从而ImageObserver */,,真的,,/* *,fill background with white */true 才能;/* *,block until 管理;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 文件(“D: \ \ \ \ mybook \ \ FilesNew \ \ img \ \“, + *,才能小姐:+,“.jpg");,如果(imgfile.exists ()) { *才能,如果(imgfile.createNewFile ()), {, System.out.println(“创建图片:“+ *,才能“D: \ \ \ \ mybook \ \工作FilesNew \ \ img \ \“, +,小姐:+,“.jpg");,}, {else *,才能System.out.println(“创建图片失败!“),,},} ,*/,FileOutputStream out =, new FileOutputStream (picturepath +“/? +,我 +,才能“.png"); ,/* *输出到文件流,*/,JPEGImageEncoder encoder =, JPEGCodec.createJPEGEncoder(出); ,JPEGEncodeParam param2 =, encoder.getDefaultJPEGEncodeParam(标签); ,param2.setQuality (1 f,,真的); ,/* * 1 ~ 0.01 f是提高生成的图片质量,*/,encoder.setJPEGEncodeParam (param2); ,encoder.encode(标签); ,/* * JPEG编码,*/,out.close (); ,} ,channel.close (); ,raf.close (); ,映射(buf); ,/* *如果要在转图片之后删除pdf、就必须要这个关闭流和清空缓冲的方法,*/,}catch (FileNotFoundException e), { ,e.printStackTrace (); ,}catch (IOException e), { ,e.printStackTrace (); ,} ,return countpage; , ,} , ,@SuppressWarnings (“unchecked") ,public static  void 映射(final Object 缓冲区),{ ,AccessController.doPrivileged (new PrivilegedAction (), { ,public Object  run (), { ,try { Method 才能;getCleanerMethod =, buffer.getClass () .getMethod ( “cleaner",才能,new 类[0]); getCleanerMethod.setAccessible才能(真正的); null null null null null null null null null null null使用java怎么将pdf按页转换为图片