近期需要将 pdf 文件转成高清图片,使用库是 pdfbox、fontbox。可以使用 renderImageWithDPI 方法指定转换的清晰度,当然清晰度越高,转换需要的时间越长,转换出来的图片越大,越清晰。

说明:由于 adobo 软件越来越强大,支持的格式越来越多,这造成了 java 软件有些不能转换。所以对于新的格式可能会有转换问题。

1 引入依赖

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.</version>
</dependency>

2 代码如下

public static void convertPdf2Image(String pdfPath, String imageDirPath) {
log.info("start convert pdf file:[{}] to image path:[{}]", pdfPath, imageDirPath);
if (!new File(pdfPath).exists()) {
log.info("pdfFilename:[{}] not exist", pdfPath);
return;
}
if (!new File(imageDirPath).exists()) {
log.info("imageDir:[{}] not exist", imageDirPath);
return;
}
byte[] pdfContent = FileUtil.getFileContentByte(pdfPath);
String filename = FileUtil.getFilename(pdfPath);
float dpi = ;
convertPdf2Image(pdfContent, filename, imageDirPath, dpi);
log.info("convert pdf file:[{}] to image success", filename);
} private static void convertPdf2Image(byte[] pdfContent, String pdfFilename, String imageDirPath, float dpi) {
log.info("convert pdfFilename:[{}] to imageDir:[{}] with dpi:[{}]", pdfFilename, imageDirPath, dpi);
if (ArrayUtils.isEmpty(pdfContent)) {
return;
}
// 为了保证显示清除,至少 90
if (dpi < ) {
dpi = ;
}
String baseSir = imageDirPath;
if (baseSir.endsWith("/") || baseSir.endsWith("\\")) {
baseSir += pdfFilename + "_";
} else {
baseSir += File.separator + pdfFilename + "_";
}
PDDocument document = null;
BufferedOutputStream outputStream = null;
try {
document = PDDocument.load(pdfContent);
int pageCount = document.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(document);
String imgPath;
for (int i = ; i < pageCount; i++) {
imgPath = baseSir + i + ".png";
outputStream = new BufferedOutputStream(new FileOutputStream(imgPath));
BufferedImage image = pdfRenderer.renderImageWithDPI(i, dpi, ImageType.RGB);
ImageIO.write(image, "png", outputStream);
outputStream.close();
log.info("convert to png, total[{}], now[{}], ori:[{}], des[{}]", pageCount, i + , pdfFilename, imgPath);
}
} catch (IOException e) {
log.error("convert pdf to image error, pdfFilename:" + pdfFilename, e);
} finally {
IOUtil.closeSilently(outputStream);
IOUtil.closeSilently(document);
}
} // IOUtil.closeSilently 代码
public static void closeSilently(Closeable io) {
if (io != null) {
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

在实际使用中遇到问题

1)ERROR o.a.p.contentstream.PDFStreamEngine 911 - Cannot read JBIG2 image: jbig2-imageio is not installed

2)Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed

3) java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match at java.awt.image.ColorConvertOp.filter

以上两个问题需要使用 JAI 插件和 jbig2 插件支持,通过引入 jai-imageio-core、jai-imageio-jpeg2000、jbig2-imageio

<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-jpeg2000 -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jbig2-imageio</artifactId>
<version>3.0.2</version>
</dependency>

参考问题文件

https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/000208-p1.pdf

https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/001659-p14.pdf

https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/main%20doc.pdf

https://github.com/crazyCodeLove/studentservice/blob/master/sys/src/main/resources/pdffile/573636.pdf

参考文献

https://stackoverflow.com/questions/42169154/pdfbox1-8-12-convert-pdf-to-white-page-image

https://stackoverflow.com/questions/20424796/pdf-box-generating-blank-images-due-to-jbig2-images-in-it

https://blog.csdn.net/qq_15801963/article/details/80746830

https://my.oschina.net/u/2345654/blog/1058192

https://stackoverflow.com/questions/18351583/illegalargumentexception-numbers-of-source-raster-bands-and-source-color-space

https://stackoverflow.com/questions/10416378/imageio-read-illegal-argument-exception-raster-bands-colour-space-components

java 库 pdfbox 将 pdf 文件转换成高清图片方法的更多相关文章

  1. 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件

    PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...

  2. 怎样将PDF文件转换成Excel表格

    PDF文件怎样转换成Excel表格呢?因为很多的数据信息现在都是通过PDF文件进行传输的,所以很多时候,信息的接受者都需要将这些PDF文件所传输的数据信息转换成Excel表格来进行整理,但是我们应该怎 ...

  3. java 用PDFBox 删除 PDF文件中的某一页

    依赖: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-app ...

  4. PDF文件转换成Excel表格的操作技巧

    我们都知道2007以上版本的Office文档,是可以直接将文档转存为PDF格式文档的.那么反过来,PDF文档可以转换成其他格式的文档吗?这是大家都比较好奇的话题.如果可以以其他格式进行保存,就可以极大 ...

  5. Ghostscript 将PDF文件转换成PNG图片 问题一二

    由于项目需求,需要将原来的PDF文档转换成图片文件,在网上找了一些PDF转图片的方法:测试了几个后,都有这样或那样的问题 1.PDFLibNet.dll,这个类型最初还是挺好用的,能转图片和HTML, ...

  6. nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件

    目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...

  7. Java 使用PDFBox提取PDF文件中的图片

    今天做PDF文件解析,遇到一个需求:提取文件中的图片并保存.使用的是流行的apache开源jar包pdfbox, 但还是遇到坑了,比如pdfbox版本太高或太低都不能用!!这个包竟然没有很好地做好兼容 ...

  8. 使用icepdf将pdf文件转换成照片(以及隐藏的一个bug)

    首先引入依赖: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox ...

  9. 01.在Java中如何创建PDF文件

    1.简介 在这篇快速文章中,我们将重点介绍基于流行的iText和PdfBox库从头开始创建 PDF 文档. 2. Maven 依赖 <dependency> <groupId> ...

随机推荐

  1. Java开发环境之Eclipse

    查看更多Java开发环境配置,请点击<Java开发环境配置大全> 拾壹章:Eclipse安装教程 1)去官网下载安装包 http://www.eclipse.org/downloads/ ...

  2. C#-使用GoogleAPI读写spreadsheets

    https://docs.google.com/spreadsheets/在线使用一些常用办公工具,比如excel. 如需要C#代码自动读写这些excel,则需要使用GoogleAPI. 封装的公用类 ...

  3. equals与hashCode

    当我们需要将自己的类存入HashMap或HashSet时一般都要重写其equals与hashCode方法,但在重写时要符合规范否则会出问题. 1.equals方法 首先equals方法需要满足如下几点 ...

  4. 微信小程序校验文件在浏览器无法打开

    txt文件放在网站根目录后浏览器无法打开,同一目录的其他txt文件却可以打开.我试过了修改文件权限.修改所有者.修改文件编码等各种办法都没有用. 最后找到了.htaccess文件,原来在这个文件里修改 ...

  5. PAT甲级1005水题飘过

    题目分析:用一个字符串输入之后遍历每一位求和后,不断%10获取最后一位存储下来,逆序用对应的英文单词输出(注意输入为0的情况) #include<iostream> #include< ...

  6. sql分割函数

    drop function [dbo].[f_split] create function [dbo].[f_split] ( ),--需要分割的字符串(例如:1,2,3,4,5 我|和|你) )-- ...

  7. 用python做数据分析pandas库介绍之DataFrame基本操作

    怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这一部分主要学习pandas中基于前面两种数据结构的基本操作. 设有DataF ...

  8. 小米BL不解锁刷机

    关于小米NOTE顶配近期解锁的问题中发现还有很多人不会用9008模式刷机,现出个简单教程方便米粉们救砖.硬件:小米NOTE顶配手机 win10系统的电脑 手机与电脑相连的数据线软件:老版本的mifla ...

  9. 4-html图片与链接

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. robot framework的使用方法

    1.后台代码: 目录结构: 测试代码:Arithmetic.py 2.开始编写用例 直接在eclipse上新建一个txt文件即可,或者是通过ride编写用例. (1).首先在eclipse上新建目录T ...