如何使用 iTextsharp 格式化段落字符串以显示 pdf 文档左侧、右侧或中间的内容' vb

how to format paragraph string to show content left, Right or middle of pdf document using iTextsharp ' vb

如何在 Visual Basic 中使用 iTextsharp 格式化段落字符串以显示 pdf 文档左侧、右侧或中间的内容以及文档中的绝对位置。

谢谢

根据 Bruno Lowagie 的建议,我正在使用

Dim table As New PdfPTable(3)
table.setWidthPercentage(100)
table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT))
table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER))
table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT))
document.add(table)

Public Function getCell(ByVal text As String, ByVal alignment As Integer) As PdfPCell
Dim cell As New PdfPCell(New Phrase(text))
cell.setPadding(0)
cell.setHorizontalAlignment(alignment)
cell.setBorder(PdfPCell.NO_BORDER)
Return cell
End Function

我遇到错误 cell.setPadding、cell.setHorizontalAlignment、cell.setBorder 都不是 iTextsharp.Text.pdf.PdfPCell 的成员 table.setWidthPercentage(100) 显示错误参数未指定参数 'page size'

我不是 visual basic 程序员(我最后一次使用 visual basic 是在 1996 年,我说:再也不会了!),但只是通过使用 Google, 我这样改编了你的例子:

Dim table As New PdfPTable(3)
table.WidthPercentage = 100
table.AddCell(GetCell("Text to the left", PdfPCell.ALIGN_LEFT))
table.AddCell(GetCell("Text in the middle", PdfPCell.ALIGN_CENTER))
table.AddCell(GetCell("Text to the right", PdfPCell.ALIGN_RIGHT))
document.Add(table)

Public Function GetCell(ByVal text As String, ByVal alignment As Integer) As PdfPCell
    Dim cell As New PdfPCell(New Phrase(text))
    cell.Padding = 0
    cell.HorizontalAlignment = alignment
    cell.Border = PdfPCell.NO_BORDER
    Return cell
End Function

这是众所周知的:

  • Java中的方法以小写开头; .NET 中的方法以大写开头,所以当人们要求你使用 Java 代码作为伪代码并将 Java 转换为 .NET 时,你需要更改方法,例如 add()addCell() 变成 Add()AddCell().
  • Java 中的成员变量已使用 getter 和 setter 进行更改和查询; .NET 中的变量使用看起来像属性的方法进行更改和查询。这意味着您需要将 cell.setBorder(border);border = cell.getBorder(); 等行更改为 cell.Border = borderborder = cell.Border.

iText 和 iTextSharp 保持同步,这意味着,使用上面解释的两个规则,开发人员可以毫无问题地将 iText 代码转换为 iTextSharp 代码。

当对方法有疑问时,可以像我一样,Google这些方法和属性!您会找到以下示例:

  • Align itextsharp table
  • iTextSharp Table Cell Spacing Possible?
  • ...

如果您想在一个地方找到一大堆示例,请下载 The Best iText Questions on Whosebug