您的位置:首页 > 财经 > 金融 > pdf文件转图片,base64或保存到本地

pdf文件转图片,base64或保存到本地

2024/11/17 12:12:12 来源:https://blog.csdn.net/FenceRain/article/details/142213683  浏览:    关键词:pdf文件转图片,base64或保存到本地

pdf转图片,需要引入pdfbox依赖

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version>
</dependency>
@RequestMapping("pdfToImg")
public void pdfToImg(){PDDocument document = null;InputStream inputStream = null;ByteArrayOutputStream outputStream = null;try{inputStream = new FileInputStream(new File("F:\\贴纸.pdf"));document = PDDocument.load(inputStream);PDFRenderer renderer = new PDFRenderer(document);int pageSize = document.getNumberOfPages();String text = "";// 一页一页读取outputStream = new ByteArrayOutputStream();for (int i = 0; i < pageSize; i++) {//每一页通过分辨率和颜色值进行转化BufferedImage bufferedImage = renderer.renderImageWithDPI(i, 300);//    ImageIO.write(bufferedImage,"png",new File("F:\\下载\\img_"+i+".png"));ImageIO.write(bufferedImage,"png",outputStream);outputStream.flush();}byte[] imagesBytes = outputStream.toByteArray();String imageBase64 = Base64.getEncoder().encodeToString(imagesBytes);System.out.println(imageBase64);}catch (Exception e){System.out.println(e.getMessage());}finally {try {if (document != null) {document.close();}if(inputStream != null){inputStream.close();}if(outputStream != null){outputStream.close();}} catch (IOException e) {System.out.println(e.getMessage());}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com