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

相关文章

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2