java實(shí)現(xiàn)pdf文件截圖的方法【附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)文章
springboot的jar能獨(dú)立運(yùn)行的原因解析
這篇文章主要介紹了springboot的jar能獨(dú)立運(yùn)行的原因解析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12MyEclipse+Tomcat+MAVEN+SVN項(xiàng)目完整環(huán)境搭建(圖文教程)
這篇文章主要介紹了MyEclipse+Tomcat+MAVEN+SVN項(xiàng)目完整環(huán)境搭建(圖文教程),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法
本篇文章介紹了,Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法。需要的朋友參考下2013-05-05Java中@RequiredArgsConstructor注解的基本用法
這篇文章主要介紹了Java中@RequiredArgsConstructor注解的基本用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09java中double轉(zhuǎn)化為BigDecimal精度缺失的實(shí)例
下面小編就為大家?guī)硪黄猨ava中double轉(zhuǎn)化為BigDecimal精度缺失的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03一分鐘掌握J(rèn)ava?Quartz定時(shí)任務(wù)
這篇文章主要為大家介紹了Java?Quartz定時(shí)任務(wù)一分鐘掌握教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05SpringBoot整合JWT的實(shí)現(xiàn)示例
JWT是目前比較流行的跨域認(rèn)證解決方案,本文主要介紹了SpringBoot整合JWT的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01