0

I'm using the iTextSharp to export reports to PDF and I have a table with all content that I need.

I want to give columns width based on the most wide text of each column, but the GetWidtPoint() method returns text width that I'm not expecting and the column content jumps to the next line.

Below, there is an example about the calculus of the header text width. I put all my table header inside an array first.

Dim ColSize(10) as single 
Dim Ck as Chunk

For I = 0 to Colsize.getUpperBound(0)
        Ck = new Chunk(Data(I),myFont)
        Ck.SetCharacterSpacing(1)
        Colsize(I) = Ck.GetWidthPoint
Next I

Finally, the table declaration...

Dim T as new PdfPtable(11)
T.HorizontalAligment = 0
T.WidthPercentage = 100
T.SpacingBefore = 3
T.setWidths(Colsize)

An example about issue:

Through the calculus method that I used, the GetWidthPoint() returns 33 points to "Produto" word, but if i set the column width to 33f so I cant show this text only in the first line. To Show the entire word in the same line, I need to set the column width to 55f.

When i print the text in the cell...

Dim Cell as new PdfpCell(new Phrase(new Chunk(Data(I,X),Font)))
T.addCell(cell)

Any ideas?

1 Answer 1

3

Don't forget the cell margins, they are 2 by default, 4 in total. You are not posting the way you are creating the cell but I'm guessing that it's introducing even more margins.

3
  • then... what i must do about this? What i've need to do? Commented Feb 24, 2015 at 13:51
  • You are also setting the relative widths of the column instead of absolute widths, call PdfPTable.SetTotalWidth() instead. Commented Feb 24, 2015 at 14:07
  • i've changed Pdftable.SetWidths by the SetTotalWidth expression... passing the ColSize array in the parameter. But the columns width are wrong yet. Commented Feb 24, 2015 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.