Apache FOP - center table cell vertically

Apr 6, 2016 1 min Mike Kowalski

Nowadays we can argue if using Apache FOP is still the best solution for generating PDF documents. One of it’s biggest competitors is probably JasperReports Library which supports multiple file formats and does not require creating XML templates manually from scratch (thanks to Jaspersoft Studio).

However, sometimes you just have to face the FOP’s XML monster :)

The FOP’s way of styling elements (text blocks, tables etc.) is almost identical to the CSS. But what if you want table cell content centered vertically? Probably you will try with something like (CSS way):

<fo:table-cell vertical-align="middle">
     <fo:block>Hello FOP</fo:block>
</fo:table-cell>

Unfortunately it has no effect at all. To center table cell content vertically we need to set display-align attribute:

<fo:table-cell display-align="center">
     <fo:block>Hello FOP</fo:block>
</fo:table-cell>

This method is also applicable for top/bottom aligning - check this Stack Overflow question for details.

Mike Kowalski

Software engineer believing in craftsmanship and the power of fresh espresso. Writing in & about Java, distributed systems, and beyond. Mikes his own opinions and bytes.