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 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代