【发布时间】:2023-03-05 21:22:01
【问题描述】:
我正在使用 iTextSharp 从 gridview 创建一个 pdf。当表中有空值时,pdf 会在其位置放置一个  。如何删除它并显示空值?我知道 pdf 是在 gridview 已经填充之后创建的,但是有没有办法告诉它 null 应该显示为空白?我的代码如下:
Protected Sub btnPDF_Click(sender As Object, e As System.EventArgs) Handles btnPDF.Click
Dim pdfTable As New PdfPTable(grdResults.HeaderRow.Cells.Count)
For Each headerCell As TableCell In grdResults.HeaderRow.Cells
Dim font As New Font()
font.Color = New BaseColor(grdResults.HeaderStyle.ForeColor)
Dim pdfCell As New PdfPCell(New Phrase(headerCell.Text, font))
pdfCell.BackgroundColor = New BaseColor(grdResults.HeaderStyle.BackColor)
pdfTable.AddCell(pdfCell)
Next
For Each gridViewRow As GridViewRow In grdResults.Rows
For Each tableCell As TableCell In gridViewRow.Cells
Dim font As New Font()
font.Color = New BaseColor(grdResults.RowStyle.ForeColor)
Dim pdfCell As New PdfPCell(New Phrase(tableCell.Text, font))
pdfCell.BackgroundColor = New BaseColor(grdResults.RowStyle.BackColor)
pdfTable.AddCell(pdfCell)
Next
Next
Dim pdfDocument As New Document(iTextSharp.text.PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
pdfDocument.SetPageSize(PageSize.A4.Rotate())
PdfWriter.GetInstance(pdfDocument, Response.OutputStream)
pdfDocument.Open()
pdfDocument.Add(pdfTable)
pdfDocument.Close()
Response.ContentType = "application/pdf"
Response.AppendHeader("content-disposition", "attachment, filename-results.pdf")
Response.Write(PdfDocument)
Response.Flush()
Response.End()
End Sub
【问题讨论】:
-
您是真的看到了类似 HTML 的实体
&nbsp还是只是看到了空格?此外,与您的问题无关,但删除Response.Write(PdfDocument)行,因为它没有按照您的想法执行。该行实际上在pdfDocument上调用ToString(),但该对象没有字符串表示形式,因此您得到的是垃圾数据。它在 99% 的时间里都能正常工作,但如果你继续这样做,有时你会得到一个损坏的 PDF。 -
当gridview中的值为空白时,我在单元格字段中得到' '。
标签: asp.net vb.net pdf gridview itextsharp