当前位置: 首页 > news >正文

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代码

相关文章:

  • linux下mysql-8.2.0集群部署(python版本要在2.7以上)
  • 使用 PyTorch 构建自定义 GPT
  • android services
  • 国际多语言出海商城源码/返佣产品自动匹配拼单商城源码
  • MacOS将Node.js升级到最新版本
  • pytest 使用(二):前后置(固件,夹具)
  • asp.net老年大学教务管理信息系统VS开发sqlserver数据库web结构c#编程
  • Web Woeker和Shared Worker的使用以及案例
  • 【牛客网】安全—加密和安全
  • CSS3盒模型
  • C语言实现 1.在一个二维数组中形成 n 阶矩阵,2.去掉靠边元素,生成新的 n-2 阶矩阵;3.求矩阵主对角线下元素之和:4.以方阵形式输出数组。
  • Linux编辑器-vim使用
  • Cross Site Scripting (XSS)
  • MongoDB——MongoDB删除系统自带的local数据库
  • 淘宝协议最新版
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【Linux系统编程】快速查找errno错误码信息
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • Docker 笔记(2):Dockerfile
  • JS学习笔记——闭包
  • Laravel 菜鸟晋级之路
  • maya建模与骨骼动画快速实现人工鱼
  • PAT A1050
  • Python连接Oracle
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 高程读书笔记 第六章 面向对象程序设计
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 学习笔记:对象,原型和继承(1)
  • 延迟脚本的方式
  • 一加3T解锁OEM、刷入TWRP、第三方ROM以及ROOT
  • 移动端 h5开发相关内容总结(三)
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • ​Python 3 新特性:类型注解
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (07)Hive——窗口函数详解
  • (10)ATF MMU转换表
  • (12)Linux 常见的三种进程状态
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (八十八)VFL语言初步 - 实现布局
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (四)c52学习之旅-流水LED灯
  • (四)汇编语言——简单程序
  • (转)LINQ之路
  • (转)为C# Windows服务添加安装程序
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .net framework profiles /.net framework 配置