首页 / JAVA / java PDF转JPG
java PDF转JPG
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java PDF转JPG,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2251字,纯文字阅读大概需要4分钟。
内容图文
![java PDF转JPG](/upload/InfoBanner/zyjiaocheng/620/470576620bd04c9e972a6e29df27d2b4.jpg)
第一步:
引入jar:
<!-- PDF转jpg -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<!-- PDF转jpg -->
第二步:
java代码:
package cn.com.XX.utils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
/**
*
* @author zj
*
*/
public class Pdf2jpgUtils {
public static void main(String args[]){
pdfbox();
}
static void pdfbox() {
PDDocument doc = null;
ByteArrayOutputStream os = null;
InputStream stream = null;
OutputStream out = null;
/*InputStream is = null;
OutputStream responseOut = null;*/
try{
long start = System.currentTimeMillis();
//pdf路径
stream = new FileInputStream(new File("D:/pdf.pdf"));
// 加载解析PDF文件
doc = PDDocument.load(stream);
PDFRenderer pdfRenderer = new PDFRenderer(doc);
PDPageTree pages = doc.getPages();
int pageCount = pages.getCount();
for (int i = 0; i < pageCount; i++) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
os = new ByteArrayOutputStream();
ImageIO.write(bim, "jpg", os);
byte[] datas = os.toByteArray();
//jpg文件转出路径
out = new FileOutputStream("D:\\pdf\\abc" + i + ".jpg");
out.write(datas);
//通过response输出流
/* is = new ByteArrayInputStream(datas);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) > 0) {
responseOut.write(buffer, 0, len);
}
responseOut.flush();*/
}
long end = System.currentTimeMillis();
long time = (end - start);
System.out.println("pdf转jpg耗时: " + time);
}catch (Exception e){
e.printStackTrace();
}finally {
if (doc != null) {
try {
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (responseOut != null) {
try {
responseOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
}
}
内容总结
以上是互联网集市为您收集整理的java PDF转JPG全部内容,希望文章能够帮你解决java PDF转JPG所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。