zoukankan      html  css  js  c++  java
  • 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" + "
    ");
                        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

  • 相关阅读:
    如何利用 iTunes 把 m4a/wav 文件转成 MP3 格式
    The best way to learn a programming language
    琼瑶哀悼丈夫去世
    与“芯片”相关的专业有哪些?
    君子使物,不为物使
    SRID (空间引用识别号, 坐标系)【转】
    编码
    test
    剪贴板神器:Ditto
    写Markdown博客时遇到的一些问题
  • 原文地址:https://www.cnblogs.com/lijianda/p/10725526.html
Copyright © 2011-2022 走看看