我正在尝试使用带有AcroForm
的单个模板PDF生成10000页PDF,我在此过程中填充了值。
我的问题是,我正在点击OOME
,应用程序本身消耗4GB。如何降低内存消耗?
我如何创建我的内容:
public PDFBuilderItext5(InputStream templateSource) throws DocumentException, IOException {
templateBytes = readTemplateToBytes(templateSource);
// output = new ByteArrayOutputStream();
output = new BufferedOutputStream(new FileOutputStream("z:\\itext\\buff.pdf"));
document = new Document();
copy = new PdfSmartCopy(document, output);
document.open();
}
这是循环完成的:
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(templateBytes);
PdfStamper stamper = new PdfStamper(reader, tempOut);
try {
AcroFields fields = stamper.getAcroFields();
// fields.setGenerateAppearances(true);
Set<String> fieldNames = fields.getFields().keySet();
FieldValueBuilder fb = new FieldValueBuilder(t);
for (String fn : fieldNames) {
String label = fields.getField(fn);
String value = fb.valueFor(label);
fields.setField(fn, value,value);
}
stamper.setFormFlattening(true);
} finally {
stamper.close();
reader.close();
}
append(tempOut.toByteArray());
private synchronized void append(byte[] pdfContent) throws DocumentException, IOException{
PdfReader reader = new PdfReader(pdfContent);
copy.addDocument(reader);
reader.close();
notFlushedPagesCount++;
if(notFlushedPagesCount>=FLUSH_INTERVAL){
copy.flush();
System.out.println("Flushed copy");
notFlushedPagesCount=0;
}
}
我觉得我没有在某处发布一些资源:
答案 0 :(得分:1)
你永远不会“释放”读者。
例如:
private synchronized void append(byte[] pdfContent)
throws DocumentException, IOException{
PdfReader reader = new PdfReader(pdfContent);
copy.addDocument(reader);
copy.freeReader(reader);
reader.close();
}