Is it possible to flush output with iText RTF (Jav

2019-05-11 06:03发布

问题:

I'm using iText to create an RTF document. It'll have a few hundred pages when completed. However, I keep getting an outofmemory error, when it's finished adding all the various paragraphs and tables to the document, and it's trying to actually create the RTF file (with document.close();)

I've increased the Runtime memory with -Xmx350m, but it's not feasible to increase it anymore as the users computer won't have that much memory.

Is there a way to gradually write to the RTF document, rather than in a huge block at the end?

回答1:

I found you can set it to explicitly cache on disk rather than memory using:

Document document = new Document();
RtfWriter2 writer2 = RtfWriter2.getInstance(document, new FileOutputStream("document.rtf"));
writer2.getDocumentSettings().setDataCacheStyle(RtfDataCache.CACHE_DISK);
document.open();

This makes it slower to generate but at least it creates the file without error. However, I'd still prefer a method which gradually creates the file if anyone knows one.



标签: java itext