欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年01月11日 14:43:34   作者:yangnianbing110  
這篇文章主要介紹了java實(shí)現(xiàn)pdf文件截圖的方法,結(jié)合實(shí)例形式分析了java基于PDFRenderer.jar進(jìn)行pdf文件截圖的相關(guān)操作技巧,并附帶PDFRenderer.jar文件供讀者下載使用,需要的朋友可以參考下

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

最近做的一個(gè)網(wǎng)站中,有個(gè)需求是上傳pdf文件,顯示pdf的封頁,點(diǎn)擊封頁之后進(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)擊此處本站下載

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。

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

相關(guān)文章

最新評(píng)論