工程加入依赖:
1
2
3
4
5
6
7
8
9
10
|
< dependency >
< groupId >org.apache.pdfbox</ groupId >
< artifactId >pdfbox</ artifactId >
< version >2.0.15</ version >
</ dependency >
< dependency >
< groupId >org.apache.pdfbox</ groupId >
< artifactId >pdfbox-tools</ artifactId >
< version >2.0.15</ version >
</ dependency >
|
pdf文件转图片:
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
|
public static List<String> pdf2Img(File pdfFile) {
if (pdfFile == null || !pdfFile.exists()) {
throw new RuntimeException( "pdf文件不能为空" );
}
String name = pdfFile.getName().substring( 0 , pdfFile.getName().lastIndexOf( "." ));
String targetPath = pdfFile.getParent() + File.separator + name;
List<String> imgList = new ArrayList<>();
try {
PDDocument doc = PDDocument.load(pdfFile);
// 页数
int pageCount = doc.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(doc);
for ( int i = 0 ; i < pageCount; i++) {
File targetFile = new File(targetPath + File.separator + name + "-" + (i + 1 ) + ".jpg" );
if (!targetFile.getParentFile().exists()) {
FileUtil.mkdir(targetFile.getParentFile());
}
pdfRenderer.renderImage(i);
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 105 , ImageType.RGB);
ImageIOUtil.writeImage(image, targetFile.getPath(), 105 );
imgList.add(targetFile.getPath());
}
} catch (IOException e) {
log.error( "文件转换异常" , e);
throw new RuntimeException( "文件转换异常,err=" + e.getMessage());
}
|
pdf转成一张图片:
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
|
/**
* pdf转成一张图片
*
* @param pdfFile pdf图片文件
* @return 图片地址
*/
public static String pdf2OneImg(File pdfFile) {
List<String> imgs = pdf2Img(pdfFile);
int len = imgs.size();
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int [][] ImageArrays = new int [len][];
for ( int i = 0 ; i < len; i++) {
try {
src[i] = new File(imgs.get(i));
if (!src[i].exists()) {
throw new RuntimeException( "文件【" + imgs.get(i) + "】不存在" );
}
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
log.error( "" , e);
throw new RuntimeException(e);
}
int width = images[i].getWidth();
int height = images[i].getHeight();
// 从图片中读取RGB 像素
ImageArrays[i] = new int [width * height];
ImageArrays[i] = images[i].getRGB( 0 , 0 , width, height, ImageArrays[i], 0 , width);
}
int dst_height = 0 ;
int dst_width = images[ 0 ].getWidth();
// 合成图片像素
for ( int i = 0 ; i < images.length; i++) {
dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
dst_height += images[i].getHeight();
}
if (dst_height < 1 ) {
throw new RuntimeException( "文件合成失败,合成后的图片文件高度=" + dst_height);
}
String name = pdfFile.getName().substring( 0 , pdfFile.getName().lastIndexOf( "." ));
String targetPath = pdfFile.getParent() + File.separator + name;
// 输出路径
File outFile = new File(targetPath + File.separator + name + "-bigone.jpg" );
// 生成新图片
try {
dst_width = images[ 0 ].getWidth();
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0 ;
for ( int i = 0 ; i < images.length; i++) {
ImageNew.setRGB( 0 , height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0 , dst_width);
height_i += images[i].getHeight();
}
ImageIO.write(ImageNew, "jpg" , outFile);
} catch (Exception e) {
log.error( "图片合并异常=" , e);
throw new RuntimeException(e);
}
return outFile.getPath();
}
|
到此这篇关于Java实现pdf转图片案例的文章就介绍到这了,更多相关Java实现pdf转图片内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/tanzhming/article/details/117930788