通过 Apache PDFBox 转换 PDF 文档为图片

Apache PDFBox 是一个开源的 Java 工具库,可用来实现常规的 PDF 操作,包括但不限于创建 PDF 文档、转换 PDF 文档为图片。下面是我封装的一个工具类,实现将文档转换为图片:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import com.google.common.collect.Lists;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
* @desc PDF 工具类
* @author HLJ
* @date 2019/4/12 16:58
*/
public class PDFUtil {

// 默认 DPI
private static final int DEFAULT_DPI = 72;
// 默认图片格式
private static final String DEFAULT_IMAGE_FORMAT = "png";

/**
* @desc PDF 转换为图片格式
* @author HLJ
* @date 2019/4/12 16:54
* @param doc PDF 文档
* @param imageFormat 图片格式(jpg/png)
* @param dpi 指定的 DPI
* @return 每一页的文档对应的字节数组构成的列表
* @throws IOException 转换过程出错
*/
private static List<byte[]> pdfToImage(PDDocument doc, String imageFormat, int dpi) throws IOException {
PDFRenderer renderer = new PDFRenderer(doc);
int totalPageCount = doc.getNumberOfPages();
List<byte[]> outputPages = Lists.newArrayListWithCapacity(totalPageCount);
for (int i = 0; i < totalPageCount; i++) {
try (ByteArrayOutputStream outputPage = new ByteArrayOutputStream()) {
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, imageFormat, outputPage);
outputPages.add(outputPage.toByteArray());
}
}
return outputPages;
}

/**
* @desc PDF 转换为图片格式
* @author HLJ
* @date 2019/4/12 16:54
* @param input PDF 文档的字节数组
* @param imageFormat 图片格式(jpg/png)
* @param dpi 指定的 DPI
* @return 每一页的文档对应的字节数组构成的列表
* @throws IOException 转换过程出错
*/
public static List<byte[]> pdfToImage(byte[] input, String imageFormat, int dpi) throws IOException {
try (PDDocument doc = PDDocument.load(input)) {
return pdfToImage(doc, imageFormat, dpi);
}
}

/**
* @desc PDF 转换为图片格式
* 会转换为 DPI 为 72 的 PNG 图片
* @author HLJ
* @date 2019/4/12 16:54
* @param input PDF 文档的字节数组
* @return 每一页的文档对应的字节数组构成的列表
* @throws IOException 转换过程出错
*/
public static List<byte[]> pdfToImage(byte[] input) throws IOException {
return pdfToImage(input, DEFAULT_IMAGE_FORMAT, DEFAULT_DPI);
}

/**
* @desc PDF 转换为图片格式
* @author HLJ
* @date 2019/4/12 16:54
* @param input PDF 文档的输入流
* @param imageFormat 图片格式(jpg/png)
* @param dpi 指定的 DPI
* @return 每一页的文档对应的字节数组构成的列表
* @throws IOException 转换过程出错
*/
public static List<byte[]> pdfToImage(InputStream input, String imageFormat, int dpi) throws IOException {
try (PDDocument doc = PDDocument.load(input)) {
return pdfToImage(doc, imageFormat, dpi);
}
}

/**
* @desc PDF 转换为图片格式
* 会转换为 DPI 为 72 的 PNG 图片
* @author HLJ
* @date 2019/4/12 16:54
* @param input PDF 文档的输入流
* @return 每一页的文档对应的字节数组构成的列表
* @throws IOException 转换过程出错
*/
public static List<byte[]> pdfToImage(InputStream input) throws IOException {
return pdfToImage(input, DEFAULT_IMAGE_FORMAT, DEFAULT_DPI);
}

}

更多关于 PDFBox 的使用可访问其官网了解。


通过 Apache PDFBox 转换 PDF 文档为图片
https://blog.yohlj.cn/posts/21a3bbfc/
作者
Enoch
发布于
2019年4月12日
许可协议