Java实现pdf转图片案例
作者:简若宁 时间:2022-08-11 21:45:41
工程加入依赖:
<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
标签:Java,pdf,图片
0
投稿
猜你喜欢
JAVA中实现链式操作(方法链)的简单例子
2022-12-16 00:54:50
深入第K大数问题以及算法概要的详解
2022-05-22 16:52:29
springboot 使用mybatis查询的示例代码
2022-03-30 03:19:09
C#端口扫描器的编写方法
2023-12-17 17:47:24
IntelliJ IDEA(2020.2)的下载、安装步骤详细教程
2023-11-25 07:10:16
编写android拨打电话apk应用实例代码
2021-08-25 11:09:12
Android SQLite基本用法详解
2023-07-02 05:14:58
C# Winform实现圆角无锯齿按钮
2023-09-16 02:26:29
详解Spring Boot Oauth2缓存UserDetails到Ehcache
2023-02-26 21:57:12
SpringBoot中@ConfigurationProperties注解实现配置绑定的三种方法
2023-03-19 12:36:25
Windows系统中C#读写ini配置文件的程序代码示例分享
2022-05-15 08:57:33
spring cloud gateway网关路由分配代码实例解析
2021-06-09 02:54:08
C#解析json字符串总是多出双引号的原因分析及解决办法
2022-10-22 02:40:46
spring-boot-maven-plugin:打包时排除provided依赖问题
2023-07-18 02:12:11
Spring Boot分离配置文件的多种方式总结
2021-08-31 14:43:37
Java关于桶排序的知识点总结
2023-12-06 03:18:04
spring bean.xml文件p标签使用报错的解决
2022-03-12 23:16:16
C#操作 JSON方法汇总
2023-01-23 06:49:59
详解Java中方法重写与重载的区别(面试高频问点)
2022-07-19 10:36:48
关于@Value取值为NULL的解决方案
2021-07-15 06:38:42