Reputation: 3231
I have face several problems when using ITextSharp table.
1) How to auto fit table column width to maximum?
2) How to push the entire table to the right (right-align table not right-align table content)
below are what i am trying to achieve where | indicates starts and end of page. As we can see, column 1 (invoice A, B, CDE) are of different width, same to column 3. I would like itextsharp to auto fit the width. Perhaps there is a function in itextsharp that can achieve this?
| Invoice A : Be |
| B : Cdefg |
| CDE : abc |
Upvotes: 1
Views: 4828
Reputation: 77606
Regarding part 1 of your question: auto-sizing of columns based on their content
Please take a look at The Best iText Questions on StackOverflow. It has different examples on how to tune tables, especially in the chapter named "Tables".
It is important to understand that PDF is a format where you decide what content goes where. In other words: you define the width of the columns. The width of the columns is not defined by their content as is the case in HTML.
If you want the tables to "auto-size", why don't you create your tables as HTML and then use XML worker to convert the HTML to PDF. In that case, iText's XML Worker will look at the content and adjust the column width accordingly. For an example, take a look at my answer to the following question: How to get particular html table contents to write it in pdf using itext
Incidentally, that example is written in Java, but if you need a C# example, you'll find a C# example here: itextsharp html to pdf
Regarding part 2 of your question: setting the alignment of a table
There is a method for that. In Java, it's setHorizontalAlignment(). You can find the C# counter-part here: Align itextsharp table
In other words, you need:
table.HorizontalAlignment = Element.ALIGN_RIGHT;
Upvotes: 1