JAVA 实现PDF转图片(pdfbox版)

2023-11-01 16:12
文章标签 java 实现 图片 pdf pdfbox

本文主要是介绍JAVA 实现PDF转图片(pdfbox版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

依赖:

pdf存放路径

正文开始:

pdf转换多张图片、长图

@Test
void pdf2Image() {String dstImgFolder = "";String PdfFilePath = "";String relativelyPath=System.getProperty("user.dir");PdfFilePath = relativelyPath + "/uploadTest/"+"文档.pdf";dstImgFolder = relativelyPath + "/uploadTest/";/* dpi越大转换后越清晰,相对转换速度越慢 */int dpi = 450;File file = new File(PdfFilePath);PDDocument pdDocument; // 创建PDF文档try {String imgPDFPath = file.getParent();int dot = file.getName().lastIndexOf('.');String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名String imgFolderPath = null;if (dstImgFolder.equals("")) {imgFolderPath = imgPDFPath + File.separator;// 获取图片存放的文件夹路径} else {imgFolderPath = dstImgFolder + File.separator;}if (createDirectory(imgFolderPath)) {pdDocument = PDDocument.load(file);PDFRenderer renderer = new PDFRenderer(pdDocument);PdfReader reader = new PdfReader(PdfFilePath);int pages = reader.getNumberOfPages();StringBuffer imgFilePath = null;BufferedImage[] bufferedImages = new BufferedImage[pages];for (int i = 0; i < pages; i++) {String imgFilePathPrefix = imgFolderPath + File.separator;imgFilePath = new StringBuffer();imgFilePath.append(imgFilePathPrefix);imgFilePath.append("_");imgFilePath.append(i + 1);imgFilePath.append(".png");// File dstFile = new File(imgFilePath.toString());BufferedImage image = renderer.renderImageWithDPI(i, dpi);bufferedImages[i] = image;// ImageIO.write(image, "png", dstFile);}dstImgFolder = dstImgFolder + imagePDFName + ".png";// PDF文件全部页数转PNG图片,若多张展示注释即可 工具类贴在下面ImageMergeUtil.mergeImage(bufferedImages, 2, dstImgFolder);System.out.println("PDF文档转PNG图片成功!");} else {System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");}} catch (IOException e) {e.printStackTrace();}}private static boolean createDirectory(String folder) {File dir = new File(folder);if (dir.exists()) {return true;} else {return dir.mkdirs();}}

// ImageMergeUtil 图片的合并,多张图片合成长图
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;public class ImageMergeUtil {public static void main(String[] args) throws IOException {String filePath = "D:\\temp\\ImageMergeUtil\\";String path1 = filePath + "a.png";String path2 = filePath + "b.png";mergeImage(path1, path2,  2, filePath+"c.png");}/*** 图片拼接* @param path1     图片1路径* @param path2     图片2路径* @param type      1 横向拼接, 2 纵向拼接* (注意:必须两张图片长宽一致)*/public static void mergeImage( String path1, String path2, int type, String targetFile) throws IOException {File file1 = new File(path1);File file2 = new File(path2);//两张图片的拼接int len = 2;BufferedImage[] images = new BufferedImage[len];images[0] = ImageIO.read(file1);images[1] = ImageIO.read(file2);mergeImage(images, type, targetFile);}/*** 图片拼接* @param images     图片数组* @param type      1 横向拼接, 2 纵向拼接* (注意:必须两张图片长宽一致)*/public static void mergeImage(BufferedImage[] images, int type, String targetFile) throws IOException {int len = images.length;int[][] ImageArrays = new int[len][];for (int i = 0; i < len; i++) {int width = images[i].getWidth();int height = images[i].getHeight();ImageArrays[i] = new int[width * height];ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);}int newHeight = 0;int newWidth = 0;for (int i = 0; i < images.length; i++) {// 横向if (type == 1) {newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();newWidth += images[i].getWidth();} else if (type == 2) {// 纵向newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();newHeight += images[i].getHeight();}}if (type == 1 && newWidth < 1) {return;}if (type == 2 && newHeight < 1) {return;}// 生成新图片try {BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);int height_i = 0;int width_i = 0;for (int i = 0; i < images.length; i++) {if (type == 1) {ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,images[i].getWidth());width_i += images[i].getWidth();} else if (type == 2) {ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);height_i += images[i].getHeight();}}//输出想要的图片ImageIO.write(ImageNew, "png", new File(targetFile));} catch (Exception e) {e.printStackTrace();}}

展示效果:

附加:小程序预览wxml代码

这篇关于JAVA 实现PDF转图片(pdfbox版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/324203

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分