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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM