介绍
这期内容当中小编将会给大家带来有关如何在java中将PDF文件转换为图片,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
java的特点有哪些
java的特点有哪些 1. java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2. java具有简单性,面向对象,分布式、安全性,平台独立与可移植性,动态性等特点。 3 .使用Java可以编写桌面应用程序,网络应用程序,分布式系统和嵌入式系统应用程序等。
1。首先利用maven引入所需的jar包
& lt; dependency>,,, & lt;才能groupId> org.apache.pdfbox</groupId>,,, & lt;才能artifactId> fontbox</artifactId>,,, & lt;才能version> 2.0.1</version>, 时间/dependency> & lt; & lt; dependency>,, & lt;才能groupId> org.apache.pdfbox</groupId>,, & lt;才能artifactId> pdfbox</artifactId>, & lt;才能version> 2.0.1</version>, & lt;/dependency>
2。这是本人自己写的一个工具类,有两个方法,一个是获取PDF总页码,一个是通过传过来的页面把对应的PDF转成指定格式的图片,并通过流的方式响应给客户端
public class PDFToImgUtil { , ,private static  Logger Logger =, LoggerFactory.getLogger (PDFToImgUtil.class); , ,/* * ,*获取PDF总页数 ,* @throws IOException ,*/,public static  int getPDFNum (String fileUrl), throws IOException { ,PDDocument PDDocument =,空; ,int pages =, 0; ,try { 时间=pdDocument 才能;getPDDocument (fileUrl); 时间=pages 才能;pdDocument.getNumberOfPages (); ,}catch (Exception e), { e.printStackTrace才能(); ,,logger.error (e.getMessage (), e); ,}finally { if 才能;(pdDocument !=, null), { pdDocument.close才能(); ,,} ,} ,return 页; ,} , ,/* * ,* PDF转图片,根据页码一页一页转, ,* @throws IOException ,* imgType:转换后的图片类型,jpg, png ,*/,public static  void PDFToImg (OutputStream sos, String fileUrl, int 页面,String imgType), throws IOException { ,PDDocument PDDocument =,空; ,/* dpi越大转换后越清晰,相对转换速度越慢,*/,int dpi =, 100; ,try { 时间=pdDocument 才能;getPDDocument (fileUrl); PDFRenderer 才能;renderer =, new PDFRenderer (pdDocument); int 才能;pages =, pdDocument.getNumberOfPages (); if 才能;(page & lt;=, pages ,,, page 祝辞,0),{ BufferedImage 才能;image =, renderer.renderImageWithDPI(页面,dpi); ImageIO.write才能(图像,imgType,, sos); ,,} ,}catch (Exception e), { e.printStackTrace才能(); ,,logger.error (e.getMessage (), e); ,}finally { if 才能;(pdDocument !=, null), { pdDocument.close才能(); ,,} ,} , ,} , , ,private static  PDDocument getPDDocument (String fileUrl), throws IOException { ,File File =, new 文件(fileUrl); ,FileInputStream inputStream =, new FileInputStream(文件); ,,return PDDocument.load (inputStream); ,} , }
上述就是小编为大家分享的如何在java中将PDF文件转换为图片了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。