最近项目中要实现将PDF文件转换成image图片,以便pc端和移动端预览,网上搜索很久后发现spire.pdf很好用。
如何通过maven引用:https://www.cnblogs.com/Yesi/p/10483565.html
如果PDF文件页数小于10张,可以使用官方提供的免费版,非免费版的在转换成的图片中顶部会存在 Evaluation Warning : The document was created with Spire.PDF for .NET. 水印
去除方式:
public class SpirePdfUtil { protected static Log log = LogFactory.getLog(SpirePdfUtil.class); public static int aspPdfToJpg(String sourPath ,String savePath){ int flag = 0; PdfDocument pdfDocument = new PdfDocument(); pdfDocument.loadFromFile(sourPath); BufferedImage bufferedImage = null; try{ for (int i=0;i<pdfDocument.getPages().getCount();i++){ bufferedImage = pdfDocument.saveAsImage(i); bufferedImage = bufferedImage.getSubimage(bufferedImage.getMinX(),15,bufferedImage.getWidth(),bufferedImage.getHeight()-15); File saveFile = new File(savePath+i+".jpg"); if(!saveFile.exists()){ saveFile.mkdirs(); } ImageIO.write(bufferedImage,"jpg",saveFile); bufferedImage.flush(); } flag = 1; }catch (Exception e){ e.printStackTrace(); }finally { } return flag; } }