目录
- 一、背景
- 二、maven环境
- 2.1、依赖
- 2.2、插件
- 三、PDF工具类
- 四、实践
- 4.1、单页PDF形式的发票转为图片
- 4.2、多页PDF文档转为图片
- 结语
一、背景
很多小伙伴们不知道怎么把PDF文件转成图片,并且网上的实例总是跑不通,因为很多小细节没有写出来。现在我给大家两个个完整的实例:单页PDF形式的发票转为图片和多页PDF文档转为图片。我这边采用的是springboot项目,JDK是1.8。具体的环境搭建和项目的创建我就不多说了,网上关于这些有很多的教程,请大家仔细查阅。
二、maven环境
2.1、依赖
本文主要以maven项目进行相关事例
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope>
</dependency><!--主要用到Pair类-->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version>
</dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.23</version><type>bundle</type>
</dependency>
2.2、插件
很多小伙伴可能会依赖有问题,那是因为pdfbox的依赖类型为bundle,需要加载插件:把他放到<build></build>标签下的<plugins></plugins>里面,插件依赖如下:
<plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions>
</plugin>
三、PDF工具类
PdfUtil.java
本工具类主要使用到apache的pdfbox和common-lang3实现(临时写的,异常处理就没有特别关注,主要在于怎么转换)
package com.alian.csdn.utils;import org.apache.commons.lang3.tuple.Pair;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class PdfUtil {/*** @param source 原文件* @param desFilePath 生成图片的路径* @param desFileName 生成图片的名称(多页文档时会变成:名称+下划线+从1开始的数字)* @param imageType 图片类型* @return*/public static Pair<Boolean, Object> pdfToImage(String source, String desFilePath, String desFileName, String imageType) {//通过给定的源路径名字符串创建一个File实例File file = new File(source);if (!file.exists()) {return Pair.of(false, "文件不存在,无法转化");}//目录不存在则创建目录File destination = new File(desFilePath);if (!destination.exists()) {boolean flag = destination.mkdirs();System.out.println("创建文件夹结果:" + flag);}PDDocument doc = null;try {//加载PDF文件doc = PDDocument.load(file);PDFRenderer renderer = new PDFRenderer(doc);//获取PDF文档的页数int pageCount = doc.getNumberOfPages();System.out.println("文档一共" + pageCount + "页");List<Object> fileList = new ArrayList<>();for (int i = 0; i < pageCount; i++) {//只有一页的时候文件名为传入的文件名,大于一页的文件名为:文件名_自增加数字(从1开始)String realFileName = pageCount > 1 ? desFileName + "_" + (i + 1) : desFileName;//每一页通过分辨率和颜色值进行转化BufferedImage bufferedImage = renderer.renderImageWithDPI(i, 96 * 2, ImageType.RGB);String filePath = desFilePath + File.separator + realFileName + "." + imageType;//写入文件ImageIO.write(bufferedImage, imageType, new File(filePath));//文件名存入listfileList.add(filePath);}return Pair.of(true, fileList);} catch (IOException e) {e.printStackTrace();return Pair.of(false, "PDF转化图片异常");} finally {try {if (doc != null) {doc.close();}} catch (IOException e) {System.out.println("关闭文档失败");e.printStackTrace();}}}
}
四、实践
4.1、单页PDF形式的发票转为图片
/*** 单页PDF形式的发票转为图片* 此处单元测试的注解是采用:org.junit.Test*/@Testpublic void pdfInvoiceToImage() {String source = "C:\\myFile\\invoice\\加油发票.pdf";String desFileName = "invoice";String desFilePath = "C:\\myFile\\invoice";String imageType = "png";Pair<Boolean, Object> pair = PdfUtil.pdfToImage(source, desFilePath, desFileName, imageType);System.out.println("PDF形式的发票转化为图片结果:" + pair.getLeft());if (!pair.getLeft()) {System.out.println("" + pair.getRight());} else {List<String> fileList = (List<String>) pair.getRight();System.out.println("转化的文件的内容:");fileList.forEach(System.out::println);}}
运行结果:
文档一共1页
单页PDF形式的发票转化为图片结果:true
转化的文件的内容:
C:\myFile\invoice\invoice.png
在目标目录里PDF发票已经转成图片了,并且清晰度还可以,如果觉得不可以大家可以去调整工具类dpi的值。

4.2、多页PDF文档转为图片
/*** 多页PDF文档转为图片* 此处单元测试的注解是采用:org.junit.Test*/@Testpublic void pdfDocumentToImage2() {String source = "C:\\myFile\\document\\阿里巴巴Java开发手册(终极版).pdf";String desFileName = "阿里巴巴Java开发手册";String desFilePath = "C:\\myFile\\document";String imageType = "png";Pair<Boolean, Object> pair = PdfUtil.pdfToImage(source, desFilePath, desFileName, imageType);System.out.println("PDF文档转化为图片结果:" + pair.getLeft());if (!pair.getLeft()) {System.out.println("" + pair.getRight());} else {List<String> fileList = (List<String>) pair.getRight();System.out.println("转化的文件的内容:");fileList.forEach(System.out::println);}}
运行结果:
文档一共39页
PDF文档转化为图片结果:true
转化的文件的内容:
C:\myFile\document\阿里巴巴Java开发手册_1.png
C:\myFile\document\阿里巴巴Java开发手册_2.png
C:\myFile\document\阿里巴巴Java开发手册_3.png
C:\myFile\document\阿里巴巴Java开发手册_4.png
C:\myFile\document\阿里巴巴Java开发手册_5.png
C:\myFile\document\阿里巴巴Java开发手册_6.png
C:\myFile\document\阿里巴巴Java开发手册_7.png
C:\myFile\document\阿里巴巴Java开发手册_8.png
C:\myFile\document\阿里巴巴Java开发手册_9.png
C:\myFile\document\阿里巴巴Java开发手册_10.png
C:\myFile\document\阿里巴巴Java开发手册_11.png
C:\myFile\document\阿里巴巴Java开发手册_12.png
C:\myFile\document\阿里巴巴Java开发手册_13.png
C:\myFile\document\阿里巴巴Java开发手册_14.png
C:\myFile\document\阿里巴巴Java开发手册_15.png
C:\myFile\document\阿里巴巴Java开发手册_16.png
C:\myFile\document\阿里巴巴Java开发手册_17.png
C:\myFile\document\阿里巴巴Java开发手册_18.png
C:\myFile\document\阿里巴巴Java开发手册_19.png
C:\myFile\document\阿里巴巴Java开发手册_20.png
C:\myFile\document\阿里巴巴Java开发手册_21.png
C:\myFile\document\阿里巴巴Java开发手册_22.png
C:\myFile\document\阿里巴巴Java开发手册_23.png
C:\myFile\document\阿里巴巴Java开发手册_24.png
C:\myFile\document\阿里巴巴Java开发手册_25.png
C:\myFile\document\阿里巴巴Java开发手册_26.png
C:\myFile\document\阿里巴巴Java开发手册_27.png
C:\myFile\document\阿里巴巴Java开发手册_28.png
C:\myFile\document\阿里巴巴Java开发手册_29.png
C:\myFile\document\阿里巴巴Java开发手册_30.png
C:\myFile\document\阿里巴巴Java开发手册_31.png
C:\myFile\document\阿里巴巴Java开发手册_32.png
C:\myFile\document\阿里巴巴Java开发手册_33.png
C:\myFile\document\阿里巴巴Java开发手册_34.png
C:\myFile\document\阿里巴巴Java开发手册_35.png
C:\myFile\document\阿里巴巴Java开发手册_36.png
C:\myFile\document\阿里巴巴Java开发手册_37.png
C:\myFile\document\阿里巴巴Java开发手册_38.png
C:\myFile\document\阿里巴巴Java开发手册_39.png
我们现在去目标目录查看文件是否已转化完成,从列表可以看到文档未丢失,并且清晰。

小伙伴们可以直接拷贝相关代码进行试验,赶紧行动起来吧。
结语
以上就是今天要讲的内容,本文简单介绍了使用pdfbox把pdf文件转换成图片,同时支持单页和多页PDF文件的转化,当然pdfbox是一个很强大的功能,我们这里只是用到最简单的处理,详细的使用还需要大家多多查看官方文档。如果有什么疑问,欢迎大家评论交流。如果觉得不错,可以帮忙一键三连,谢谢。















