itextsharp - iText: Can I adjust the height of a table cell during rendering? -
i generating pdfptable , adding chapter, , adding chapter document. table has several cells rowspan > 1, , spans cross page boundaries, , when happens want spanned cell appear again. in other words, if table looks this:
+------------------+------------------+ | cell 1 rowspan=1 | cell 2 rowspan=3 | +------------------+------------------+ | cell 3 rowspan=1 | | +------------------+------------------+ | cell 4 rowspan=1 | | +------------------+------------------+ but when printed, there's page break between rows 2 , 3, want table this:
+--------+--------+ | cell 1 | cell 2 | +--------+ | | cell 3 | | +--------+--------+ page break +--------+--------+ | cell 4 | cell 2 | +--------+--------+ this working trick learned here, have class implements ipdfpcellevent instantiate , attach cells have rowspan > 1:
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(); } } } if celllayout() gets called more once, subsequent calls happening because rowspan has spilled onto new page (the exact situation described above) , manually re-draw cell text. works great.
except.
if cell 2 taller cell 1:
+------------------+-------------------------+ | cell 1 rowspan=1 | cell 2 line 1 rowspan=2 | | | cell 2 line 2 | +------------------+-------------------------+ | cell 3 rowspan=1 | | +------------------+-------------------------+ and table breaks between 2 rows, see this:
+--------+---------------+ | cell 1 | cell 2 line 1 | | | cell 2 line 2 | +--------+---------------+ page break +--------+---------------+ | cell 3 | cell 2 line 1 | +--------+---------------+ because, of course, table has no idea want draw 2 lines worth of text in row #2.
i can set row #2 tall enough 2 lines, if table happens fall in such way there isn't page break between rows #1 , #2, generates unnecessary white space in row #2:
+--------+---------------+ | cell 1 | cell 2 line 1 | | | cell 2 line 2 | +--------| | | cell 3 | | | | | <- un-necessary space +--------+---------------+ i tried adding class implements ipdfptableevent , attaching document, appears tablelayout() called after layout has been performed, don't have chance sneak in , adjust row heights.
i think can use pdfptable.keeprowstogether() prevent page breaks in middle of row spans, that's not preferred format.
is there way "get involved" layout of table, it's being rendered pdf document, can adjust height of row?
p.s. created subclass of pdfptable override getrowheight(idx) , return row height want. has no effect on resulting pdf.
the solution came with, non-iterative (see @chrishaas' idea in comments) took bit more code. code kind of mess (reflecting iterations took figure out solution) i'm not going upload all, basic gist is:
public class tablerowinfo { public int height; } public class tableevent : ipdfptableeventaftersplit { dictionary<pdfprow, tablerowinfo> m_rows = null; public bool loadspans(pdfptable table) { // fill in m_rows, mapping pdfprow information height // of cells in row, keeping row-spanning in mind } void ipdfptableeventaftersplit.aftersplittable(pdfptable table, pdfprow rowtostartnextpage, int startidx) { tablerowinfo rowinfo = m_rows[rowtostartnextpage]; if (rowinfo.height > rowtostartnextpage.maxheights) { pdfpcell[] cells = rowtostartnextpage.getcells(); foreach (pdfpcell cell in cells) { if (cell != null) cell.minimumheight = rowinfo.height; } rowtostartnextpage.maxheights = rowinfo.height; } } } pdfptable table = new pdfptable(...); // fill in rows of table here table.tableevent = new tableevent(); table.tableevent.loadspans(table);
Comments
Post a Comment