iText: Can I adjust the height of a table cell during rendering?
<p>I am generating a PdfPTable and adding it to a Chapter, and then adding the Chapter to my Document.  My table has several cells where RowSpan > 1, and sometimes those spans cross page boundaries, and when that happens I want the spanned cell to appear again.  In other words, if my table looks like this:</p>

<pre><code>+------------------+------------------+
| Cell 1 RowSpan=1 | Cell 2 RowSpan=3 |
+------------------+------------------+
| Cell 3 RowSpan=1 |                  |
+------------------+------------------+
| Cell 4 RowSpan=1 |                  |
+------------------+------------------+
</code></pre>

<p>but when printed, there's a page break between rows 2 and 3, I want the table to look like this:</p>

<pre><code>+--------+--------+
| Cell 1 | Cell 2 |
+--------+        |
| Cell 3 |        |
+--------+--------+
Page Break
+--------+--------+
| Cell 4 | Cell 2 |
+--------+--------+
</code></pre>

<p>This is working thanks to a trick I learned here, I have a class which implements IPdfPCellEvent which I instantiate and attach to cells that have RowSpan > 1:</p>

<pre><code>public class CellEvent : IPdfPCellEvent
{
    Phrase m_phrase;
    bool m_first = true;

    public CellEvent(Phrase phrase)
    {
        m_phrase = phrase;
    }

    void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle r, PdfContentByte[] canvases)
    {
        if (m_first)
            m_first = false;
        else
        {
            ColumnText ct = cell.Column;
            ct.Canvases = canvases;
            m_phrase.Leading = m_phrase.Font.Size;
            ct.AddElement(m_phrase);
            ct.Go();
        }
    }
}
</code></pre>

<p>If the CellLayout() gets called more than once, the subsequent calls are happening because the RowSpan has spilled onto a new page (the exact situation I described above) and so I manually re-draw the cell text.  This works great.</p>

<p>Except.</p>

<p>If my cell 2 is taller than my cell 1:</p>

<pre><code>+------------------+-------------------------+
| Cell 1 RowSpan=1 | Cell 2 Line 1 RowSpan=2 |
|                  | Cell 2 Line 2           |
+------------------+-------------------------+
| Cell 3 RowSpan=1 |                         |
+------------------+-------------------------+
</code></pre>

<p>And the table breaks between those two rows, what I see is this:</p>

<pre><code>+--------+---------------+
| Cell 1 | Cell 2 Line 1 |
|        | Cell 2 Line 2 |
+--------+---------------+
Page Break
+--------+---------------+
| Cell 3 | Cell 2 Line 1 |
+--------+---------------+
</code></pre>

<p>Because, of course, the table has no idea that I want to draw two lines worth of text in row #2.</p>

<p>I can set row #2 to always be tall enough for two lines, but then if the table happens to fall in such a way that there isn't a page break between rows #1 and #2, it generates unnecessary white space in row #2:</p>

<pre><code>+--------+---------------+
| Cell 1 | Cell 2 Line 1 |
|        | Cell 2 Line 2 |
+--------|               |
| Cell 3 |               |
|        |               | &lt;- un-necessary space
+--------+---------------+
</code></pre>

<p>I tried adding a class that implements IPdfPTableEvent and attaching it to my document, but it appears that TableLayout() is called after all the layout has been performed, so I don't have a chance to sneak in and adjust row heights.</p>

<p>I think I can use PdfPTable.KeepRowsTogether() to prevent page breaks in the middle of row spans, but that's not the preferred format.</p>

<p>Is there a way to "get involved" with the layout of the table, as it's being rendered into the PDF document, so that I can adjust the height of the row?</p>

<p>p.s. I just created a subclass of PdfPTable where I override GetRowHeight(idx) and return the row height I want.  It has no effect on the resulting PDF.</p>