大青葱 2021-12-21 13:56 采纳率: 0%
浏览 2011
已结题

java使用Apache PDFBox进行pdf转图片内存溢出,因dpi太大

在使用Apache PDFBox转图片时候报错了,之前使用一直是正常的我一看pdf只有一页,然后用了其他工具报了一个pdf的宽高太大,所以我将dpi从200改为30重新转换就正常出来了,不过有一个比较尴尬的问题就是没办法自己识别自适应去调节dpi,所以在这里想请有遇到这种问题的小伙伴分享一下方法。

报错是这一行

 BufferedImage image = renderer.renderImageWithDPI(i, dpi);


    public List<File> convertPdfToImage(File file, String destination,String imageName,int dpi,int maxWidth,int maxHeight) throws Exception {

        File destinationFile = new File(destination);

        if (!destinationFile.exists()) {
            destinationFile.mkdir();
            System.out.println("DESTINATION FOLDER CREATED -> " + destinationFile.getAbsolutePath());
        }else if(destinationFile.exists()){
            System.out.println("DESTINATION FOLDER ALLREADY CREATED!!!");
        }else{
            System.out.println("DESTINATION FOLDER NOT CREATED!!!");
        }

        if (file.exists()) {
//            PDDocument doc = PDDocument.load(file);
            PDDocument doc = Loader.loadPDF(file);



            PDFRenderer renderer = new PDFRenderer(doc);
            List<File> fileList = new ArrayList<File>();

            String fileName = file.getName().replace(".pdf", "");
            System.out.println("CONVERTER START.....");

            for (int i = 0; i < doc.getNumberOfPages(); i++) {
                // default image files path: original file path
                // if necessary, file.getParent() + "/" => another path
                File fileTemp = new File(destination + imageName + (i+1)); // jpg or png


                BufferedImage image = renderer.renderImageWithDPI(i, dpi);


                System.out.println("输出第"+i+"张图片 ,宽度:"+ image.getWidth()+"高度:"+image.getHeight());

                if (image.getWidth()>maxWidth || image.getHeight()>maxHeight){
                    float widthRatio=(float) image.getWidth()/(float)maxWidth;
                    float heightRatio=(float)image.getHeight()/(float)maxHeight;
                    float Ratio = widthRatio>heightRatio?widthRatio:heightRatio;

                    System.out.println(Ratio);

                    int newWidth = Math.round((float)image.getWidth()/(float)Ratio);
                    int heightWidth = Math.round((float)image.getHeight()/(float)Ratio);
                    image=resizeBufferedImage(image,newWidth,heightWidth,true);
                    System.out.println("输出第"+i+"张图片,宽高超出,进行缩放新的 ,宽度:"+ newWidth+"高度:"+heightWidth);
                }
                ImageIO.write(image, "JPEG", fileTemp); // JPEG or PNG
                fileList.add(fileTemp);
            }
            doc.close();
            System.out.println("CONVERTER STOPTED.....");
            System.out.println("IMAGE SAVED AT -> " + destinationFile.getAbsolutePath());
            return fileList;
        } else {
            System.err.println(file.getName() + " FILE DOES NOT EXIST");
        }
        return null;
    }

  • 写回答

2条回答 默认 最新

  • 大青葱 2021-12-22 19:01
    关注

    我成功在类中找到了获取宽高的函数,这样应该可以自适应出dpi

    主要代码

             PDPage page = doc.getPage(i);
                    PDRectangle cropbBox = page.getCropBox();
                    float widthPt = cropbBox.getWidth();
                    float heightPt = cropbBox.getHeight();
    
      PDDocument doc = Loader.loadPDF(file);
    
    
    
                PDFRenderer renderer = new PDFRenderer(doc);
                List<File> fileList = new ArrayList<File>();
    
                String fileName = file.getName().replace(".pdf", "");
                System.out.println("CONVERTER START.....");
    
                for (int i = 0; i < doc.getNumberOfPages(); i++) {
                    // default image files path: original file path
                    // if necessary, file.getParent() + "/" => another path
                    File fileTemp = new File(destination + imageName + (i+1)); // jpg or png
    
                    PDPage page = doc.getPage(i);
                    PDRectangle cropbBox = page.getCropBox();
                    float widthPt = cropbBox.getWidth();
                    float heightPt = cropbBox.getHeight();
                    System.out.println("测试获取pdf宽高....."+widthPt);
                    System.out.println("测试获取pdf宽高....."+heightPt);
    }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月22日
  • 创建了问题 12月21日