PDF文件转jpg格式图片

 1 public static void pdfToJpg(String filepath) throws IOException{
 2         File file = new File(filepath);
 3         // 加载解析PDF文件
 4         PDDocument doc = PDDocument.load(file);
 5         PDFRenderer pdfRenderer = new PDFRenderer(doc);
 6         PDPageTree pages = doc.getPages();
 7         int pageCount = pages.getCount();
 8         for (int i = 0; i < pageCount; i++) {
 9             BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
10             ByteArrayOutputStream os = new ByteArrayOutputStream();
11             ImageIO.write(bim, "jpg", os);
12             byte[] datas = os.toByteArray();
13             //jpg文件转出路径
14             FileOutputStream fs = new FileOutputStream("e:/jpg/" + i + ".jpg");
15             fs.write(datas);
16             fs.close();
17         }
18     }

    本方法的实现需要用到 pdfbox-2.0.15.jar

 

posted @ 2019-06-06 09:57  奋进的小土豆  阅读(361)  评论(0编辑  收藏  举报