PDF转换成图片(Java pdfbox实现)

pdf转成图片在java中主流的做法就是pdfbox和icepdf
然而我选择pdfbox

废话不多说上代码

引入依赖

        <!--pdfbox--><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.13</version></dependency>

导出工具类

/*** pdf操作工具类* @author chao11.lei*/
public class PdfUtils {//经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大,一般电脑显示分辨率为96public static final float DEFAULT_DPI = 144;//默认转换的图片格式为jpgpublic static final String DEFAULT_FORMAT = "jpg";public final static Logger logger = LoggerFactory.getLogger(PdfUtils.class);private static final String PROPERTY_KEY = "sun.java2d.cmm";private static final String PROPERTY_VALUE = "sun.java2d.cmm.kcms.KcmsServiceProvider";/*** pdf转图片** @param pdfPath PDF路径* @return 图片路径*/public static void pdfToImage(String pdfPath, String imgPath) {try {logger.debug(">>处理开始");System.setProperty(PROPERTY_KEY, PROPERTY_VALUE);//图像合并使用参数// 总宽度int width = 0;// 保存一张图片中的RGB数据int[] singleImgRGB;int shiftHeight = 0;//保存每张图片的像素值BufferedImage imageResult = null;File pdfFile = new File(pdfPath);//利用PdfBox生成图像PDDocument pdDocument = PDDocument.load(pdfFile);PDFRenderer renderer = new PDFRenderer(pdDocument);//生成目录String fileRealName = pdfFile.getName().replace(".pdf","");File fileDir = new File(imgPath + "/" +fileRealName);if(!fileDir.exists()) {fileDir.mkdirs();}//循环每个页码for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);int imageHeight = image.getHeight();int imageWidth = image.getWidth();//计算高度和偏移量//使用第一张图片宽度;width = imageWidth;//保存每页图片的像素值imageResult = new BufferedImage(width, imageHeight , BufferedImage.TYPE_INT_RGB);singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);// 写入流中imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);// 写图片ImageIO.write(imageResult, DEFAULT_FORMAT, new File(imgPath  +fileRealName+"/" +fileRealName + "_" + (i +1)+ ".jpg"));if(i%10==0||i+1==len) {logger.debug("处理进度:{}/{}", i + 1, len);}}pdDocument.close();logger.debug("处理结束>>");} catch (Exception e) {logger.error("PDF转图片失败:{}",e);e.printStackTrace();}}public static void pdfToImage(File pdfFile, String imgPath) {try {logger.debug(">>处理开始");System.setProperty(PROPERTY_KEY, PROPERTY_VALUE);//图像合并使用参数// 总宽度int width = 0;// 保存一张图片中的RGB数据int[] singleImgRGB;int shiftHeight = 0;//保存每张图片的像素值BufferedImage imageResult = null;//利用PdfBox生成图像PDDocument pdDocument = PDDocument.load(pdfFile);PDFRenderer renderer = new PDFRenderer(pdDocument);//生成目录String fileRealName = pdfFile.getName().replace(".pdf","");File fileDir = new File(imgPath + "/" +fileRealName);if(!fileDir.exists()) {fileDir.mkdirs();}//循环每个页码for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);int imageHeight = image.getHeight();int imageWidth = image.getWidth();//计算高度和偏移量//使用第一张图片宽度;width = imageWidth;//保存每页图片的像素值imageResult = new BufferedImage(width, imageHeight , BufferedImage.TYPE_INT_RGB);singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);// 写入流中imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);// 写图片ImageIO.write(imageResult, DEFAULT_FORMAT, new File(imgPath  +fileRealName+"/" +fileRealName + "_" + (i +1)+ ".jpg"));if(i%10==0||i+1==len) {logger.debug("处理进度:{}/{}", i + 1, len);}}pdDocument.close();logger.debug("处理结束>>");} catch (Exception e) {logger.error("PDF转图片失败:{}",e);e.printStackTrace();}}public static void pdfToImage(InputStream pdfFileInputStream, String fileName,String imgPath) {try {logger.debug(">>处理开始");System.setProperty(PROPERTY_KEY, PROPERTY_VALUE);//图像合并使用参数// 总宽度int width = 0;// 保存一张图片中的RGB数据int[] singleImgRGB;int shiftHeight = 0;//保存每张图片的像素值BufferedImage imageResult = null;//利用PdfBox生成图像PDDocument pdDocument = PDDocument.load(pdfFileInputStream);PDFRenderer renderer = new PDFRenderer(pdDocument);//生成目录String fileRealName = fileName.replace(".pdf","");File fileDir = new File(imgPath + "/" +fileRealName);if(!fileDir.exists()) {fileDir.mkdirs();}//循环每个页码for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);int imageHeight = image.getHeight();int imageWidth = image.getWidth();//计算高度和偏移量//使用第一张图片宽度;width = imageWidth;//保存每页图片的像素值imageResult = new BufferedImage(width, imageHeight , BufferedImage.TYPE_INT_RGB);singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);// 写入流中imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);// 写图片ImageIO.write(imageResult, DEFAULT_FORMAT, new File(imgPath  +fileRealName+"/" +fileRealName + "_" + (i +1)+ ".jpg"));if(i%10==0||i+1==len) {logger.debug("处理进度:{}/{}", i + 1, len);}}pdDocument.close();logger.debug("处理结束>>");} catch (Exception e) {logger.error("PDF转图片失败:{}",e);e.printStackTrace();}}
}

方法调试过很多次,绝对好用
支持多种方式转换

  • 路径文件
  • File文件对象
  • 输入流

测试

@Testvoid testPdfToImg() {PdfUtils.pdfToImage("D:\\img\\test.pdf", "D:\\img\\");}

这里我发现在部分pc上不存在STsong-light字体且不存在可替换字体就会有警告,同时导出的文件就会乱码。
这里我也没找到STsong-light 字体,但是我找到可以替换的字体 AdobeFangsongStd-Regular

这是adobe的一款仿宋字体
下载链接,我就不放在下载专栏,骗大家的c币啦
http://yun.dlblog.club/f/8f02d1b05cf04dc682bb/

PDF转换成图片(Java pdfbox实现 附中文乱码解决方案)相关推荐

  1. 使用pdfbox将pdf转换成图片的时候,STSong-Light字体不存在解决方案

    问题 使用pdfbox将pdf转换成图片的时候,STSong-Light字体的文字全为空格问题解决. 告警信息:Using fallback MT-Extra for CID-keyed font S ...

  2. 纯java pdf转换成html,java pdf转换html代码

    java pdf转换html代码 [2021-02-03 00:56:42]  简介: php去除nbsp的方法:首先创建一个PHP代码示例文件:然后通过"preg_replace(&quo ...

  3. C#技术分享【PDF转换成图片——13种方案】

    1.[O2S.Components.PDFRender4NET.dll],第三方DLL,可以实现PDF转图片,支持32位系统.64位系统 官方试用版的dll左上角会有一排红色水印,下面这个是破解版的没 ...

  4. 怎么把PDF转换成图片?这几种转换方法都可以做到

    怎么把PDF文件转换成图片呢?大家在使用PDF文件的时候,很多情况下不仅是用来发送文件,还会用来阅读文件,如果只需要浏览文件中某部分内容的话,远没有图片使用的方便,我们可以将这部分内容转换成图片来更加 ...

  5. pdf转换成jpg python_【PDF转图片】如何将PDF转换成图片格式--Python

    可能会有人问,很多PDF阅读软件都是可以直接将PDF文件保存成图片格式的,为什么非得用Python?? 使用Python(代码)的优势就在于可以批量操作,试想,如果你有成百上千的PDF文件,你很有可能 ...

  6. 将pdf转换成图片在转换回pdf文件

    将pdf转换成图片在转换回pdf文件 之所有要这么做主要是曲线解决pdf文本内容增加覆盖层后依然可以复制出来的问题.查了不少资料发现通过覆盖层的方式并不能去除原有的文字内容,只是达到了掩耳盗铃的效果而 ...

  7. 使用imagick将PDF转换成图片时报Fatal error: Uncaught exception 'ImagickException' with message 'FailedToExecute

    个人博客原文地址:http://www.lampnick.com/php/720 $IM = new imagick(); $IM->setResolution(200, 200); $IM-& ...

  8. android开发将h5转换成pdf_如何将PDF转换成图片?搞定PDF格式转换,就用这招就够了!...

    原标题:如何将PDF转换成图片?搞定PDF格式转换,就用这招就够了! 在我们日常学习和日常工作中,我们有时候会遇到要使用到PDF中某一部分的情况,这时候我们可以将PDF文件转成图片.有什么办法可以把P ...

  9. 怎么把pdf转换成图片?这个方法你值得拥有

    想要高效率的工作,除了需要大家合理安排时间之外,一些能够辅助高效工作的工具也是必不可少的.就拿要把一份pdf文件转换成若干图片来说,如果不知道方法,找不到合适的转换工具,那么想要完成这一任务,势必要花 ...

最新文章

  1. tkinter安装_mac pyenv 安装tkinter,解决tkinter环境的问题
  2. jQuery / JavaScript:访问iframe的内容
  3. 【TensorFlow】笔记5:图像数据处理
  4. java分布式api网管关,分布式04-Spring Cloud Zuul Api网关 一
  5. Verizon发布2010年数据破坏调查报告
  6. C/C++学习之路: 模板和异常
  7. WPF Application 类介绍以及怎样修改启动方式
  8. uniapp动态修改样式_掌握Photoshop图层样式技术
  9. linux 桌面使用体验 远程访问win for linux
  10. 面试精讲之面试考点及大厂真题 - 分布式专栏 10 Redis雪崩,穿透,击穿三连问
  11. 象棋软件最强手机版_我说它是地表手机最强清理软件,没意见吧
  12. Mysql(8)_存储引擎之InnoDB
  13. Dead Pixel
  14. ecshop后台首页mysql_ecshop商城后台使用手册
  15. 软件测试服务方案ppt,测试方案(测试策略).ppt
  16. android红外线开发实例,Android实例-红外线操作(XE10.2+小米5)
  17. mysql char存汉子_char如何存储汉字
  18. 【重识前端】闭包与模块
  19. python爬取高德地图_爬虫实战:如何爬取高德地图?
  20. JavaScript # 前端 js、html中的单引号、双引号及其转义使用

热门文章

  1. Mysql数据库使用:学生选课系统,其中设计到三张表,分别为学生表,课程表,学生和课程对应的关联表。
  2. MySQL数据库的安装与配置
  3. php采集防盗链的图片
  4. 10 AD运行DRC检查
  5. rz: garbage on commandline
  6. 谷歌中文输入法已经可以使用了
  7. 哈罗出行数据挖掘实习生电面题 (一面二面)
  8. 二分和牛顿法实现开根号
  9. SEO如何低成本利用问答平台推广?
  10. 申银万国管理信息系统案例