iTextSharp 添加没有外部边框的子表
iTextSharp add subtable without external border
我正在尝试使用 ItextSharp 从我的网络应用程序创建 pdf。
为了在我的 pdf 的每一页上创建一个 Header 部分,我创建了一个部分 class 并在其中覆盖了 OnEndPage
方法。
一切正常,只有一个例外。
我将 header 设计为具有 2 列的 table,第一列我放了一个徽标,第二列我想在多行上显示一些文本;所以在第二个单元格中,我创建了一个包含 1 列和多行的 subtable,但是这个 subtable 总是显示我无法删除的外部黑色边框。
这是代码:
Public Overrides Sub OnEndPage(writer As PdfWriter, document As Document)
Dim headerIMG As Image = Image.GetInstance(HttpContext.Current.Server.MapPath(logoPath))
Dim pageSize As Rectangle = document.PageSize
Dim headerTbl As New PdfPTable(2)
headerTbl.TotalWidth = 600
headerTbl.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(headerIMG)
cell.Border = 0
cell.PaddingLeft = 10
cell.PaddingBottom = 10
headerTbl.AddCell(cell)
Dim subTable = New PdfPTable(1)
For Each s As String In HeaderText
Dim myCell As New PdfPCell(New Paragraph(s))
myCell.Border = 0
subTable.AddCell(myCell)
Next
subTable.DefaultCell.BorderWidth = 0
headerTbl.AddCell(subTable)
headerTbl.WriteSelectedRows(0, -1, 0, pageSize.GetTop(5), writer.DirectContent)
End Sub
有人可以帮忙吗?
非常感谢
您的代码中有几处错误。例如:您为每个页面创建一个新的 headerIMG
对象。这意味着相同的图像字节将一遍又一遍地添加到 PDF 中。您应该在 OnStartPage
方法 headerIMG
之外声明 。
此外:您定义的 BorderWidth
为 0。如 PDF 规范中所定义,线宽为 0 并不意味着没有线。请阅读 ISO-32000-1,第 8.4.3.2 节 "Line Width":
A line width of 0 shall denote the thinnest line that can be rendered at device resolution: 1 device pixel wide.
如果您不需要边框,请告诉 iText 您不需要边框:
Dim headerIMG As Image = Image.GetInstance(HttpContext.Current.Server.MapPath(logoPath))
Public Overrides Sub OnStartPage(writer As PdfWriter, document As Document)
Dim pageSize As Rectangle = document.PageSize
Dim headerTbl As New PdfPTable(2)
headerTbl.TotalWidth = 600
headerTbl.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(headerIMG)
cell.Border = PdfPCell.NO_BORDER
cell.PaddingLeft = 10
cell.PaddingBottom = 10
headerTbl.AddCell(cell)
Dim subTable = New PdfPTable(1)
For Each s As String In HeaderText
Dim myCell As New PdfPCell(New Paragraph(s))
myCell.Border = PdfPCell.NO_BORDER
subTable.AddCell(myCell)
Next
subTable.DefaultCell.Border = PdfPCell.NO_BORDER
headerTbl.AddCell(subTable)
headerTbl.WriteSelectedRows(0, -1, 0, pageSize.GetTop(5), writer.DirectContent)
End Sub
你看到我改变了什么了吗?
另外,请阅读nelek提供的评论。为什么需要子 table?您可以轻松地为包含图像的单元格定义行跨度。
我正在尝试使用 ItextSharp 从我的网络应用程序创建 pdf。
为了在我的 pdf 的每一页上创建一个 Header 部分,我创建了一个部分 class 并在其中覆盖了 OnEndPage
方法。
一切正常,只有一个例外。
我将 header 设计为具有 2 列的 table,第一列我放了一个徽标,第二列我想在多行上显示一些文本;所以在第二个单元格中,我创建了一个包含 1 列和多行的 subtable,但是这个 subtable 总是显示我无法删除的外部黑色边框。
这是代码:
Public Overrides Sub OnEndPage(writer As PdfWriter, document As Document)
Dim headerIMG As Image = Image.GetInstance(HttpContext.Current.Server.MapPath(logoPath))
Dim pageSize As Rectangle = document.PageSize
Dim headerTbl As New PdfPTable(2)
headerTbl.TotalWidth = 600
headerTbl.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(headerIMG)
cell.Border = 0
cell.PaddingLeft = 10
cell.PaddingBottom = 10
headerTbl.AddCell(cell)
Dim subTable = New PdfPTable(1)
For Each s As String In HeaderText
Dim myCell As New PdfPCell(New Paragraph(s))
myCell.Border = 0
subTable.AddCell(myCell)
Next
subTable.DefaultCell.BorderWidth = 0
headerTbl.AddCell(subTable)
headerTbl.WriteSelectedRows(0, -1, 0, pageSize.GetTop(5), writer.DirectContent)
End Sub
有人可以帮忙吗? 非常感谢
您的代码中有几处错误。例如:您为每个页面创建一个新的 headerIMG
对象。这意味着相同的图像字节将一遍又一遍地添加到 PDF 中。您应该在 OnStartPage
方法 headerIMG
之外声明 。
此外:您定义的 BorderWidth
为 0。如 PDF 规范中所定义,线宽为 0 并不意味着没有线。请阅读 ISO-32000-1,第 8.4.3.2 节 "Line Width":
A line width of 0 shall denote the thinnest line that can be rendered at device resolution: 1 device pixel wide.
如果您不需要边框,请告诉 iText 您不需要边框:
Dim headerIMG As Image = Image.GetInstance(HttpContext.Current.Server.MapPath(logoPath))
Public Overrides Sub OnStartPage(writer As PdfWriter, document As Document)
Dim pageSize As Rectangle = document.PageSize
Dim headerTbl As New PdfPTable(2)
headerTbl.TotalWidth = 600
headerTbl.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(headerIMG)
cell.Border = PdfPCell.NO_BORDER
cell.PaddingLeft = 10
cell.PaddingBottom = 10
headerTbl.AddCell(cell)
Dim subTable = New PdfPTable(1)
For Each s As String In HeaderText
Dim myCell As New PdfPCell(New Paragraph(s))
myCell.Border = PdfPCell.NO_BORDER
subTable.AddCell(myCell)
Next
subTable.DefaultCell.Border = PdfPCell.NO_BORDER
headerTbl.AddCell(subTable)
headerTbl.WriteSelectedRows(0, -1, 0, pageSize.GetTop(5), writer.DirectContent)
End Sub
你看到我改变了什么了吗?
另外,请阅读nelek提供的评论。为什么需要子 table?您可以轻松地为包含图像的单元格定义行跨度。