Generating large pdf without having content in main memory

和自甴很熟 提交于 2019-11-29 16:39:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!