為什麼使用icepdf轉換圖片使用Action和mian方法測的時候沒有出現內存溢出而放在項目裡面調用的時候就出現內存溢出
代碼:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
/**
*/
public class PDFchangToImageUtil {
public static final String FILETYPE_JPG = "jpg";
public static final String SUFF_IMAGE = "." + FILETYPE_JPG;
public static void main(String[] args) {
String filePath = "E:\test\pdf\";
File desFile = new File(filePath);
if (desFile.exists()) {
File[] pdfFs = desFile.listFiles();
for (File file : pdfFs) {
String fileName = file.getName();
System.out.println("要切割的文件"+fileName);
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
if("PDF".equals(prefix)){
// 加密文件帶路徑 輸出路徑 文件名輸出 key
try {
tranfer(filePath + "\" + fileName, filePath, 2);
file=null;
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(fileName+"切割完成!!!");
}
System.gc();
System.out.println("結束!!!");
}
}
/**
縮略圖顯示倍數,1表示不縮放,0.3則縮小到30%
*/
public static void tranfer(String filepath, String imagepath, float zoom)
throws PDFException, PDFSecurityException, IOException {
// ICEpdf document class
Document document = null;
try {
float rotation = 0f;
document = new Document();
document.setFile(filepath);
Integer maxPages = document.getPageTree().getNumberOfPages();
for (int i = 0; i < maxPages; i++) {
BufferedImage img = (BufferedImage) document.getPageImage(i,
GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,
rotation, zoom);
Iterator iter = null;
ImageWriter writer = null;
// 文件輸出位置:
FileOutputStream out = null;
ImageOutputStream outImage = null;
try {
File outFile = new File(imagepath + "\\" + (i + 1) + ".jpg");
iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);
writer = (ImageWriter) iter.next();
out = new FileOutputStream(outFile);
outImage = ImageIO.createImageOutputStream(out);
writer.setOutput(outImage);
writer.write(new IIOImage(img, null, null));
System.gc();
} catch (Exception e) {
e.printStackTrace();
} finally {
outImage.flush();
outImage.close();
out.close();
System.gc();
}
}
} catch (Exception e) {
e.printStackTrace();
}
document.dispose();
System.gc();
}
}
Java中使用icepdf輕松把pdf轉換為圖片
----------------------同志你好,我是CSDN問答機器人小N,奉組織之命為你提供參考答案,編程尚未成功,同志仍需努力!