标签:throws uuid rand detail port java 自己 dff 生成
相关jar包:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
相关代码:
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<String> 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<String> imagePathList=new ArrayList<>();
String fileParent = pdfFile.getParent();
for (int pageIndex=0; pageIndex<pageCount; 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位数字字母,作为文件名。
也可以自己去找些工具类,或者改用具体的时间作为文件名。
参考资料:
https://blog.csdn.net/u013405574/article/details/51086186
标签:throws uuid rand detail port java 自己 dff 生成
原文地址:https://www.cnblogs.com/expiator/p/11385670.html