we are using iText 5.5.7 with XML Worker and have encountered an issue with long tables where rows that run off the end of the page are split in two over to the next page (see image).
We have tried using page-break-inside:avoid; as suggested in Prevent page break in text block with iText, XMLWorker and iText Cut between pages in PDF from HTML table but to no effect.
we have tried
- wrapping each row in a
<tbody>and applying page break avoid (no effect) - targeting
tr, tdand applying page break (no effect) - wrapping the contents of each
tdin adivand applying page break (itext stops processing rows once it gets to end of page)
We are under the impression page-break-inside:avoid is supported but have yet to see confirmation of this. Is there an example or best practice for creating this effect using XML worker or is the Java api needed to do this level of manipulation?
cheers
Rows currently splitting across pages:


.NET developer, but you should be able to easily translate the following C# code.
Anytime the default XML Worker implementation doesn't meet your needs, you're basically left with an exercise in looking through the source code. First, see if the
XML Workersupports the tag you want in the Tags class. There's a nice implementation for<table>that supports thepage-break-inside:avoidstyle, but it only works at the<table>level, not the row<tr>level. Luckily, it's not that much work to override theEnd()method for Table.If the tag is not supported, you need to roll your own custom tag processor by inheriting from AbstractTagProcessor, but not going there for this answer.
Anyway, on to the code. Instead of blowing away the default implementation by changing the behavior of the
page-break-inside:avoidstyle, we can use a customHTMLattribute and have the best of both worlds:And a simple method to generate some test
HTML:Finally the parsing code:
Full source.
The default implementation is maintained - first
<table>is kept together instead of being split over two pages:And the custom implementation keeps rows together in the second
<table>: