I am using iText to generate very large tables in pdf format. What is the best way to generate these tables instead of having the entire content in memory? If I just increa
When using PdfPTable
objects with very many cells you should make use of that class implementing LargeElement
which is documented as
/**
* Interface implemented by Element objects that can potentially consume
* a lot of memory. Objects implementing the LargeElement interface can
* be added to a Document more than once. If you have invoked setComplete(false),
* they will be added partially and the content that was added will be
* removed until you've invoked setComplete(true);
* @since iText 2.0.8
*/
public interface LargeElement extends Element
I.e. you should first use
setComplete(false),
then add some content (e.g. 20 rows) to the table, then add the table to the document, add some more content, add the table to the document again, etc..., and when all is added, use
setComplete(true)
and add the table once more. That way the table data does not remain on the heap but gets bit by bit serialized and written to the writer.
As an aside, there are other iText classes, too, which implement LargeElement
. If you have issues with a huge memory consumption with iText, you should always check the objects you add to your Document
: If they implement LargeElement
, first try to forward them to the Document
piece by piece as explained above.