Spring Boot pdf文件转图片
PDF转图片工具类
亲测可以使用,无需下载其他jar包
pom.xml
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.20</version></dependency>
PDFToIMG.java
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import java.util.List;import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.springframework.stereotype.Component;/*** @description: PDF转图片工具类* @author: LCHYUE* @time: 2021/6/15 13:35*/
@Component
public class PDFToIMG {public final static String IMG_TYPE_JPG = "jpg";public final static String IMG_TYPE_PNG = "png";// public static void main(String[] args) throws IOException {// App app = new App();
// app.pdf2img("D:\\测试文件.pdf", "D:", IMG_TYPE_PNG);
PDDocument pdDocument = app.pdfInfo("D:\\api.pdf");
// }/*** @param pdfPath pdf文件的路径* @param savePath 图片保存的地址*/public void pdf2img(String pdfPath, String savePath) {//imgType 图片保存方式String imgType = IMG_TYPE_PNG;
// String fileName = pdfPath.substring(pdfPath.lastIndexOf("\\") + 1, pdfPath.length());/*---window---*/String fileName = pdfPath.substring(pdfPath.lastIndexOf(File.separator) + 1, pdfPath.length());/*---linux---*/fileName = fileName.substring(0, fileName.lastIndexOf("."));InputStream is = null;PDDocument pdDocument = null;try {is = new BufferedInputStream(new FileInputStream(pdfPath));PDFParser parser = new PDFParser(is);parser.parse();pdDocument = parser.getPDDocument();List pages = pdDocument.getDocumentCatalog().getAllPages();for (int i = 0; i < pages.size(); i++) {// String saveFileName = savePath + "\\" + fileName + i + "." + imgType; /*---window---*/String saveFileName = savePath + File.separator + fileName + i + "." + imgType;/*---linux---*/PDPage page = (PDPage)pages.get(i);pdfPage2Img(page, saveFileName, imgType);}} catch (Exception e) {e.printStackTrace();} finally {if (pdDocument != null) {try {pdDocument.close();} catch (IOException e) {e.printStackTrace();}}}}/*** pdf页转换成图片** @param page* @param saveFileName* @throws IOException*/public void pdfPage2Img(PDPage page, String saveFileName, String imgType) throws IOException {BufferedImage img_temp = page.convertToImage();Iterator it = ImageIO.getImageWritersBySuffix(imgType);ImageWriter writer = (ImageWriter) it.next();ImageOutputStream imageout = ImageIO.createImageOutputStream(new FileOutputStream(saveFileName));writer.setOutput(imageout);writer.write(new IIOImage(img_temp, null, null));}public PDDocument pdfInfo(String filePath) throws IOException {InputStream is = new BufferedInputStream(new FileInputStream(filePath));PDFParser parser = new PDFParser(is);parser.parse();PDDocument pdDocument = parser.getPDDocument();System.out.println("pageNum:" + pdDocument.getNumberOfPages());return pdDocument;}public void createPdf() throws COSVisitorException, IOException {PDDocument document = new PDDocument();PDPage blankPage = new PDPage();document.addPage(blankPage);document.save("D:\\test.pdf");document.close();}
}
Spring Boot pdf文件转图片相关推荐
- java byte 图片浏览器直接显示_以Spring Boot的方式显示图片或下载文件到浏览器的示例代码...
以Java web的方式显示图片到浏览器以Java web的方式下载服务器文件到浏览器 以Spring Boot的方式显示图片或下载文件到浏览器 请求例子:http://localhost:8080/ ...
- 项目_功能模块_基于Spring Boot的文件上传下载功能的设计与实现
文章目录 基于Spring Boot的文件上传下载功能模块的设计与实现 1.前言 2.技术栈 3.关键源码 4.实现效果 4.1.登录 4.2.文件列表 4.3.上传文件测试 4.3.1.测试图片 4 ...
- 上传pdf文件转图片翻页小工具,遇到JSP form中type=file的文件上传及后台Part处理问题(后台取值为null)
最近公司要求做一个上传pdf文件后将文件转换为翻页图片的小工具,我是这么做的 1.先找图片翻页的demo,微软有现有的产品turn.js感兴趣的可以自己看一下 ,有双页的有单页的,因为我司要求手机端使 ...
- Spring Boot解决无法访问图片的问题
Spring Boot解决无法访问图片的问题 参考文章: (1)Spring Boot解决无法访问图片的问题 (2)https://www.cnblogs.com/yang101/p/11442802 ...
- spring boot 字体文件等静态资源无法获取
spring boot 字体文件等静态资源无法获取 原因 原因maven打包时会过滤掉一些静态文件 解决办法 在pom.xml文件中配置静态资源过滤,然后再放行静态资源,这样就能让maven识别到那些 ...
- pdf转图片 java_有将pdf文件转图片的格式方法吗?
有时候我们为了方便传输,需要把图片转为PDF的格式,因为它所占的空间相对要小.但是因为pdf文件的安全性很好所以不能进行编辑,那我们又要进行编辑图片的时候,就又会把PDF文件格式转换成图片来进行编辑. ...
- 导出文件_一招解决PDF文件导出图片
将PDF文件保存为高清图片是经常需要进行的保存方式之一,因为PDF文件虽然安全性高,但是相比较图片而言,还是不易进行查看的,那么如何将PDF文件导出图片呢?以前也了解过很多方法,不过最好的当然不是截图 ...
- Spring boot yml文件的书写格式
Spring boot yml文件的书写格式 使用ide 创建好spring boot文件格式后https://blog.csdn.net/weixin_42292697/article/detail ...
- Java实现pdf文件转图片
Java实现pdf文件转图片 文章顺序是按照测试类- -Service- -Service实现类- -工具类- - POM依赖. test测试类里 pdfPath:存放pdf源文件的地方 imgflo ...
最新文章
- iptables配置-Linux系统安全防火墙
- wpf指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。_在60分钟内建立一个无代码应用程序...
- python使用缩进来体现代码之间的逻辑关系-Python使用缩进来体现代码之间的逻辑关系。...
- CSS 魔法系列:纯 CSS 绘制基本图形(圆、椭圆等)
- 基于Warshall算法的连通图及欧拉图判定方法
- C语言之strstr函数类似Java字符串的contain函数
- 要闻君说:微软拿下五角大楼超大云服务订单!恒大玩儿起了充电桩and新能源汽车?苹果公司要放弃自动驾驶业务?...
- ipad鼠标圆圈变成箭头_下一代 macOS 或将支持 Mac「投屏」到 iPad
- java根据sessionid获取session
- 大数据之-Hadoop完全分布式_集群的启动和停止方式总结---大数据之hadoop工作笔记0039
- 曾估值 45 亿美元!Apache Kafka 商业化公司 Confluent 计划上市:已申请 IPO
- 计算机上已安装某个第三方应用程序,防止电脑第三方软件“被”安装 3招彻底屏蔽不明软件...
- QuickPCB2005超级封装库与免费教程
- [sprd]Android Q修改 Launcher 上文件夹图标的大小和应用的图标大小一致
- 女程序员们的结婚要求
- MultiValueMap
- 试题 历届真题 时间显示【第十二届】【省赛】【B组】
- doc转docx文件会乱吗_docx怎么转换成doc?docx转doc方法汇总
- 物联全域定位技术漫谈
- 苹果回应巴西政府禁止销售不附赠充电器的iPhone;TikTok否认用户数据源代码泄露|极客头条...