java pdf转换jpg
2016-06-15 21:46
796 查看
/**
* 把PDF所有页转换为JPG, 并返回所有图片的路劲集合
* @param inputFilePath
* 图片路径,具体到文件名
* @param outputFilePath
* 输出目录, 不需要文件名
* @return
* @throws IOException
*/
public static List<String> Pdf2Jpg(String inputFilePath,
String outputFilePath) throws IOException {
List<String> outputFilePathList = new ArrayList<String>();
// load a pdf from a byte buffer
File file = new File(inputFilePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
System.out.println("页数: " + pdffile.getNumPages());
for (int i = 1; i <= pdffile.getNumPages(); i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null);
String outputFilePath2 = outputFilePath + System.currentTimeMillis() + ".jpg";
FileOutputStream out = new FileOutputStream(outputFilePath2); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG编码
out.close();
outputFilePathList.add(outputFilePath2);
}
return outputFilePathList;
}
* 把PDF所有页转换为JPG, 并返回所有图片的路劲集合
* @param inputFilePath
* 图片路径,具体到文件名
* @param outputFilePath
* 输出目录, 不需要文件名
* @return
* @throws IOException
*/
public static List<String> Pdf2Jpg(String inputFilePath,
String outputFilePath) throws IOException {
List<String> outputFilePathList = new ArrayList<String>();
// load a pdf from a byte buffer
File file = new File(inputFilePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
System.out.println("页数: " + pdffile.getNumPages());
for (int i = 1; i <= pdffile.getNumPages(); i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null);
String outputFilePath2 = outputFilePath + System.currentTimeMillis() + ".jpg";
FileOutputStream out = new FileOutputStream(outputFilePath2); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG编码
out.close();
outputFilePathList.add(outputFilePath2);
}
return outputFilePathList;
}
相关文章推荐
- Java语言编写矩阵转置
- Java反射学习总结(3)——反射的基本操作
- JAVA设计模式:代理模式
- 初学者对Spring MVC的认识
- JavaSE随笔——原码反码补码
- JavaWeb学习之HibernateDaoSupport使用
- javac -d . hello.java中的 -d .是什么作用?
- Java反射学习总结(2)——动态加载类
- 46. Spring Boot中使用AOP统一处理Web请求日志
- 反射(Reflect)
- 【SSM学习之路】用eclipse中的maven创建web项目
- Java记事
- java之修改并选择显示SQL Server数据库中的数据
- Java关键字final、static使用总结
- JavaWeb学习之Servlet3.0新特性详解
- Java多线程之内存可见性
- JavaBasic
- Spring MVC的实现原理
- Java关键字final、static使用总结
- Spring Framework Reference Documentation手册官网下载地址