1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java生成多页pdf_java 多页pdf转化为多张图片

java生成多页pdf_java 多页pdf转化为多张图片

时间:2019-08-10 10:25:09

相关推荐

java生成多页pdf_java 多页pdf转化为多张图片

相关jar包:

com.itextpdf

itext-asian

5.2.0

org.apache.pdfbox

pdfbox

2.0.0

org.apache.pdfbox

fontbox

2.0.0

com.itextpdf

itextpdf

5.5.13

相关代码:

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.rendering.ImageType;

import org.apache.pdfbox.rendering.PDFRenderer;

import java.util.UUID;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

/**

* 将多页pdf转化为多张图片

* @param pdfPath 表示pdf的路径

* @return 转化后的图片的路径集合

* @throws IOException

*/

public static List pdfPathToImagePaths(String pdfPath) throws IOException {

log.info("将多页pdf转化为图片,pdf路径为:"+pdfPath);

File pdfFile = new File(pdfPath);

PDDocument pdDocument = PDDocument.load(pdfFile);

int pageCount = pdDocument.getNumberOfPages();

PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);

List imagePathList=new ArrayList<>();

String fileParent = pdfFile.getParent();

for (int pageIndex=0; pageIndex

String imgPath = fileParent + File.separator +UUID.randomUUID().toString()+".png";

BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 105, ImageType.RGB);

ImageIO.write(image, "png", new File(imgPath));

imagePathList.add(imgPath);

log.info("第{}张生成的图片路径为:{}",pageIndex,imgPath);

}

pdDocument.close();

return imagePathList;

}

这里的UUID.randomUUID()是产生一个随机的32位数字字母,作为文件名。

也可以自己去找些工具类,或者改用具体的时间作为文件名。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。