国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java使用renderer將pdf按頁轉換為圖片

2024-07-14 08:43:15
字體:
來源:轉載
供稿:網友

項目中遇到了需要把用戶上傳的word,execl,ppt每頁截圖保存。需要先用到jacob把資源轉換為pdf,在通過pdf-renderer把每頁截圖下來。

首先下載相關jar包:下載地址

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; //如果com.sun.image找不到,就是Eclipse默認把這些受訪問限制的API設成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)選為Warning就可以編譯通過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 pdfToImage {   public static void main(String[] args) { String instructiopath="E:/臨時文件1.pdf"; String picturepath = "E:/臨時文件1/"; changePdfToImg(instructiopath,picturepath); }    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轉化成圖片后,釋放MappedByteBuffer資源。調用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;       }     });   } }

成功釋放MappedByteBuffer資源

Method m = FileChannelImpl.class.getDeclaredMethod("unmap",    MappedByteBuffer.class);   m.setAccessible(true);   m.invoke(FileChannelImpl.class, buf);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西昌市| 沧源| 白城市| 万宁市| 北宁市| 黔西| 微山县| 满洲里市| 邳州市| 遵化市| 怀来县| 长乐市| 闽清县| 崇义县| 金山区| 潜江市| 西华县| 休宁县| 晋中市| 饶平县| 武清区| 泗水县| 鹤山市| 石景山区| 桓台县| 施甸县| 平山县| 葵青区| 铜鼓县| 聊城市| 广水市| 仁寿县| 阿勒泰市| 刚察县| 利川市| 通辽市| 铜鼓县| 平远县| 十堰市| 南丹县| 聂拉木县|