欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何使用java實現(xiàn)截取PDF指定頁并進(jìn)行圖片格式轉(zhuǎn)換功能

這篇文章給大家分享的是有關(guān)如何使用java實現(xiàn)截取PDF指定頁并進(jìn)行圖片格式轉(zhuǎn)換功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

10年積累的成都網(wǎng)站設(shè)計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有淅川免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1、引入依賴

<dependency>  <groupId>org.apache.pdfbox</groupId>  <artifactId>pdfbox</artifactId>  <version>2.0.16</version></dependency><dependency>  <groupId>org.apache.pdfbox</groupId>  <artifactId>fontbox</artifactId>  <version>2.0.16</version></dependency>

jar包下載地址:

https://mvnrepository.com/artifact/org.apache.pdfbox/pdfboxhttps://mvnrepository.com/artifact/org.apache.pdfbox/fontbox

2、實現(xiàn)DEMO

package com.dddpeter.app;import org.apache.pdfbox.multipdf.Splitter;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;import java.awt.image.BufferedImage;import java.io.*;import java.util.List;import java.util.ListIterator;public class PDFUtils {  public static String splitPdf(int pageNum, String source, String dest) {    File indexFile = new File(source);    File outFile = new File(dest);    PDDocument document = null;    try {      document = PDDocument.load(indexFile);      // document.getNumberOfPages();      Splitter splitter = new Splitter();      splitter.setStartPage(pageNum);      splitter.setEndPage(pageNum);      List<PDDocument> pages = splitter.split(document);      ListIterator<PDDocument> iterator = pages.listIterator();      while (iterator.hasNext()) {        PDDocument pd = iterator.next();        if (outFile.exists()) {          outFile.delete();        }        pd.save(outFile);        pd.close();        if (outFile.exists()) {          return outFile.getPath();        }      }      document.close();    } catch (IOException e) {      e.printStackTrace();    } catch (Exception e) {      e.printStackTrace();    }    return null;  }  public static void pdfFileToImage(File pdffile,String targetPath){    try {      FileInputStream instream = new FileInputStream(pdffile);      InputStream byteInputStream=null;      try {        PDDocument doc = PDDocument.load(instream);        PDFRenderer renderer = new PDFRenderer(doc);        int pageCount = doc.getNumberOfPages();        if (pageCount > 0) {          BufferedImage image = renderer.renderImage(0, 4.0f);          image.flush();          ByteArrayOutputStream bs = new ByteArrayOutputStream();          ImageOutputStream imOut;          imOut = ImageIO.createImageOutputStream(bs);          ImageIO.write(image, "png", imOut);          byteInputStream = new ByteArrayInputStream(bs.toByteArray());          byteInputStream.close();        }        doc.close();      }      catch (IOException e) {        e.printStackTrace();      }      File uploadFile = new File(targetPath);      FileOutputStream fops;      fops = new FileOutputStream(uploadFile);      fops.write(readInputStream(byteInputStream));      fops.flush();      fops.close();    }    catch (Exception e) {      e.printStackTrace();    }  }  public static byte[] readInputStream(InputStream inStream) throws Exception {    ByteArrayOutputStream outStream = new ByteArrayOutputStream();    byte[] buffer = new byte[1024];    int len = 0;    while ((len = inStream.read(buffer)) != -1) {      outStream.write(buffer, 0, len);    }    inStream.close();    return outStream.toByteArray();  }  public static void main(String[] args) {    String path = splitPdf(4,"D:\\data\\11.pdf","D:\\data\\out11.pdf");    File file =new File(path);    //上傳的是png格式的圖片結(jié)尾    String targetfile="D:\\data\\out11.png";    pdfFileToImage(file,targetfile);  }}

感謝各位的閱讀!關(guān)于“如何使用java實現(xiàn)截取PDF指定頁并進(jìn)行圖片格式轉(zhuǎn)換功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

名稱欄目:如何使用java實現(xiàn)截取PDF指定頁并進(jìn)行圖片格式轉(zhuǎn)換功能
分享URL:http://chinadenli.net/article38/gpjepp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站建設(shè)、軟件開發(fā)App設(shè)計

廣告

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

成都定制網(wǎng)站網(wǎng)頁設(shè)計