pdf转图片
1.引入jar包
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>
2.编写代码测试
@Test
public void contextLoads() {
String fileAddress="C:\\Users\\intasect\\Desktop\\测试";
pdf2png(fileAddress,"kafka消息接口文档","jpg");
}
public static void pdf2png(String fileAddress,String filename,String type) {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File(fileAddress+"\\"+filename+".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
}
} catch (IOException e) {
e.printStackTrace();
}
}
3.结果