天天看點

Java-截取PDF中的某一頁作為縮略圖

目錄

Apache PDFBox是一個開源Java庫,支援PDF文檔的開發和轉換。

引入依賴

業務代碼

讀取網絡中的PDF檔案與檔案類型轉換

如果本篇部落格對您有一定的幫助,大家記得留言+點贊+收藏哦。

Apache PDFBox是一個開源Java庫,支援PDF文檔的開發和轉換。

有以下中有功能 -
  • Extract Text - 使用PDFBox,您可以從PDF檔案中提取Unicode文本。
  • Split & Merge - 使用PDFBox,您可以将單個PDF檔案分成多個檔案,并将它們合并為一個檔案。
  • Fill Forms - 使用PDFBox,您可以在文檔中填寫表單資料。
  • Print - 使用PDFBox,您可以使用标準Java列印API列印PDF檔案。
  • Save as Image - 使用PDFBox,您可以将PDF儲存為圖像檔案,如PNG或JPEG。
  • Create PDFs - 使用PDFBox,您可以通過建立Java程式建立新的PDF檔案,還可以包含圖像和字型。
  • Signing - 使用PDFBox,您可以将數字簽名添加到PDF檔案。

 有一個教程對PDFBox的介紹很詳細,這裡不再多說。

PDFBox - 快速指南_學習PDFbox|WIKI教程此方法接受檔案對象作為參數,因為這是一個靜态方法,您可以使用類名調用它,如下所示。. 此方法接受檔案對象作為參數,因為這是一個靜态方法,您可以使用類名調用它,如下所示。. 此方法接受檔案對象作為參數,因為這是一個靜态方法,您可以使用類名調用它,如下所示。. 此方法接受檔案對象作為參數,因為這是一個靜态方法,您可以使用類名調用它,如下所示。. 此方法接受檔案對象作為參數,因為這是一個靜态方法,您可以使用類名調用它,如下所示。

Java-截取PDF中的某一頁作為縮略圖

https://iowiki.com/pdfbox/pdfbox_quick_guide.html

引入依賴

<!--start:PDF擷取第一頁的圖檔-->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.20</version>
</dependency>
<!--end:PDF擷取第一頁的圖檔-->
           

業務代碼

/**
 * 截取PDF中的某一頁作為縮略圖,并上傳(儲存)
 * @param pdfFileName
 * @return
 */
public  String PDFFramer(String pdfFileName){

    //将網絡中的PDF檔案轉換成file
    File file = URLToFile(pdfFileName);
    //new File() 隻能通路本地檔案
    //将本地檔案轉換成file
    //File file = new File("C:\\Users\\Administrator\\Downloads\\(重要必看).pdf");

    String pdfUrl="";
    try
    {
        // 打開來源 pdf
        log.info("開始截取PDF:");
        //PDDocument類的load()方法用于加載現有PDF文檔
        PDDocument pdfDocument = PDDocument.load(file);
        //PDFRenderer的類将PDF文檔呈現為AWT BufferedImage 
        PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);

        // 提取的頁碼
        int pageNumber = 0;
        // 以300 dpi 讀取存入 BufferedImage 對象
        int dpi = 300;
       //Renderer類的renderImage()方法在特定頁面中渲染圖像
        BufferedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
        // 檔案類型轉換
        MultipartFile multipartFile = fileCase(buffImage);
        log.info("PDF開始上傳:");
        pdfUrl = fileLoad(multipartFile);
        log.info("PDF上傳成功:{}",pdfUrl);

        // 關閉文檔
        pdfDocument.close();
        //删除臨時檔案
        String s = threadLocal.get();
        log.info("臨時檔案的目錄:"+s);

        File f=new File(s);
        boolean delete = f.delete();
        log.info("檔案是否删除"+delete);

    }
    catch (InvalidPasswordException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return pdfUrl;
}
           

讀取網絡中的PDF檔案與檔案類型轉換

/**
 * 讀取網絡中的PDF檔案
 * @param url
 * @return
 */
public  File URLToFile(String url){
    log.info("讀取FastDFS上的PDF");
    //儲存臨時檔案--jar包的相對位置
    File file1 = new File("Temporary.pdf");
    try {

        URL url1 = new URL(url);
        FileUtils.copyURLToFile(url1,file1);

    } catch (IOException e) {
        e.printStackTrace();
    }
    File absoluteFile = file1.getAbsoluteFile();
    threadLocal.set(absoluteFile.toString());
    log.info("ppt已經存儲到本地"+absoluteFile.toString());
    return file1;
}



/**
 * 檔案轉換将BufferedImage轉換成MultipartFile:為了檔案上傳
 * @param image
 * @return
 */
public static MultipartFile fileCase(BufferedImage image){
    //得到BufferedImage對象
   // BufferedImage bufferedImage = JoinTwoImage.testEncode(200, 200, url);
    MultipartFile multipartFile= null;
    try {
        //建立一個ByteArrayOutputStream
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //把BufferedImage寫入ByteArrayOutputStream
        ImageIO.write(image, "jpg", os);
        //ByteArrayOutputStream轉成InputStream
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        //InputStream轉成MultipartFile
        multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input);
    } catch (IOException e) {
        e.printStackTrace();
    }
return multipartFile;

}
           

如果本篇部落格對您有一定的幫助,大家記得留言+點贊+收藏哦。