您是否只需要在加载/保存操作后关闭 PDDocument 或制作每个新的 PDDocument 对象(例如,在执行合并/拆分/...操作时)?例如,假设我有 3 个从字节数组加载的 PDDocument:PDDocument pdf1 = PDDocument.load(bytearray1);PDDocument pdf2 = PDDocument.load(bytearray2);PDDocument pdf3 = PDDocument.load(bytearray3);假设我然后将这 3 个 pdf 合并到一个 PDDocument 中:PdfMergerUtility merger = new PdfMergerUtility();PDDocument mergedPdf = new PDDocument();mergedDocument.appendDocument(mergedPdf, pdf1);mergedDocument.appendDocument(mergedPdf, pdf2);mergedDocument.appendDocument(mergedPdf, pdf3);我关闭了 3 个 pdf:pdf1.close();pdf2.close();pdf3.close();但是我现在必须关闭mergedPdf还是不需要?mergedPdf.close(); // is this needed?
1 回答
九州编程
TA贡献1785条经验 获得超4个赞
PDDocument完成所有对象后,您也应该关闭它们mergedPdf。这是一个很好的做法,它可以避免内存泄漏。在可能的情况下,使用 try-with-resources。
添加回答
举报
0/150
提交
取消
