pdf 转 jpg
该方法是 pdf 的每一页,分别生成一个 jpg 文件
例:pdf 有6页 ======生成jpg======> 6个jpg文件,分别对应pdf的各页面
依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.13</version>
</dependency>
函数
直接返回 File
/**
* pdf 转 jpg 返回文件
*
* @param path 文件路径
* @return
* @throws IOException
*/
private static File pdfToJpg(String path) throws IOException {
File file = File.createTempFile("newImg", ".jpg");
PDDocument document = PDDocument.load(new File(path));
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
ImageIO.write(bim, "jpg", file);
}
return file;
}
扩展
1、直接生成到指定路径下
PDDocument document = PDDocument.load(new File("pdfPath"));
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
ImageIO.write(bim, "jpg", new FileOutputStream("路径"));
}
2、其他自己想吧
Leslie Lee 随笔