icepdf中使用BufferedImage时内存溢出的解决方法

最近项目中需要将pdf转成图片,网上找了各种开源的工具,发觉icepdf用的人比较多。
但是在实际使用过程中,遇到几个问题。

1. 出现jpeg2000的错误:

ImageIO missing required plug-in to read JPEG 2000 images. 
You can download the JAI ImageIO Tools from: http://www.oracle.com/technetwork/java/current-142188.html

解决方法:
需要下载jai-imageio-core-1.3.1.jar,jai-imageio-jpeg2000-1.3.0.jar这两个包。
把这两个jar包导入你的项目即可。
我是从Maven下载的:

	  
	    bintray-jai-imageio
	    jai-imageio at bintray
	    https://dl.bintray.com/jai-imageio/maven/
	    
	      false
	    
	  

  
	
	    com.github.jai-imageio
	    jai-imageio-core
	    1.3.1
	
	
	    com.github.jai-imageio
	    jai-imageio-jpeg2000
	    1.3.0
	
  

2. 可能是由于使用了BufferedImage,报了out of memory错误:
照着github上icepdf的例子,虽然转换少量pdf的时候没有问题,但是由于我这里转换的pdf比较多,所以out of memory了。
所以需要在BufferedImage使用完以后置null,告诉jvm可以回收资源,并在每次截图完成以后都调用System.gc(); 释放资源。
我的理解:设null是告诉jvm此资源可以回收,System.gc(); 是让系统回收资源,但不一定是回收你刚才设成null的资源,可能是回收其他没用的资源。
icepdf截图的代码如下:
package com.sun.pdfImage;

import java.awt.Graphics;
import java.awt.image.BufferedImage;  
import java.awt.image.RenderedImage;  
import java.io.File;  
import java.io.IOException;  
import javax.imageio.ImageIO;  
  
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;  
import org.icepdf.core.pobjects.PDimension;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;  
/*  
 * pdf 转 图片  
 */  
public class Icepdf {  
    public static void pdf2Pic(String pdfPath){  

    	File pdf = new File(pdfPath);
    	if (!pdf.exists()){
            System.out.println("pdf截图失败:" + pdfPath + " 不是pdf文件");  
    		return;
    	}
    	
    	int dot = pdfPath.lastIndexOf('.'); 
    	String imgName = "";
    	if ((dot >-1) && (dot < (pdfPath.length()))) { 
    		imgName = pdfPath.substring(0, dot) + ".jpg";
            File file = new File(imgName);  
            if (file.exists()){
            	return;
            }
    	}
    	else{
            System.out.println("pdf截图失败:" + pdfPath + " 没有后缀名");  
    		return;
    	}
    	
        Document document = new Document();  
        document.setFile(pdfPath);
        
        float scale = 2.5f;//缩放比例
        float rotation = 0f;//旋转  

        for (int i = 0; i < document.getNumberOfPages(); i++) {
        	if (i>0)
        		break;
        	  
        	System.out.println("pdf截图Start:" + pdfPath);
            try {
            	Page page = document.getPageTree().getPage(i);
            	page.init();
                PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, rotation, scale);
                int pageWidth = (int) sz.getWidth();
                int pageHeight = (int) sz.getHeight();
                BufferedImage image = new BufferedImage(pageWidth,
                        pageHeight,
                        BufferedImage.TYPE_INT_RGB);
                Graphics g = image.createGraphics();
                page.paint(g, GraphicsRenderingHints.PRINT,
                        Page.BOUNDARY_CROPBOX, rotation, scale);
                g.dispose();
            	
                // capture the page image to file
                System.out.println("pdf截图:" + imgName);  
                File file = new File(imgName);  
                ImageIO.write(image, "jpg", file);
                
	            /*BufferedImage image = (BufferedImage)  
	            document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
	            RenderedImage rendImage = image;
            	int dot = pdfPath.lastIndexOf('.'); 
				if ((dot >-1) && (dot < (pdfPath.length()))) { 
					String imgName = pdfPath.substring(0, dot) + ".jpg";
	                System.out.println("pdf截图:" + imgName);  
	                File file = new File(imgName);  
	                ImageIO.write(rendImage, "jpg", file);
	            }*/
	            image.flush();
	            image = null;
            } catch (Exception e) {
                System.out.println(e.getMessage());
            	System.out.println("icepdf截图Error");
            }  
        }
        
        document.dispose();  
        document = null;
        
        pdf = null;
    	
    	System.gc(); 
    }  
    
    public static void main(String[] args) {  
        String filePath = "C:/Users/Administrator/Desktop/30.pdf";  
        pdf2Pic(filePath);  
    }  
}  

如果还是不行,就只能自己增加运行程序的内存了:

java -Xms128m -Xmx2048m -jar TEST.jar

你可能感兴趣的:(Java)