java中pdf轉(zhuǎn)圖片的實現(xiàn)方法
JAVA中實現(xiàn)pdf轉(zhuǎn)圖片可以通過第三方提供的架包,這里介紹幾種常用的,可以根據(jù)自身需求選擇使用。
一、icepdf。有收費版和開源版,幾種方法里最推薦的。轉(zhuǎn)換的效果比較好,能識別我手頭文件中的中文,就是轉(zhuǎn)換后可能字體的關(guān)系部分字間距有點寬。因為,字體支持是要收費的,所以轉(zhuǎn)換的圖片會帶有官方的水印。去水印的方法可以查看另一篇文章:icepdf去水印方法
1、下載icepdf的架包,并導(dǎo)入項目中,這里用到4個,如下:
2、附上代碼例子:
String filePath = "c:/test.pdf"; Document document = new Document(); document.setFile(filePath); float scale = 2.5f;//縮放比例 float rotation = 0f;//旋轉(zhuǎn)角度 for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale); RenderedImage rendImage = image; try { File file = new File("c:/iecPDF_" + i + ".png"); ImageIO.write(rendImage, "png", file); 14 } catch (IOException e) { e.printStackTrace(); } image.flush(); } document.dispose();
例子中是pdf轉(zhuǎn)png格式的,也可以將12、13行改成jpg,轉(zhuǎn)出jpg格式的,但是從轉(zhuǎn)換效果來看png的清晰度會相對較高。有個小技巧是12行改成jpg,但13行使用png,也就是轉(zhuǎn)換成jpg格式但有png清晰度的圖片。
二、pdfbox。轉(zhuǎn)換效果還可以,能識別我手中文件大部分內(nèi)容,有部分內(nèi)容無法識別。
1、下載pdfbox的架包,并導(dǎo)入項目,這里用到2個,如下:
2、附上代碼例子:
File file = new File("c:\\test.pdf"); try { PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); 5 int pageCount = doc.getNumberOfPages(); for(int i=0;i<pageCount;i++){ BufferedImage image = renderer.renderImageWithDPI(i, 296); // BufferedImage image = renderer.renderImage(i, 2.5f); ImageIO.write(image, "PNG", new File("C:\\pdfbox_image.png")); } } catch (IOException e) { e.printStackTrace(); }
例子中rederImageWithDPI的第二個參數(shù)為dpi分辨率單位,可根據(jù)需求調(diào)節(jié)大小,代碼第八行提供了架包里另一種轉(zhuǎn)圖片的方法,第二個參數(shù)為縮放比。
三、jpedal。效果不太理想,貌似對中文支持不太好,下面的lgpl版本是開源版。
1、下載jpedal的架包,并導(dǎo)入項目中,如下:
2、附上代碼例子:
PdfDecoder decode_pdf = new PdfDecoder(true); try { decode_pdf.openPdfFile("c:\\test.pdf"); //file // decode_pdf.openPdfFile("C:/jpedalPDF.pdf", "password"); //encrypted file // decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF // decode_pdf.openPdfFileFromURL("http://www.mysite.com/jpedalPDF.pdf",false); // decode_pdf.openPdfFileFromInputStream(in, false); int start = 1, end = decode_pdf.getPageCount(); for(int i = start; i < end+1; i++){ BufferedImage img=decode_pdf.getPageAsImage(i); try { ImageIO.write(img, "png", new File("C:\\jpedal_image.png")); } catch (IOException e) { e.printStackTrace(); } } decode_pdf.closePdfFile(); } catch (PdfException e) { e.printStackTrace(); }
例子的3-7行還提供了幾種不同的pdf打開方式,可以根據(jù)自己的需要選擇使用。
以上這篇java中pdf轉(zhuǎn)圖片的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java中static關(guān)鍵字的用法
這篇文章主要介紹了關(guān)于Java中static關(guān)鍵字的用法,static:意為靜態(tài)的,在?Java?里面作為靜態(tài)修飾符,可以理解為全局的意思,static?不僅可以修飾成員變量,成員方法,還可以修飾代碼塊,需要的朋友可以參考下2023-08-08Spring?MVC中@Controller和@RequestMapping注解使用
這篇文章主要介紹了Spring?MVC中@Controller和@RequestMapping注解使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Redis6搭建集群并在SpringBoot中使用RedisTemplate的實現(xiàn)
本文主要介紹了Redis6搭建集群并在SpringBoot中使用RedisTemplate,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04SpringBoot集成SpringSecurity和JWT做登陸鑒權(quán)的實現(xiàn)
這篇文章主要介紹了SpringBoot集成SpringSecurity和JWT做登陸鑒權(quán)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04SpringBoot整合kafka遇到的版本不對應(yīng)問題及解決
這篇文章主要介紹了SpringBoot整合kafka遇到的版本不對應(yīng)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03