PDDocument document = null;
String pdfdir = "d:/pdf2image/";
File file = new File(pdfdir);
if(!file.exists()||!file.isDirectory()){System.out.println("the diretory is not exist");
return;
}
File [] files = file.listFiles();
for(File pdf :files){String filepath = pdf.getAbsolutePath();
if(!filepath.contains(".pdf")){continue;}
String filename = pdf.getName();
String imageNamePrefix = filename.substring(0,filename.lastIndexOf("."));
File imagedir = new File(pdfdir+imageNamePrefix);
if(!imagedir.exists()){imagedir.mkdir();
}
InputStream inputStream = new FileInputStream(pdf);
document = PDDocument.load(inputStream);
PDFRenderer renderer = new PDFRenderer(document);
int pageCount = document.getNumberOfPages();
System.out.println("共"+pageCount+"页.");
for (int i = 0; i < pageCount; i++) {BufferedImage image = renderer.renderImage(i, 1.9f);
ImageIO.write(image, "jpg", new File(pdfdir+imageNamePrefix+File.separator+ i + ".jpg"));
}
}
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.18</version>
</dependency>