Is there any way to give the columns an automatic width?

Asked

Viewed 123 times

1

I’m assembling a report via Itextsharp and have read about how to assign specific widths to Pdfptable columns, however I would like this width to be automatically assigned according to the content that the columns have.

Any idea?

1 answer

1

Considering that the report is constructed from an array containing the table content, and an array containing its header, then just use this method to get the vector with the proper column sizes.

Public Function getColsize(ByVal Dados(,) As String, ByVal Header As String(), ByVal FtCalculo As Font)
    Dim L As Integer
    Dim C As Integer
    Dim nLin As Integer
    Dim nCol As Integer
    Dim Colsize() As Integer
    Dim chk As Chunk

    nCol = Dados.GetUpperBound(1)
    nLin = Dados.GetUpperBound(0)

    ReDim Colsize(nCol)

    Dim B As BaseFont = FtCalculo.GetCalculatedBaseFont(False)

    For C = 0 To nCol
        'chk = New Chunk(Header(C).ToUpper, Ft2)
        'Colsize(C) = Math.Ceiling(chk.GetWidthPoint)
        Colsize(C) = Math.Ceiling(B.GetWidthPoint(Header(C).ToUpper, FtCalculo.Size)) + 2
    Next C

    For C = 0 To nCol
        For L = 0 To nLin
            chk = New Chunk(Dados(L, C), FtCalculo)
            If Colsize(C) < (Math.Ceiling(chk.GetWidthPoint) + 2) Then
                Colsize(C) = (Math.Ceiling(chk.GetWidthPoint) + 2)
            End If
        Next L
    Next C

    Return Colsize

End Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.