java怎么實(shí)現(xiàn)pdf文件截圖的方法-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)java怎么實(shí)現(xiàn)pdf文件截圖的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供城固網(wǎng)站建設(shè)、城固做網(wǎng)站、城固網(wǎng)站設(shè)計(jì)、城固網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、城固企業(yè)網(wǎng)站模板建站服務(wù),十年城固做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

具體如下:

最近做的一個(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");

關(guān)于“java怎么實(shí)現(xiàn)pdf文件截圖的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)頁名稱:java怎么實(shí)現(xiàn)pdf文件截圖的方法-創(chuàng)新互聯(lián)
當(dāng)前地址:http://muchs.cn/article16/dshogg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化