Handling a splitting table in iTextSharp

The PdfPTable class has a property called TableEvent that can be used to receive (as you would expect), events related to a table. If you’re used to .Net, however, you’ll find that it isn’t a true .Net event that you can += or AddHandler and event onto. Instead, like most (or probably all) of iTextSharp’s events you need to implement a specific interface or possibly subclass a specific class and then pass your specific implementation to that property.

Back to PdfPTable, if you want to receive notifications about table events you want to implement IPdfPTableEvent. One example for why you would want to do this would be to draw a border around a table. When you implement this interface you need a single method called TableLayout that will receive a copy of the table containing just the rows that need to be written out for the current page. If your table splits two pages this method will be called twice.

Another slightly more interesting interface is the IPdfPTableEventSplit which itself implements IPdfPTableEvent. The interesting part is the extra method SplitTable which receives a copy of the entire table, not just the current rows being written. This allows you to write out things like Showing rows XXX of YYY. You can also do other things like disabling headers on the second page or draw extra text.