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

首頁(yè) > 開(kāi)發(fā) > Java > 正文

java實(shí)現(xiàn)pdf文件截圖的方法【附PDFRenderer.jar下載】

2024-07-13 10:16:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了java實(shí)現(xiàn)pdf文件截圖的方法。分享給大家供大家參考,具體如下:

最近做的一個(gè)網(wǎng)站中,有個(gè)需求是上傳pdf文件,顯示pdf的封頁(yè),點(diǎn)擊封頁(yè)之后進(jìn)行在線閱讀,這里使用的是PDFRender對(duì)pdf進(jìn)行截圖。

public static boolean createScreenShoot(String source, String target) {    File file = new File(source);    if (!file.exists()) {      System.err.println("路徑[" + source + "]對(duì)應(yīng)的pdf文件不存在!");      return false;    }    try{      RandomAccessFile raf = new RandomAccessFile(file, "r");      FileChannel channel = raf.getChannel();      ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());      PDFFile pdffile = new PDFFile(buf);      int num = pdffile.getNumPages();      for(int i = 1; i < num; i++){         PDFPage page = pdffile.getPage(1);          // get the width and height for the doc at the default zoom          Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()              .getWidth(), (int) page.getBBox().getHeight());          // generate the image          Image img = page.getImage(rect.width, rect.height, // width &              rect, // clip rect              null, // null for the ImageObserver              true, // fill background with white              true // block until drawing is done              );          BufferedImage tag = new BufferedImage(rect.width, rect.height,   BufferedImage.TYPE_INT_RGB);          tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,null);          FileOutputStream out = new FileOutputStream(target+i+"jpg");          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);          encoder.encode(tag); // JPEG編碼          out.close();      }      return true;    }catch(Exception e){      e.printStackTrace();       return true;    }

另外如果需要在線顯示pdf的話,需要設(shè)置響應(yīng)頭

response.setContentType("application/pdf");

附:pdfRender.jar點(diǎn)擊此處本站下載。

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 喀喇| 莆田市| 宕昌县| 泰兴市| 祥云县| 安福县| 靖宇县| 鸡泽县| 疏勒县| 崇州市| 克拉玛依市| 新安县| 社会| 丰城市| 常山县| 泸水县| 贺兰县| 邹城市| 通山县| 南木林县| 安龙县| 甘谷县| 简阳市| 碌曲县| 南昌市| 平罗县| 屏东县| 商河县| 门头沟区| 长寿区| 嵊州市| 专栏| 庆云县| 乌审旗| 福泉市| 维西| 女性| 乳源| 安化县| 玛曲县| 沾益县|