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.
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.
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 });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With