1、引入pom依赖
<!--icepdf,用来将pdf文件转换为图片的依赖-->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>6.2.2</version>
<exclusions>
<exclusion>
<groupId>javax.media</groupId>
<artifactId>jai_core</artifactId>
</exclusion>
</exclusions>
</dependency>
2、测试代码
@Test
void testSavePdf() throws InterruptedException {
String filePath = "E:\\david\\在线教育.pdf";
Document document = new Document();
try {
document.setFile(filePath);
} catch (Exception ex) {
}
// save page caputres to file.
float scale = 2f;
float rotation = 0f;
// Paint each pages content to an image and write the image to fil
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)document.getPageImage(i, GraphicsRenderingHints.SCREEN,
Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
// capture the page image to file
try {
System.out.println("/t capturing page " + i);
File file = new File("E:\\var\\tmp\\image_" + i + ".png");
ImageIO.write(rendImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
// clean up resources
document.dispose();
}