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

本文介绍使用icepdf将PDF文件转换为图片的过程,包括解决jpeg2000错误的方法及如何避免outofmemory错误,提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1. 出现jpeg2000的错误:


 
  1. ImageIO missing required plug-in to read JPEG 2000 images.

  2. 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下载的:


 
  1. <repositories>

  2. <repository>

  3. <id>bintray-jai-imageio</id>

  4. <name>jai-imageio at bintray</name>

  5. <url>https://dl.bintray.com/jai-imageio/maven/</url>

  6. <snapshots>

  7. <enabled>false</enabled>

  8. </snapshots>

  9. </repository>

  10. </repositories>

 


 
  1. <dependencies>

  2. <dependency>

  3. <groupId>com.github.jai-imageio</groupId>

  4. <artifactId>jai-imageio-core</artifactId>

  5. <version>1.3.1</version>

  6. </dependency>

  7. <dependency>

  8. <groupId>com.github.jai-imageio</groupId>

  9. <artifactId>jai-imageio-jpeg2000</artifactId>

  10. <version>1.3.0</version>

  11. </dependency>

  12. </dependencies>


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


 
  1. package com.sun.pdfImage;

  2.  
  3. import java.awt.Graphics;

  4. import java.awt.image.BufferedImage;

  5. import java.awt.image.RenderedImage;

  6. import java.io.File;

  7. import java.io.IOException;

  8. import javax.imageio.ImageIO;

  9.  
  10. import org.icepdf.core.exceptions.PDFException;

  11. import org.icepdf.core.exceptions.PDFSecurityException;

  12. import org.icepdf.core.pobjects.Document;

  13. import org.icepdf.core.pobjects.PDimension;

  14. import org.icepdf.core.pobjects.Page;

  15. import org.icepdf.core.util.GraphicsRenderingHints;

  16. /*

  17. * pdf 转 图片

  18. */

  19. public class Icepdf {

  20. public static void pdf2Pic(String pdfPath){

  21.  
  22. File pdf = new File(pdfPath);

  23. if (!pdf.exists()){

  24. System.out.println("pdf截图失败:" + pdfPath + " 不是pdf文件");

  25. return;

  26. }

  27.  
  28. int dot = pdfPath.lastIndexOf('.');

  29. String imgName = "";

  30. if ((dot >-1) && (dot < (pdfPath.length()))) {

  31. imgName = pdfPath.substring(0, dot) + ".jpg";

  32. File file = new File(imgName);

  33. if (file.exists()){

  34. return;

  35. }

  36. }

  37. else{

  38. System.out.println("pdf截图失败:" + pdfPath + " 没有后缀名");

  39. return;

  40. }

  41.  
  42. Document document = new Document();

  43. document.setFile(pdfPath);

  44.  
  45. float scale = 2.5f;//缩放比例

  46. float rotation = 0f;//旋转

  47.  
  48. for (int i = 0; i < document.getNumberOfPages(); i++) {

  49. if (i>0)

  50. break;

  51.  
  52. System.out.println("pdf截图Start:" + pdfPath);

  53. try {

  54. Page page = document.getPageTree().getPage(i);

  55. page.init();

  56. PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, rotation, scale);

  57. int pageWidth = (int) sz.getWidth();

  58. int pageHeight = (int) sz.getHeight();

  59. BufferedImage image = new BufferedImage(pageWidth,

  60. pageHeight,

  61. BufferedImage.TYPE_INT_RGB);

  62. Graphics g = image.createGraphics();

  63. page.paint(g, GraphicsRenderingHints.PRINT,

  64. Page.BOUNDARY_CROPBOX, rotation, scale);

  65. g.dispose();

  66.  
  67. // capture the page image to file

  68. System.out.println("pdf截图:" + imgName);

  69. File file = new File(imgName);

  70. ImageIO.write(image, "jpg", file);

  71.  
  72. /*BufferedImage image = (BufferedImage)

  73. document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);

  74. RenderedImage rendImage = image;

  75. int dot = pdfPath.lastIndexOf('.');

  76. if ((dot >-1) && (dot < (pdfPath.length()))) {

  77. String imgName = pdfPath.substring(0, dot) + ".jpg";

  78. System.out.println("pdf截图:" + imgName);

  79. File file = new File(imgName);

  80. ImageIO.write(rendImage, "jpg", file);

  81. }*/

  82. image.flush();

  83. image = null;

  84. } catch (Exception e) {

  85. System.out.println(e.getMessage());

  86. System.out.println("icepdf截图Error");

  87. }

  88. }

  89.  
  90. document.dispose();

  91. document = null;

  92.  
  93. pdf = null;

  94.  
  95. System.gc();

  96. }

  97.  
  98. public static void main(String[] args) {

  99. String filePath = "C:/Users/Administrator/Desktop/30.pdf";

  100. pdf2Pic(filePath);

  101. }

  102. }

 

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

java -Xms128m -Xmx2048m -jar TEST.jar
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值