pdf转图片的两种方式(java)
最近在工作中遇到业务需要把pdf转成图片供前台预览,在网上查了一些方法,有的能用,有的缺东西,时间又比较急,没时间去看。在这里我整理了两种亲测可行的方式,节省大家时间带备忘:
1.pdf按页数转成多张PNG
/**
* @description pdf文件逐页转成png图片
**/
public class pdfToPng {
//测试方法
// public static void main(String[] args) throws Exception{
// pdf2Image("E:\\123.pdf", "E:\\", 300);
// }
/***
* PDF文件转PNG图片,全部页数
*
* @param PdfFilePath pdf完整路径
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @return
*/
public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) throws Exception {
File file = new File(PdfFilePath);
PDDocument pdDocument;
try {
String imgPDFPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
imgFolderPath = imgPDFPath + File.separator + imagePDFName;// 获取图片存放的文件夹路径
} else {
imgFolderPath = dstImgFolder + File.separator + imagePDFName;
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大转换后越清晰,相对转换速度越慢 */
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath = null;
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append(String.valueOf(i + 1));
imgFilePath.append(".png");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
}
System.out.println("PDF文档转PNG图片成功!");
} else {
System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
}
2.pdf文件转成一张JPG
/**
* @description pdf文件转成一张jpg图片
**/
public class pdfTojpg {
//测试方法
// public static void main(String[] args) throws Exception{
// File file = new File("E:\\123.pdf");
// InputStream input = new FileInputStream(file);
//
// useStream(input,"E:\\123.jpg");
// }
public static void useStream(InputStream is, String outpath) throws Exception {
try {
PDDocument pdf = PDDocument.load(is);
int actSize = pdf.getNumberOfPages();
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, 100,
ImageType.RGB);
piclist.add(image);
}
yPic(piclist, outpath);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同
*
* @param piclist 文件流数组
* @param outPath 输出路径
*/
public static void yPic(List<BufferedImage> 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<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的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();
}
}
}
pdf转图片的两种方式(java)相关推荐
- java之pdf转图片的几种方式,以及在使用过程中遇到的问题和处理方案
java之pdf转图片的几种方式,以及在使用过程中遇到的问题和处理方案 方式 pdfBox iText(方式同上) spire.pdf.free(方式同上) 遇到的问题 图片清晰度.这种方式都有这个问 ...
- 设置背景图片的两种方式,并解决手机端背景图片高度自适应问题
设置背景图片的两种方式,并解决手机端背景图片高度自适应问题 参考文章: (1)设置背景图片的两种方式,并解决手机端背景图片高度自适应问题 (2)https://www.cnblogs.com/Dark ...
- ImGui添加背景图片的两种方式
给ImGui添加背景图片的两种方式 最近在使用ImGui做客户端程序,想给窗口添加背景图片,但是作者的文档里面好像并没有讲如何添加背景图片,研究了下找到了两种方式. 第一种 创建一个和窗口一样大的Im ...
- Android加载GIF图片的两种方式
飞哥语录:得到一件东西最好的方式是让自己配得上它. 方式一:使用第三开源框架直接在布局文件中加载gif 1.在工程的build.gradle中添加如下 buildscript {repositorie ...
- java 产生随机数的两种方式,Java产生随机数的两种方式
Java产生随机数的两种方式 Java产生随机数的两种方式 在java中,我们可以通过两种方式来获取随机数(generating a random number)一种是大家熟悉的java.lang.M ...
- Android之从网络上获取图片的两种方式讲解:thread+handle和AsyncTask方式
从网络上获取图片是一个比较耗时的操作,放在主线程会导致阻塞主线程,响应超时,所以我们不能把它放在主线程里操作,必须放在一个子线程里,我打算采用两种方式去实现.1.采用thread去获取图片,获取到后通 ...
- java 产生随机数的两种方式,Java产生随机数的几种方式
一.在j2se里我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以把他乘以一定的数,比如说乘以100,他就是个100以内的随机,这个在 ...
- Word文档怎么转换为PDF格式?介绍两种方式
word应该是我们平日里接触最多的文件格式了,浏览编辑起来都很方便,但打印出来的格式却时不时就出错.这时可以将文档转成格式更稳定的PDF文件,那Word怎么转PDF呢?下面就给大家介绍两种方式,一分钟 ...
- Java代码读取图片的两种方式
不废话在,直接上代码: 方式一: 适用场景: 图片size小 方式二: 适用场景: 图片size大 细节点: java 代码读取图片的方式,从这点入手: 关注我的博客
最新文章
- 阿里云安全肖力:安全基础建设是企业数字化转型的基石
- Arm 发布移动端 v9 体系新架构,CPU、GPU、IP全囊括了
- maven 打包时缺少文件_(三)Java资源文件和路径相关扫盲
- MySQL 是如何利用索引的
- css html 字竖,CSS实现文字竖排 DIV CSS文字垂直竖列排版显示如何实现?
- CCleaner Free
- java 分级显示_上级部门与下级部门的分类显示
- oracle客户端查看版本号,Oracle 版本查看及版本号说明
- 【论文分享】ACL 2020 信息抽取与问答系统
- Mac菜单栏使用过程中遇到的问题及解决方法
- java案例代码11-学生类的录入,查询,对象的封装
- matches php,PHP 正则表达式 推荐
- 购物提醒(基于python的信息轰炸)
- html语言实现两数相加,HTML(2)
- 玩转树莓派(六) 部署Seafile搭建私有云
- jmeter性能测试步骤入门
- 带内部参考电压(VREFINT)校正的STM32 DMA 内置温度采集
- 关于怎样突破一个个微信投一票及人工刷票微信号推荐的方法和教程
- Efficient Image Dehazing with Boundary Constraint and Contextual Regularization
- 三星S10系列相关参数对比,S10e/S10/S10+/S10 5G
热门文章
- 泰国旅游必拍照打卡景点推荐,高清靓图欣赏
- 深入了解新朋友——面向对象
- docker之卷10
- 关于支持电脑登录选项设置
- AI+教育 I 69天流利说APP学习浅谈自适应学习
- Java小白修炼手册--第三阶段--WebBasic( Web前端)--CSS
- 官宣!2022汇佳学校毕业生捷报汇总
- OpenSSL命令之算法类大全
- “粽”使相距千里,也要长“酒”团圆
- 高级英语(张汉熙版)第一册学习笔记(原文及全文翻译)——8 - But What‘s a Dictionary For?(词典的用途究竟何在?)