我已经能够在我的 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)