vb.netitextpdfptable

itextsharp underlined text in cell falls outside cell


I have been able to create a PDF using iTextSharp in my VB .net code that looks almost exactly as I want it to. However, I am using a table with one column and multiple rows to display long text strings put together by using chunks and phrases. Some of the chunks contain text that is underlined. When there is underlined text in a phrase, the entire cell needs to be highlighted gray, and I am using the BackgroundColor property of PdfPCell for this.

The problem I am having is that the underline line falls outside of the cell boundaries (outside of the highlighting). I have tried many things to fix this such as setting a fixed cell height and then setting cell.VerticalAlignment to Element.ALIGN_TOP; using SetLeading with various values including (0, 0) which only made the problem worse; setting cell.Ascender to True; and changing padding values. I may have tried other things too, but for some reason, no matter what I try, the line for the underline text falls outside the highlighting. And, the highlighting goes right up to the bottom of the text in the cell above (which is why I tried playing with the SetLeading values.)

The image shows page 2 of my resulting PDF. PDF with table

Below is and example of the sections of code that are implementing this - "outString1" and "outString2" are the output text strings that get appended to a single line. There is one Boolean value to determine if a chunk of text needs to be underlined, and one to determine if cell highlighting is needed - there are some cases where the cell might be highlighted but the text is not underlined. Any suggestions for how I can fix this?

Dim pdfTable As PdfPTable = New PdfPTable(1)
pdfTable.WidthPercentage = 100

    'the next section is within a loop to create and load each cell
    Dim P As New Phrase()
    'Slisted is a Boolean value to determine need for underlining
    If Slisted Then
        P.Add(New Chunk(outString1, myULfont))
    Else
        P.Add(New Chunk(outString1, myfont))
    End If
    P.Add(New Chunk(outString2, myfont))
    Dim cell As PdfPCell = New PdfPCell(P)
    cell.Border = 0
    cell.Padding = 0
    'hilite is a Boolean value to determine whether 
    If hilite Then
        cell.BackgroundColor = BaseColor.LIGHT_GRAY
    End If
    pdfTable.AddCell(cell)

'out of loop, load table into document
pdfDoc.Add(pdfTable)

Solution

  • An underline, by default, is a certain offset away from the text. Unfortunately, since you've killed off the padding on the table that offset is conflicting with the cell's layout. One option is that you should just change the padding as people have suggested. Another option, however, is to manually set the underline's offset of the Chunk on your own. Below is a sample of that:

    Dim T As New PdfPTable(1)
    
    For I = 1 To 10
    
        ''//Your chunk
        Dim Ch As New Chunk("Hello", DocFont)
    
        ''//Wrap the chunk in a phrase
        Dim p As New Phrase(Ch)
    
        ''//Wrap the phrase in a cell
        Dim cell As PdfPCell = New PdfPCell(p)
    
        ''//Optional borders and padding
        cell.Border = 0
        cell.Padding = 0
    
        ''//This is just an example to do every other cell
        If I Mod 2 = 0 Then
            cell.BackgroundColor = BaseColor.LIGHT_GRAY
    
            ''//Set the thickness and offset from the text
            Ch.SetUnderline(0.1, 0)
        End If
    
        T.AddCell(cell)
    
    Next