0

我已经能够在我的 VB .net 代码中使用 iTextSharp 创建一个 PDF,看起来几乎完全符合我的要求。但是,我正在使用具有一列和多行的表格来显示通过使用块和短语组合在一起的长文本字符串。一些块包含带下划线的文本。当短语中有下划线文本时,需要将整个单元格突出显示为灰色,为此我使用 PdfPCell 的 BackgroundColor 属性。

我遇到的问题是下划线位于单元格边界之外(突出显示之外)。我尝试了很多方法来解决这个问题,例如设置固定的单元格高度,然后将 cell.VerticalAlignment 设置为 Element.ALIGN_TOP; 使用具有各种值的 SetLeading,包括 (0, 0),这只会使问题变得更糟;将 cell.Ascender 设置为 True;并更改填充值。我可能也尝试过其他的东西,但出于某种原因,无论我尝试什么,下划线文本的行都超出了突出显示的范围。而且,突出显示一直到上方单元格中文本的底部(这就是我尝试使用 SetLeading 值的原因。)

该图像显示了我生成的 PDF 的第 2 页。 带表格的 PDF

下面是实现此功能的代码部分的示例 - “outString1”和“outString2”是附加到单行的输出文本字符串。有一个布尔值来确定是否需要对一大块文本加下划线,另一个确定是否需要突出显示单元格 - 在某些情况下,单元格可能被突出显示但文本没有下划线。关于如何解决这个问题的任何建议?

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)
4

1 回答 1

0

默认情况下,下划线与文本有一定的偏移。不幸的是,由于您已经取消了表格上的填充,因此偏移量与单元格的布局冲突。一种选择是您应该按照人们的建议更改填充。但是,另一种选择是自己手动设置下划线的偏移量Chunk。以下是其中的一个示例:

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
于 2015-10-15T22:11:12.457 回答