共计 969 个字符,预计需要花费 3 分钟才能阅读完成。
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>
正文完