Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PdfPTable calculate column-width dynamically

Tags:

java

itext

I want to create a PdfPTable with columns containing Strings of different lengths. I learned that the width of each cell/column are the same for each column (default) or one can set the fraction of space every column shall occupy.

However, I want the columns to be as wide as required, but not any wider, depending on the data inserted. Assume that the table fits easily on the page (no breaks!). Of course, I can manually step through all my data and calculate the maximum-String-length of each column, setting the properties of the table accordingly, but I wondered whether such a common option is already provided by itext itself.

like image 279
mlwida Avatar asked Oct 07 '11 12:10

mlwida


People also ask

How do you set column width dynamically in SSRS?

You need to assign a static value to the column width. But the width will keep the same as in design surface, height might be increase based on its content. If you want to change column width dynamically, you could try the workaround in this article: SSRS Column Width Auto Size.

How to set column width in PdfPTable iTextsharp in vb net?

PdfPTable table = new PdfPTable(new float[] { 1, 3 }); If the width of table is 288, the width of the first column will now be 72 and the width of the second column will be 216. PdfPTable table = new PdfPTable(new float[] { 25, 75 });


1 Answers

iText is not HTML, so it doesn't have options to automatically adjust width, instead is page oriented and so you've to decide your widths, either as percentage or absolute values.

I'm afraid that to do what you ask, you've to implement your own extension by measuring the widths, as you said. See how to calculate the string with with iText.

like image 184
stivlo Avatar answered Sep 22 '22 03:09

stivlo