将PDF转换为图片(支持PDF含有多页内容)
2017-03-15 17:40
411 查看
/***
*
* @param source 源文件
* @param target 目标文件
* @param n 缩放系数
*/
private static void changePdfToImg(String source,String target,int n) {
try {
File file = new File(source);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
BufferedImage tag = new BufferedImage(595 * 2, 842 * 2 * pdffile.getNumPages(), BufferedImage.TYPE_INT_RGB);
for (int i = 1; i <= pdffile.getNumPages(); i++) {
PDFPage page = pdffile.getPage(i);
java.awt.Rectangle rect = new java.awt.Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
java.awt.Image img = page.getImage((int)rect.getWidth()* n, (int)rect.getHeight() * n, rect,
null,
true,
true
);
tag.getGraphics().drawImage(img, 0, (i-1)*rect.height*n, rect.width * n, rect.height * n, null);
}
FileOutputStream out = new FileOutputStream(target);
ImageIO.write(tag, "jpg", out);
out.close();
channel.close();
raf.close();
buf.clear();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
*
* @param source 源文件
* @param target 目标文件
* @param n 缩放系数
*/
private static void changePdfToImg(String source,String target,int n) {
try {
File file = new File(source);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
BufferedImage tag = new BufferedImage(595 * 2, 842 * 2 * pdffile.getNumPages(), BufferedImage.TYPE_INT_RGB);
for (int i = 1; i <= pdffile.getNumPages(); i++) {
PDFPage page = pdffile.getPage(i);
java.awt.Rectangle rect = new java.awt.Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
java.awt.Image img = page.getImage((int)rect.getWidth()* n, (int)rect.getHeight() * n, rect,
null,
true,
true
);
tag.getGraphics().drawImage(img, 0, (i-1)*rect.height*n, rect.width * n, rect.height * n, null);
}
FileOutputStream out = new FileOutputStream(target);
ImageIO.write(tag, "jpg", out);
out.close();
channel.close();
raf.close();
buf.clear();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
相关文章推荐
- java 将pdf多页转换成一张图片,支持指定页数
- pdf文件内容如何转换成图片
- 将pdf文档内容转换成jpg图片的方法
- 怎么把pdf文件内容都转换成jpg图片
- 支持导出PDF文件并转换成图片格式的PDF查看控件Spire.PDFViewer
- 将多页PDF文件转换为多张JPG图片
- C#将图片转换成pdf
- 【更新】Spire.PDF for .NET V3.9.285发布 | 支持将PDF转换为HTML
- C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)
- CPDF通用工具之四——PDF水印工具增强版(支持大部分图片格式)
- 如何免费在线将pdf转换成jpg图片
- android webview将网页内容导出为图片和PDF方法
- PDF转换成图片-11种方案
- 点击按钮切换相应的内容,可用于转换图片
- 怎样将图片迅速转换成pdf格式
- jpg图片如何转换pdf
- c# PDF 转换成图片
- 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】
- Ubuntu下Java使用pdfbox将pdf转换为图片的方法及问题
- 在线pdf如何转换成jpg图片格式