java-使用icepdf实现pdf转换成png
下载icepdf的架包,并导入项目中,这里用到4个,如下:
/** * 本地pdf文件转png */ public static int pdf2pngByFile(String target){ String filePath = target; Document document = new Document(); // System.out.println("开始转png"); try { document.setFile(filePath); float scale = 1.5f;// 缩放比例(大图) // float scale = 0.2f;// 缩放比例(小图) float rotation = 0f;// 旋转角度 int pageSize = document.getNumberOfPages(); for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale); RenderedImage rendImage = image; // try { // File file = new File("D:/fileUpload/ftpDownload/icepdf_a" + i + ".png"); // // 这里png作用是:格式是jpg但有png清晰度 // ImageIO.write(rendImage, "png", file); // } catch (IOException e) { // e.printStackTrace(); // } try { // WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); // ServletContext servletContext = webApplicationContext.getServletContext(); // File contextPath = new File(servletContext.getRealPath("/")); // 项目根目录 // File uploadPath = new File( // contextPath.getParentFile().getAbsoluteFile() + File.separator + "uploadFiles"); // 上传图片存放目录 File uploadPath = new File(target); String fileName = getPathWithName(target); File file1 = new File(fileName); if (!file1.exists()) { file1.mkdirs(); } // System.out.println("地址=" + uploadPath.getAbsolutePath() + "/icepdf_a" + i + ".png" + "\n"); File file = new File(fileName + "\\" + i + ".png"); // 这里png作用是:格式是jpg但有png清晰度 ImageIO.write(rendImage, "png", file); } catch (IOException e) { e.printStackTrace(); } image.flush(); } document.dispose(); System.out.println("png_ok"); System.out.println("pageSize="+pageSize); return pageSize; } catch (Exception e1) { e1.printStackTrace(); } return 0; }
参考:
http://www.cnblogs.com/pcheng/p/5704470.html