java操作PDF:转换、合成、切分
将PDF每一页切割成图片
PDFUtils.cutPNG("D:/tmp/1.pdf","D:/tmp/输出图片路径/");
将PDF转换成一张长图片
PDFUtils.transition_ONE_PNG("D:/tmp/1.pdf");
将多张图片合并成一个PDF文件
PDFUtils.merge_PNG("D:/tmp/测试图片/");
将多个PDF合并成一个PDF文件
PDFUtils.merge_PDF("D:/tmp/测试图片/");
取出指定PDF的起始和结束页码作为新的pdf
PDFUtils.getPartPDF("D:/tmp/1.pdf",3,5);
引入依赖
<!-- apache PDF转图片--><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version></dependency>
<!-- itext 图片合成PDF--><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.2</version></dependency>
代码(如下4个java类放在一起直接使用即可)
PDFUtils.java
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** pdf工具类*/
public class PDFUtils {/*** PDF分解图片文件** @param pdfPath pdf文件路径*/public static void cutPNG(String pdfPath) throws IOException {File pdf = new File(pdfPath);cutPNG(pdfPath, pdf.getParent() + File.separator + pdf.getName() + "_pngs");}/*** PDF分解图片文件** @param pdfPath pdf文件路径* @param outPath 输出文件夹路径*/public static void cutPNG(String pdfPath, String outPath) throws IOException {File outDir = new File(outPath);if (!outDir.exists()) outDir.mkdirs();cutPNG(new File(pdfPath), outDir);}/*** PDF分解图片文件** @param pdf pdf文件* @param outDir 输出文件夹*/public static void cutPNG(File pdf, File outDir) throws IOException {LogUtils.info("PDF分解图片工作开始");List<BufferedImage> list = getImgList(pdf);LogUtils.info(pdf.getName() + " 一共发现了 " + list.size() + " 页");FileUtils.cleanDir(outDir);for (int i = 0; i < list.size(); i++) {IMGUtils.saveImageToFile(list.get(i), outDir.getAbsolutePath() + File.separator + (i + 1) + ".png");LogUtils.info("已保存图片:" + (i + 1) + ".png");}LogUtils.info("PDF分解图片工作结束,一共分解出" + list.size() + "个图片文件,保存至:" + outDir.getAbsolutePath());}/*** 将pdf文件转换成一张图片** @param pdfPath pdf文件路径*/public static void transition_ONE_PNG(String pdfPath) throws IOException {transition_ONE_PNG(new File(pdfPath));}/*** 将pdf文件转换成一张图片** @param pdf pdf文件*/public static void transition_ONE_PNG(File pdf) throws IOException {LogUtils.info("PDF转换长图工作开始");List<BufferedImage> list = getImgList(pdf);LogUtils.info(pdf.getName() + " 一共发现了 " + list.size() + " 页");BufferedImage image = list.get(0);for (int i = 1; i < list.size(); i++) {image = IMGUtils.verticalJoinTwoImage(image, list.get(i));}byte[] data = IMGUtils.getBytes(image);String imgPath = pdf.getParent() + File.separator + pdf.getName().replaceAll("\\.", "_") + ".png";FileUtils.saveDataToFile(imgPath, data);LogUtils.info("PDF转换长图工作结束,合成尺寸:" + image.getWidth() + "x" + image.getHeight() + ",合成文件大小:" + data.length / 1024 + "KB,保存至:" + imgPath);}/*** 将PDF文档拆分成图像列表** @param pdf PDF文件*/private static List<BufferedImage> getImgList(File pdf) throws IOException {PDDocument pdfDoc = PDDocument.load(pdf);List<BufferedImage> imgList = new ArrayList<>();PDFRenderer pdfRenderer = new PDFRenderer(pdfDoc);int numPages = pdfDoc.getNumberOfPages();for (int i = 0; i < numPages; i++) {BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB);imgList.add(image);}pdfDoc.close();return imgList;}/*** 图片合成PDF** @param pngsDirPath 图片文件夹路径*/public static void merge_PNG(String pngsDirPath) throws Exception {File pngsDir = new File(pngsDirPath);merge_PNG(pngsDir, pngsDir.getName() + ".pdf");}/*** 图片合成pdf** @param pngsDir 图片文件夹* @param pdfName 合成pdf名称*/private static void merge_PNG(File pngsDir, String pdfName) throws Exception {File pdf = new File(pngsDir.getParent() + File.separator + pdfName);if (!pdf.exists()) pdf.createNewFile();File[] pngList = pngsDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".png"));LogUtils.info("在" + pngsDir.getAbsolutePath() + ",一共发现" + pngList.length + "个PNG文件");Document document = new Document();FileOutputStream fo = new FileOutputStream(pdf);PdfWriter writer = PdfWriter.getInstance(document, fo);document.open();for (File f : pngList) {document.newPage();byte[] bytes = FileUtils.getFileBytes(f);Image image = Image.getInstance(bytes);float heigth = image.getHeight();float width = image.getWidth();int percent = getPercent2(heigth, width);image.setAlignment(Image.MIDDLE);image.scalePercent(percent + 3);// 表示是原来图像的比例;document.add(image);System.out.println("正在合成" + f.getName());}document.close();writer.close();System.out.println("PDF文件生成地址:" + pdf.getAbsolutePath());}private static int getPercent2(float h, float w) {int p = 0;float p2 = 0.0f;p2 = 530 / w * 100;p = Math.round(p2);return p;}/*** 多PDF合成** @param pngsDirPath pdf所在文件夹路径*/public static void merge_PDF(String pngsDirPath) throws IOException {File pngsDir = new File(pngsDirPath);merge_PDF(pngsDir, pngsDir.getName() + "_合并.pdf");}/*** 多PDF合成** @param pngsDir pdf所在文件夹* @param pdfName 合成pdf文件名*/private static void merge_PDF(File pngsDir, String pdfName) throws IOException {File[] pdfList = pngsDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".pdf"));LogUtils.info("在" + pngsDir.getAbsolutePath() + ",一共发现" + pdfList.length + "个PDF文件");PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();pdfMergerUtility.setDestinationFileName(pngsDir.getParent() + File.separator + pdfName);for (File f : pdfList) {pdfMergerUtility.addSource(f);}pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());}public static void getPartPDF(String pdfPath, int from, int end) throws Exception {pdfPath = pdfPath.trim();Document document = null;PdfCopy copy = null;PdfReader reader = new PdfReader(pdfPath);int n = reader.getNumberOfPages();if (end == 0) {end = n;}document = new Document(reader.getPageSize(1));copy = new PdfCopy(document, new FileOutputStream(pdfPath.substring(0, pdfPath.length() - 4) + "_" + from + "_" + end + ".pdf"));document.open();for (int j = from; j <= end; j++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}document.close();}}
IMGUtils.java
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;public class IMGUtils {/*** 将图像转为png文件的字节数据* @param image 目标图像* @return 返回数据*/public static byte[] getBytes(BufferedImage image){return getBytes(image,"png");}/*** 将图像转换为指定媒体文件类型的字节数据* @param image 目标图像* @param fileType 文件类型(后缀名)* @return 返回数据*/public static byte[] getBytes(BufferedImage image,String fileType){ByteArrayOutputStream outStream = new ByteArrayOutputStream();try {ImageIO.write(image,fileType,outStream);} catch (IOException e) {e.printStackTrace();}return outStream.toByteArray();}/*** 读取图像,通过文件* @param filePath 文件路径* @return BufferedImage*/public static BufferedImage getImage(String filePath){try {return ImageIO.read(new FileInputStream(filePath));} catch (IOException e) {e.printStackTrace();}return null;}/*** 读取图像,通过字节数据* @param data 字节数据* @return BufferedImage*/public static BufferedImage getImage(byte[] data){try {return ImageIO.read(new ByteArrayInputStream(data));} catch (IOException e) {e.printStackTrace();}return null;}/*** 保存图像到指定文件* @param image 图像* @param filePath 文件路径*/public static void saveImageToFile(BufferedImage image,String filePath) throws IOException {FileUtils.saveDataToFile(filePath,getBytes(image));}/*** 纵向拼接图片*/public static BufferedImage verticalJoinTwoImage(BufferedImage image1, BufferedImage image2){if(image1==null)return image2;if(image2==null)return image1;BufferedImage image=new BufferedImage(image1.getWidth(),image1.getHeight()+image2.getHeight(),image1.getType());Graphics2D g2d = image.createGraphics();g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);g2d.drawImage(image1, 0, 0, image1.getWidth(), image1.getHeight(), null);g2d.drawImage(image2, 0, image1.getHeight(), image2.getWidth(), image2.getHeight(), null);g2d.dispose();// 释放图形上下文使用的系统资源return image;}/*** 剪裁图片* @param image 对象* @param x 顶点x* @param y 顶点y* @param width 宽度* @param height 高度* @return 剪裁后的对象*/public static BufferedImage cutImage(BufferedImage image,int x,int y,int width,int height){if(image==null)return null;return image.getSubimage(x,y,width,height);}}
FileUtils.java
import java.io.*;public class FileUtils {/*** 将数据保存到指定文件路径*/public static void saveDataToFile(String filePath,byte[] data) throws IOException {File file = new File(filePath);if(!file.exists())file.createNewFile() ;FileOutputStream outStream = new FileOutputStream(file);outStream.write(data);outStream.flush();outStream.close();}/*** 读取文件数据*/public static byte[] getFileBytes(File file) throws IOException {if(!file.exists()||(file.exists()&&!file.isFile()))return null;InputStream inStream=new FileInputStream(file);ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}inStream.close();return outStream.toByteArray();}/*** 读取文本文件字*/public static String getFileText(String path){try {byte[] data= getFileBytes(new File(path));return new String(data);} catch (Exception e) {return "";}}/*** 清空文件夹下所有文件*/public static void cleanDir(File dir){if(dir!=null&&dir.isDirectory()){for(File file:dir.listFiles()){if(file.isFile())file.delete();if(file.isDirectory())cleanDir(file);}}}}
LogUtils.java
public class LogUtils {public static void info(String context){System.out.println(context);}public static void warn(String context){System.out.println(context);}public static void error(String context){System.out.println(context);}}
相关文章:
java操作PDF:转换、合成、切分
将PDF每一页切割成图片 PDFUtils.cutPNG("D:/tmp/1.pdf","D:/tmp/输出图片路径/"); 将PDF转换成一张长图片 PDFUtils.transition_ONE_PNG("D:/tmp/1.pdf"); 将多张图片合并成一个PDF文件 PDFUtils.merge_PNG("D:/tmp/测试图片/"); 将多…...

递增子序列——力扣491
文章目录 题目描述递归枚举 + 减枝题目描述 递归枚举 + 减枝 递归枚举子序列的通用模板 vector<vector<int>> ans; vector<int> temp; void dfs(int cur...

解密!品牌独立站为何能成为外国消费者的心头爱?
中国人做事强调要知其然、知其所以然、知其所以必然。这一理念非常符合新时代中国跨境出海品牌独立站的发展思路。在做好品牌独立站之前,我们也必须知其然(什么是独立站?),知其所以然(为什么要建独立站&…...

【HDFS】每天一个RPC系列----complete(二):客户端侧
上图给出了最终会调用到complete RPC的客户端侧方法链路(除去Router那条线了)。 org.apache.hadoop.hdfs.DFSOutputStream#completeFile(org.apache.hadoop.hdfs.protocol.ExtendedBlock): 下面这个方法在complete rpc返回true之前,会进行重试,直到超过最大重试次数抛异…...

五、PC远程控制ESP32 LED灯
1. 整体思路 2. 代码 # 整体流程 # 1. 链接wifi # 2. 启动网络功能(UDP) # 3. 接收网络数据 # 4. 处理接收的数据import socket import time import network import machinedef do_connect():wlan = network.WLAN(network.STA_IF)wlan.active(True)if not wlan.isconnected(…...

详解PHP反射API
PHP中的反射API就像Java中的java.lang.reflect包一样。它由一系列可以分析属性、方法和类的内置类组成。它在某些方面和对象函数相似,比如get_class_vars(),但是更加灵活,而且可以提供更多信息。反射API也可与PHP最新的面向对象特性一起工作&…...
打开虚拟机进行ip addr无网络连接
打开虚拟机进行ip addr无网络连接 参考地址:https://www.cnblogs.com/Courage129/p/16796390.html 打开虚拟机进行ip addr无网络连接。 输入下面的命令, sudo dhclient ens33 会重新分配一个新的ip地址,但是此时的ip地址已经不是我原先在虚…...

Spring Boot如何整合mybatisplus
文章目录 1. 相关配置和代码2. 整合原理2.1 spring boot自动配置2.2 MybatisPlusAutoConfiguration2.3 debug流程2.3.1 MapperScannerRegistrar2.3.2MapperScannerConfigurer2.3.3 创建MybatisPlusAutoConfiguration2.3.4 创建sqlSessionFactory2.3.5 创建SqlSessionTemplate2.…...

webpack基础知识一:说说你对webpack的理解?解决了什么问题?
一、背景 Webpack 最初的目标是实现前端项目的模块化,旨在更高效地管理和维护项目中的每一个资源 模块化 最早的时候,我们会通过文件划分的形式实现模块化,也就是将每个功能及其相关状态数据各自单独放到不同的JS 文件中 约定每个文件是一…...

小研究 - 基于 MySQL 数据库的数据安全应用设计(二)
信息系统工程领域对数据安全的要求比较高,MySQL 数据库管理系统普遍应用于各种信息系统应用软件的开发之中,而角色与权限设计不仅关乎数据库中数据保密性的性能高低,也关系到用户使用数据库的最低要求。在对数据库的安全性进行设计时…...
大数据-数据内容分类
大数据-数据内容分类 结构化数据 可以使用关系型数据库表示和存储,可以用二维表来逻辑表达实现的数据 结构化数据:二维表(关系型) 结构化数据:先有结构、再有数据 数据以行为单位,一行数据表示一个实体…...

Babel编译与Webpack
目录 Babel初识BabelBabel 使用方式使用 Babel 前的准备工作 WebpackWebpack介绍Webpack初体验Webpack核心概念入口(entry)出口(output)加载 (loader)插件(plugins) Babel Babel官网: https://babeljs.io/…...

0805hw
1. #include <myhead.h> void Bub_sort(int *arr,int n)//冒泡排序 {for(int i1;i<n;i){int count0;for(int j0;j<n-i;j){if(arr[j]>arr[j1]){int temparr[j];arr[j]arr[j1];arr[j1]temp;count;}}if(count0){break;}}printf("冒泡排序后输出结果:\n"…...

ROS实现机器人移动
开源项目 使用是github上六合机器人工坊的项目。 https://github.com/6-robot/wpr_simulation.git 机器人运动模型 运动模型如下所示:👇 机器人运动的消息包: 实现思路:👇 为什么要使用/cmd_vel话题。因为这…...

Dockerfile构建LNMP镜像
建立工作目录 [rootlocalhost ~]# mkdir lnmp [rootlocalhost ~]# cd lnmp/ 编写Dockerfile文件 [rootlocalhost lnmp]# vim Dockerfile [rootlocalhost lnmp]# ll 总用量 4 -rw-r--r--. 1 root root 774 8月 3 14:54 Dockerfile [rootlocalhost lnmp]# vim Dockerfile #基础…...

总结七大排序!
排序总览 外部排序:依赖硬盘(外部存储器)进行的排序。对于数据集合的要求特别高,只能在特定场合下使用(比如一个省的高考成绩排序)。包括桶排序,基数排序,计数排序,都是o…...
没有fastjson,rust怎么方便的解析提取复杂json呢?
在 Rust 中解析和提取复杂的 JSON 结构,你可以使用 serde_json 库来处理。 serde_json 提供了一组功能强大的方法来解析和操作 JSON 数据。 下面是一个示例,展示了如何解析和提取复杂的 JSON 结构: use serde_json::{Value, Result}; fn mai…...

Docker制作SpringBoot镜像
Dcokerfile目录 编写Dockerfile FROM openjdk:8 #发布到网上时只会把jar包和Dockerfile发布上去RUN mkdir -p /opt/javaCOPY app.jar /opt/java/app.jar #地址映射 #CMD ["--server.port8080"] #对外暴露端口(可以任意修改) EXPOSE 15009 #执行命令 #ENTRYPOINT [&q…...
力扣:53. 最大子数组和(Python3)
题目: 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 子数组 是数组中的一个连续部分。 来源:力扣(LeetCode) 链接ÿ…...

利用appium抓取app中的信息
一、appium简介 二、appium环境安装 三、联调测试环境 四、利用appium自动控制移动设备并提取数据...

铭豹扩展坞 USB转网口 突然无法识别解决方法
当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...
从零实现富文本编辑器#5-编辑器选区模型的状态结构表达
先前我们总结了浏览器选区模型的交互策略,并且实现了基本的选区操作,还调研了自绘选区的实现。那么相对的,我们还需要设计编辑器的选区表达,也可以称为模型选区。编辑器中应用变更时的操作范围,就是以模型选区为基准来…...

智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql
智慧工地管理云平台系统,智慧工地全套源码,java版智慧工地源码,支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求,提供“平台网络终端”的整体解决方案,提供劳务管理、视频管理、智能监测、绿色施工、安全管…...

Opencv中的addweighted函数
一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...

对WWDC 2025 Keynote 内容的预测
借助我们以往对苹果公司发展路径的深入研究经验,以及大语言模型的分析能力,我们系统梳理了多年来苹果 WWDC 主题演讲的规律。在 WWDC 2025 即将揭幕之际,我们让 ChatGPT 对今年的 Keynote 内容进行了一个初步预测,聊作存档。等到明…...
镜像里切换为普通用户
如果你登录远程虚拟机默认就是 root 用户,但你不希望用 root 权限运行 ns-3(这是对的,ns3 工具会拒绝 root),你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案:创建非 roo…...

DIY|Mac 搭建 ESP-IDF 开发环境及编译小智 AI
前一阵子在百度 AI 开发者大会上,看到基于小智 AI DIY 玩具的演示,感觉有点意思,想着自己也来试试。 如果只是想烧录现成的固件,乐鑫官方除了提供了 Windows 版本的 Flash 下载工具 之外,还提供了基于网页版的 ESP LA…...
MySQL中【正则表达式】用法
MySQL 中正则表达式通过 REGEXP 或 RLIKE 操作符实现(两者等价),用于在 WHERE 子句中进行复杂的字符串模式匹配。以下是核心用法和示例: 一、基础语法 SELECT column_name FROM table_name WHERE column_name REGEXP pattern; …...
Java多线程实现之Thread类深度解析
Java多线程实现之Thread类深度解析 一、多线程基础概念1.1 什么是线程1.2 多线程的优势1.3 Java多线程模型 二、Thread类的基本结构与构造函数2.1 Thread类的继承关系2.2 构造函数 三、创建和启动线程3.1 继承Thread类创建线程3.2 实现Runnable接口创建线程 四、Thread类的核心…...