相关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;
}
测试类
public static void main(String[] args) throws IOException {
String pdfPath = "E://opt//upFiles\\1《中国武术通史》上册_1635840894961.pdf";
String bookid = "《中国武术通史》上册";
log.info("==================================");
log.info("将多页pdf转化为图片,pdf路径为:"+pdfPath);
log.info("==================================");
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();
//带bookid的pdf路径
String bookidPath = fileParent + File.separator + bookid;
File file = new File(bookidPath + File.separator );
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
for (int pageIndex=0; pageIndex<pageCount; pageIndex++) {
// String imgPath = fileParent + File.separator + UUID.randomUUID().toString()+".png";
String imgPath = bookidPath + File.separator + pageIndex+".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();
}
网友评论