How to make PdfPTable dynamically calculate column width

I want to create a PdfPTable with columns containing strings of different lengths. I found out that the width of each cell / column is the same for each column (by default), or you can specify the fraction of the space that each column should occupy.

However, I want the columns to be as wide as possible, but not wider, depending on the data inserted. Suppose a table fits easily on a page (no breaks!). Of course, I can manually execute all my data and calculate the maximum row length of each column by setting the table properties accordingly, but I wondered if such a general parameter was already provided by this text itself.

+7
source share
2 answers

iText is not HTML, so it does not have parameters for automatic width adjustment, but is page-oriented, and therefore you must determine your width, both in percent and in absolute values.

I am afraid that in order to do what you ask, you must implement your own extension by measuring the width, as you said. See how to calculate a string using iText .

+3
source

Defines the relative width of columns float [] columnWidths = new float [] {10f, 20f, 30f, 10f}; table.setWidths (columnWidths);

according to this link: enter the link here

-2
source

All Articles