When you create a table in iText 7 using the Table and Cell classes, the table cells come with some padding built in by default. As far as I can tell by looking at a generated document, it appears to be about 2 PDF units.
Is there any way I can retrieve this value for use in calculations? Also, is there any way I can change this default, so that I can set my own padding to be used in all cells in all tables, instead of having to set it individually on every cell?
Please take a look at the iText 7: Building Blocks tutorial.
In the Before we start section, we see that every building block is derived from a class named
ElementPropertyContainer. This class is a container of properties.In the case of the
Cellclass, there is a set of properties that define the padding. You can get these properties the generic way (using a method of theAbstractElementclass) like this:But why make it difficult if you can also simply use convenience methods that is available in the
BlockElementclass:As you can see in the tutorial, the
Cellclass is a subclass of theBlockElementclass. TheBlockElementis a subclass of theAbstractElementclass. TheAbstractElementclass is a subclass of theElementPropertyContainerclass.If you want to change the padding (or the margin if you are so inclined), please read chapter 5 of that tutorial. It has an example, named CellMarginPadding:
This is what it looks like:
I'm sorry if it hurts the eyes a bit, but using those colors seemed like the best way to explain the difference between the margin and the padding to me.
Most of the properties are inherited. For instance: if you set the font for a
Div, that font will be the default font for all the elements added to thatDiv. There are some exceptions though. The padding is one of them. This is how the default values for the properties specific to theCellclass were defined:As you can see, there is no padding value for the complete cell; the padding consists of four values that incidentally are identical by default.
If you don't like to define a padding different from the default for each
Cell, just create a subclass ofCelland call itMyCustomCell. Make it custom in the sense that it uses the padding of your choice by overriding thegetDefaultProperty()class.In the tutorial, you'll find an example of a subclass that draws cells with borders that have rounded corners so that we don't have to set declare a renderer every time we want to introduce rounder corners.
I am the original author of that documentation. I hope you find it useful to answer these and other questions about the
Celland other objects in iText 7.