千家信息网

Java实现PDF切割、截取、合并工具类、转图片

发表于:2025-12-04 作者:千家信息网编辑
千家信息网最后更新 2025年12月04日,本篇内容介绍了"Java实现PDF切割、截取、合并工具类、转图片"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够
千家信息网最后更新 2025年12月04日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安全错误 数据库的锁怎样保障安全 印度信息技术和软件开发哪个好 战地5 服务器连接中断 LED贴片机软件开发厂家 我的世界魔塔服务器攻略 已重新连接服务器 奉贤区品牌数据库系统定做价格 英伟达服务器有多厉害 烟台dell服务器价格 网吧服务器自动死机是怎么回事 公安内部网络安全工作方案 数据库中创建窗体 全新网络安全时代 上海教育网络技术学院 网络安全宣传周上海地铁 网络安全管理系统功能的描述 大话西游第一个服务器 IBM云服务对软件开发的好处 祥云杯网络安全大赛试题 北京市网络安全局地址 网络安全周问答题 马鞍中学网络安全宣传周活动 数据库基层架构是什么意思 礼品包装设计软件开发 梦网网络技术 如何免费获取ccer数据库数据 河南搜客网络技术有限责任公司 北京联众恒通网络技术有限公司 数据库为啥设置主键错误 主流的流媒体服务器 免费数据库编程框架
0