Java实现pdf转图片案例
作者:简若宁 发布时间:2022-08-11 21:45:41
标签:Java,pdf,图片
工程加入依赖:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.15</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.15</version>
</dependency>
pdf文件转图片:
public static List<String> pdf2Img(File pdfFile) {
if (pdfFile == null || !pdfFile.exists()) {
throw new RuntimeException("pdf文件不能为空");
}
String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
String targetPath = pdfFile.getParent() + File.separator + name;
List<String> imgList = new ArrayList<>();
try {
PDDocument doc = PDDocument.load(pdfFile);
// 页数
int pageCount = doc.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(doc);
for (int i = 0; i < pageCount; i++) {
File targetFile = new File(targetPath + File.separator + name + "-" + (i + 1) + ".jpg");
if (!targetFile.getParentFile().exists()) {
FileUtil.mkdir(targetFile.getParentFile());
}
pdfRenderer.renderImage(i);
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 105, ImageType.RGB);
ImageIOUtil.writeImage(image, targetFile.getPath(), 105);
imgList.add(targetFile.getPath());
}
} catch (IOException e) {
log.error("文件转换异常", e);
throw new RuntimeException("文件转换异常,err=" + e.getMessage());
}
pdf转成一张图片:
/**
* pdf转成一张图片
*
* @param pdfFile pdf图片文件
* @return 图片地址
*/
public static String pdf2OneImg(File pdfFile) {
List<String> imgs = pdf2Img(pdfFile);
int len = imgs.size();
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
src[i] = new File(imgs.get(i));
if (!src[i].exists()) {
throw new RuntimeException("文件【" + imgs.get(i) + "】不存在");
}
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
log.error("", e);
throw new RuntimeException(e);
}
int width = images[i].getWidth();
int height = images[i].getHeight();
// 从图片中读取RGB 像素
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int dst_height = 0;
int dst_width = images[0].getWidth();
// 合成图片像素
for (int i = 0; i < images.length; i++) {
dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
dst_height += images[i].getHeight();
}
if (dst_height < 1) {
throw new RuntimeException("文件合成失败,合成后的图片文件高度=" + dst_height);
}
String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
String targetPath = pdfFile.getParent() + File.separator + name;
// 输出路径
File outFile = new File(targetPath + File.separator + name + "-bigone.jpg");
// 生成新图片
try {
dst_width = images[0].getWidth();
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}
ImageIO.write(ImageNew, "jpg", outFile);
} catch (Exception e) {
log.error("图片合并异常=", e);
throw new RuntimeException(e);
}
return outFile.getPath();
}
来源:https://blog.csdn.net/tanzhming/article/details/117930788


猜你喜欢
- 本文由Markdown语法编辑器编辑完成。1. 需求分析;已知当在调用某一webservice的服务时,如果调用成功,会接受到该服务的返回X
- 序章首先引入依赖 implementation 'com.squareup.retrofit2:retro
- 本文要解决在侧滑菜单右边加个文本框,并能实现文本的上下滑动和菜单的左右滚动。这里推荐可以好好看看android的触摸事件的分发机制,这里我就
- 1.介绍说明: 其实@Valid 与 @Validated都是做数据校验的,只不过注解位置与用法有点不同。不同点:(1)@Valid是使用H
- Android 动态改变布局 &n
- 鉴于谷歌最新推出的Android Studio备受开发者的推崇,所以也跟着体验一下。一、介绍Android Studio Andr
- 简单介绍equals方法是java.lang.Object类的方法有两种用法说明:一、对于字符串变量来说,使用“==”和“equals()”
- ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是:"A ViewStub is an invisible, zer
- 匿名内部类:先举个例子吧,给大家看一下什么是匿名内部类,Endeavor刚刚接触的时候,觉得哇哦,好奇怪的样子,这也太别扭了吧,不知道大家是
- 1. 数据构造索引2个文档到 hotel 索引中:PUT /hotel/_doc/1{ "title": &
- 平均背景法的基本思想是计算每个像素的平均值和标准差作为它的背景模型。平均背景法使用四个OpenCV函数:cvAcc(),累积图像;cvAbs
- 实现字符串库函数功能有些时候我们可能会被限制无法使用库函数,这个时候我们需要编写自己的库函数。但了解了字符串库函数的功能之后,想要实现并不困
- 一、什么是JSONJSON(JavaScript Object Notation)是一种基于JavaScript语法子集的开放标准数据交换格
- 本文实例讲述了C#实现TCP连接信息统计的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.
- 一个请求从客户端发出到达服务器,然后被处理的整个过程其实是非常复杂的。本博客主要介绍请求到达服务器被核心组件DispatcherServle
- 目录多通道分离API操作通道合并API操作结果源码多通道分离APIpublic static void split(Mat m, List&
- 异常算术异常类:ArithmeticExecption空指针异常类:NullPointerException类型强制转换异常:ClassCa
- 一、前言在Spring中,事务有两种实现方式:编程式事务管理: 编程式事务管理使用TransactionTemplate可实现更细
- 这篇文章主要介绍了MyBatis传入数组集合类并使用foreach遍历,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 序言之前封装过一个日志注解,打印方法执行信息,功能较为单一不够灵活,近来兴趣来了,想重构下,使其支持表达式语法,以应对灵活的日志打印需求。该