当前位置: 首页 > news >正文

Java实现pdf转图片

第一步

 <dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.32</version> <!-- 请检查最新版本 -->
</dependency>

第二步

package com.example.demo.file.pdf;import org.apache.pdfbox.pdmodel.PDDocument;
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.IOException;public class PdfToImageConverter {public static void main(String[] args) {String pdfFilePath = "C:\\Users\\EDY\\Desktop\\output\\output.pdf"; // PDF文件路径String outputDirectory = "C:\\Users\\EDY\\Desktop\\directory"; // 输出目录路径convertPdfToImages(pdfFilePath, outputDirectory);}public static void convertPdfToImages(String pdfFilePath, String outputDirectory) {try (PDDocument document = PDDocument.load(new File(pdfFilePath))) {PDFRenderer renderer = new PDFRenderer(document);File outputDir = new File(outputDirectory);// 确保输出目录存在if (!outputDir.exists() && !outputDir.mkdirs()) {throw new IOException("Unable to create output directory: " + outputDirectory);}for (int page = 0; page < document.getNumberOfPages(); ++page) {BufferedImage bim = renderer.renderImageWithDPI(page, 300, ImageType.RGB);String fileName = "page_" + (page + 1) + ".png";File outputFile = new File(outputDir, fileName);ImageIO.write(bim, "PNG", outputFile);}System.out.println("Picture created successfully!");} catch (IOException e) {e.printStackTrace();}}
}

http://www.lryc.cn/news/477943.html

相关文章:

  • 健身房管理新纪元:SpringBoot技术应用
  • Java之字符串分割转换List
  • RabbitMQ如何保证发送的消息可靠(RabbitMQ的Confirm模式和2.Return模式)
  • 适配器模式:类适配器与对象适配器
  • volatile原理
  • 【AI神器】SD(Stable Diffusion)一键安装包
  • lanqiaoOJ 1112:小王子双链表 ← STL list
  • C#WPF之快速理解MVVM模式
  • 微积分[1]|微积分的底层逻辑——解析几何、不等式与极限(含博主推荐的数理阅读教材共计21本书籍)
  • 1-磁盘建立空闲分区
  • 使用SearXNG-搭建个人搜索引擎(附国内可用Docker镜像源)
  • InnoDB 存储引擎<五>undo log, redo log,以及双写缓冲区
  • Find My运动耳机|苹果Find My技术与耳机结合,智能防丢,全球定位
  • 书生大模型实战营Linux+InternStudio 关卡任务
  • 研究实锤:别让大模型「想」太多,OpenAI o1准确率竟下降36.3%
  • C++游戏开发
  • ChatGPT中的RAG;大模型微调;通过正确的提问和回答数据进行问答系统的微调;
  • 6款IntelliJ IDEA插件,让Spring和Java开发如虎添翼
  • 源代码加密解决方案:文档加密与沙盒加密的比较分析
  • Spring Boot 与 Vue 共筑高校网上订餐卓越平台
  • 【数据仓库】Hive 拉链表实践
  • 【python_pandas_将列表按照某几列进行分组,再求和,按照原列表的字段顺序返回】
  • Vue的双向绑定
  • 谷歌浏览器安装 Vue.js devtools 插件
  • LWIP通信协议UDP发送、接收源码解析
  • Linux—进程学习-01
  • FR动态数据源插件支持配置模板中某个数据集进行数据连接的切换
  • epoll 技术为什么用rbtree而不用hashmap呢?
  • 关于Android Studio Koala Feature Drop | 2024.1.2下载不了插件的解决办法
  • 公共命名空间,2024年11月的笔记