如何使用java实现截取PDF指定页并进行图片格式转换功能
发表于:2025-11-17 作者:千家信息网编辑
千家信息网最后更新 2025年11月17日,这篇文章给大家分享的是有关如何使用java实现截取PDF指定页并进行图片格式转换功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1、引入依赖 org.apache.pd
千家信息网最后更新 2025年11月17日如何使用java实现截取PDF指定页并进行图片格式转换功能
这篇文章给大家分享的是有关如何使用java实现截取PDF指定页并进行图片格式转换功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
1、引入依赖
org.apache.pdfbox pdfbox 2.0.16 org.apache.pdfbox fontbox 2.0.16
jar包下载地址:
https://mvnrepository.com/artifact/org.apache.pdfbox/pdfboxhttps://mvnrepository.com/artifact/org.apache.pdfbox/fontbox
2、实现DEMO
package com.dddpeter.app;import org.apache.pdfbox.multipdf.Splitter;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;import java.awt.image.BufferedImage;import java.io.*;import java.util.List;import java.util.ListIterator;public class PDFUtils { public static String splitPdf(int pageNum, String source, String dest) { File indexFile = new File(source); File outFile = new File(dest); PDDocument document = null; try { document = PDDocument.load(indexFile); // document.getNumberOfPages(); Splitter splitter = new Splitter(); splitter.setStartPage(pageNum); splitter.setEndPage(pageNum); List pages = splitter.split(document); ListIterator iterator = pages.listIterator(); while (iterator.hasNext()) { PDDocument pd = iterator.next(); if (outFile.exists()) { outFile.delete(); } pd.save(outFile); pd.close(); if (outFile.exists()) { return outFile.getPath(); } } document.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void pdfFileToImage(File pdffile,String targetPath){ try { FileInputStream instream = new FileInputStream(pdffile); InputStream byteInputStream=null; try { PDDocument doc = PDDocument.load(instream); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); if (pageCount > 0) { BufferedImage image = renderer.renderImage(0, 4.0f); image.flush(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut; imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(image, "png", imOut); byteInputStream = new ByteArrayInputStream(bs.toByteArray()); byteInputStream.close(); } doc.close(); } catch (IOException e) { e.printStackTrace(); } File uploadFile = new File(targetPath); FileOutputStream fops; fops = new FileOutputStream(uploadFile); fops.write(readInputStream(byteInputStream)); fops.flush(); fops.close(); } catch (Exception e) { e.printStackTrace(); } } public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } public static void main(String[] args) { String path = splitPdf(4,"D:\\data\\11.pdf","D:\\data\\out11.pdf"); File file =new File(path); //上传的是png格式的图片结尾 String targetfile="D:\\data\\out11.png"; pdfFileToImage(file,targetfile); }} 感谢各位的阅读!关于"如何使用java实现截取PDF指定页并进行图片格式转换功能"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
图片
格式
功能
内容
更多
篇文章
不错
实用
地址
文章
看吧
知识
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
国外服务器 备案
重庆诚航驰互联网科技
数据库基于日志延迟修改技术
数据库并发操作采用什么技术
计算机网络技术专业最好的就业
centos 服务器配置
杭电网络安全院研究生
服务器关机之后灯还亮着
娄底九小网络安全教育
git服务器配置ssh
微擎数据库用户名密码
网络技术加QQ
霞浦县让彻网络技术服务部
wow审判服务器咋样
网络技术管理技术内容
衡阳市网络安全工程
内部服务器的管理
服务器内存为什么便宜
数据库版本管理入门
联想出厂服务器密码是什么
北斗服务器怎么同步网络设备
网络安全手繁体字
ssh 进入服务器
软件开发后的变化
海门市天气预报软件开发
骏域网络技术电话
服务器硬盘保存位置
象山直销软件开发工程
软件开发指标度量
服务器系统集成服务价格