近期需要将 pdf 文件转成高清图片,使用库是 pdfbox、fontbox。可以使用 renderImageWithDPI 方法指定转换的清晰度,当然清晰度越高,转换需要的时间越长,转换出来的图片越大,越清晰。
说明:由于 adobo 软件越来越强大,支持的格式越来越多,这造成了 java 软件有些不能转换。所以对于新的格式可能会有转换问题。
1 引入依赖
org.apache.pdfbox
pdfbox
2.0.16
org.apache.pdfbox
fontbox
2.0.16
2 代码如下
public static voidconvertPdf2Image(String pdfPath, String imageDirPath) {
log.info("start convert pdf file:[{}] to image path:[{}]", pdfPath, imageDirPath);if (!newFile(pdfPath).exists()) {
log.info("pdfFilename:[{}] not exist", pdfPath);return;
}if (!newFile(imageDirPath).exists()) {
log.info("imageDir:[{}] not exist", imageDirPath);return;
}byte[] pdfContent =FileUtil.getFileContentByte(pdfPath);
String filename=FileUtil.getFilename(pdfPath);float dp