Java实现PDF切割、截取、合并工具类、转图片
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容介绍了"Java实现PDF切割、截取、合并工具类、转图片"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够
千家信息网最后更新 2026年11月02日Java实现PDF切割、截取、合并工具类、转图片
本篇内容介绍了"Java实现PDF切割、截取、合并工具类、转图片"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
依赖导入
com.itextpdf itextpdf 5.5.13 org.apache.pdfbox pdfbox 2.0.15 org.apache.pdfbox fontbox 2.0.15
工具代码
import com.itextpdf.text.Document;import com.itextpdf.text.pdf.PdfCopy;import com.itextpdf.text.pdf.PdfImportedPage;import com.itextpdf.text.pdf.PdfReader;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;/** * @author zh * @data 2019/10/28 16:20 * 说明:TODO */public class TestPDF { public static void main(String[] args) throws IOException { } /** * pdf拷贝到新文件, * @param pdfFile * @param newFile * @param from 从第几页考 * @param end 考到第几页 */ public static void copyPdfFile(String pdfFile, String newFile, int from, int end) { Document document = null; PdfCopy copy = null; try { PdfReader reader = new PdfReader(pdfFile); int n = reader.getNumberOfPages(); if (end == 0) { end = n; } ArrayList savepaths = new ArrayList(); String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\") + 1); String savepath = staticpath + newFile; savepaths.add(savepath); document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0))); document.open(); for (int j = from; j <= end; j++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } document.close(); }catch (Exception e){ e.printStackTrace(); } } /** * 将所有的pdf切割成一页 */ public static void cutOnePageFormPDF(String pdfFile, String toPath,String fileName){ Document document = null; PdfCopy copy = null; try { PdfReader reader = new PdfReader(pdfFile); int n = reader.getNumberOfPages(); for(int i=1; i <= n; i++){ document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(toPath+fileName+"-"+i+".pdf")); document.open(); document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, i); copy.addPage(page); document.close(); } System.out.println(n); }catch (Exception e){ e.printStackTrace(); } } /** * 多个pdf合并 */ public static void sumPDFFile(String newfilePath, String... filePaths){ int length = filePaths.length; Document document = null; PdfCopy copy = null; try { if(length > 0){ document = new Document(new PdfReader(filePaths[0]).getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(newfilePath)); document.open(); for(int i=0; i < length; i++){ PdfReader reader = new PdfReader(filePaths[i]); for(int j=1; j <= reader.getNumberOfPages(); j++){ document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } } document.close(); } } catch (Exception e) { e.printStackTrace(); } } /** * 将PDF按页数每页转换成一个jpg图片 * @param filePath * @return */ public static List pdfToImagePath(String filePath){ List list = new ArrayList<>(); String fileDirectory = filePath.substring(0,filePath.lastIndexOf("."));//获取去除后缀的文件路径 String imagePath; File file = new File(filePath); try { File f = new File(fileDirectory); if(!f.exists()){ f.mkdir(); } PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for(int i=0; i piclist = new ArrayList(); for (int i = 0; i < actSize; i++) { BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i,130, org.apache.pdfbox.rendering.ImageType.RGB); piclist.add(image); } yPic(piclist, outpath); is.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同 * @param piclist 文件流数组 * @param outPath 输出路径 */ public static void yPic(List piclist, String outPath) {// 纵向处理图片 if (piclist == null || piclist.size() <= 0) { System.out.println("图片数组为空!"); return; } try { int height = 0, // 总高度 width = 0, // 总宽度 _height = 0, // 临时的高度 , 或保存偏移高度 __height = 0, // 临时的高度,主要保存每个高度 picNum = piclist.size();// 图片的数量 int[] heightArray = new int[picNum]; // 保存每个文件的高度 BufferedImage buffer = null; // 保存图片流 List imgRGB = new ArrayList(); // 保存所有的图片的RGB int[] _imgRGB; // 保存一张图片中的RGB数据 for (int i = 0; i < picNum; i++) { buffer = piclist.get(i); heightArray[i] = _height = buffer.getHeight();// 图片高度 if (i == 0) { width = buffer.getWidth();// 图片宽度 } height += _height; // 获取总高度 _imgRGB = new int[width * _height];// 从图片中读取RGB _imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width); imgRGB.add(_imgRGB); } _height = 0; // 设置偏移高度为0 // 生成新图片 BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < picNum; i++) { __height = heightArray[i]; if (i != 0) _height += __height; // 计算偏移高度 imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 写入流中 } File outFile = new File(outPath); ImageIO.write(imageResult, "jpg", outFile);// 写图片 } catch (Exception e) { e.printStackTrace(); } }} "Java实现PDF切割、截取、合并工具类、转图片"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
图片
高度
文件
宽度
工具
参数
偏移
相同
像素
内容
数组
方式
更多
知识
路径
j++
输出
实用
学有所成
接下来
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
喔噻互联网科技有限公司
互联网科技工作委员会
互联网扶贫科技
互联网 科技创新
齐家互联网科技
安徽鑫晟辉互联网科技
互联网金融科技平台
撒么互联网科技公司
中正互联网科技招聘
新华互联网科技校企合作
湖南诚度互联网科技有限公司
晓花互联网科技有限公司网址
迪庆互联网科技招生
中和天地互联网科技有限公司
深圳无忧互联网科技
黑云互联网科技有限公司
东营紫格互联网科技
新氧科技和美国互联网有关系吗
互联网对科技影响
数字科技和互联网金融哪个好
有格互联网科技
互联网科技网
齐家互联网科技
上海喔噻互联网科技
北京华百购互联网科技有限公司
世界互联网科技公司市值排名
链环科技传统企业怎么转型互联网
小米互联网科技园入驻
互联网公司高科技产品
上海创道互联网络科技有限公司