本文实例为大家分享了java使用icepdf将pdf文件按页转成图片的具体代码,供大家参考,具体内容如下
maven icepdf包,这里过滤掉jai-core
1
2
3
4
5
6
7
8
9
10
11
|
<dependency>
<groupid>org.icepdf.os</groupid>
<artifactid>icepdf-core</artifactid>
<version> 6.1 . 2 </version>
<exclusions>
<exclusion>
<groupid>javax.media</groupid>
<artifactid>jai-core</artifactid>
</exclusion>
</exclusions>
</dependency>
|
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.text.decimalformat;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import java.util.map;
import javax.imageio.iioimage;
import javax.imageio.imageio;
import javax.imageio.imagewriter;
import javax.imageio.stream.imageoutputstream;
import org.apache.commons.logging.logfactory;
import org.icepdf.core.exceptions.pdfexception;
import org.icepdf.core.exceptions.pdfsecurityexception;
import org.icepdf.core.pobjects.document;
import org.icepdf.core.pobjects.page;
import org.icepdf.core.util.graphicsrenderinghints;
public class test20160929 {
public static final string filetype_jpg = "jpg" ;
/**
*
* 将指定的pdf文件转换为指定路径的图片
*
* @param filepath 原文件路径,例如d:/test/test.pdf
*
* @param imagepath 图片生成路径,例如 d:/test/
*
* @param zoom 缩略图显示倍数,1表示不缩放,0.3则缩小到30%
*
*/
public static void tranfer(string filepath, string imagepath, float zoom) throws pdfexception, pdfsecurityexception, ioexception {
document document = null ;
float rotation = 0f;
document = new document();
document.setfile(filepath);
int maxpages = document.getpagetree().getnumberofpages();
for ( int i = 0 ; i < maxpages; i++) {
bufferedimage img = (bufferedimage) document.getpageimage(i, graphicsrenderinghints.screen, page.boundary_cropbox, rotation, zoom);
iterator iter = imageio.getimagewritersbysuffix(filetype_jpg);
imagewriter writer = (imagewriter) iter.next();
file outfile = new file(imagepath + new file(filepath).getname() + "_" + new decimalformat( "000" ).format(i) + "." + filetype_jpg);
fileoutputstream out = new fileoutputstream(outfile);
imageoutputstream outimage = imageio.createimageoutputstream(out);
writer.setoutput(outimage);
writer.write( new iioimage(img, null , null ));
}
system.out.println( "转换完成" );
}
public static void main(string[] args) throws pdfexception, pdfsecurityexception, ioexception {
tranfer( "d:/test/test.pdf" , "d:/test/" , 1 );
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u013024120/article/details/53584520