【问题标题】:itextSharp null value showing &nbspitextSharp 空值显示 &nbsp
【发布时间】:2023-03-05 21:22:01
【问题描述】:

我正在使用 iTextSharp 从 gridview 创建一个 pdf。当表中有空值时,pdf 会在其位置放置一个 &nbsp。如何删除它并显示空值?我知道 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中的值为空白时,我在单元格字段中得到'&nbsp'。

标签: asp.net vb.net pdf gridview itextsharp


【解决方案1】:

我在 pdf 中收到的 &nbsp 值来自计算字段。当计算没有值时,它不会在 gridview 单元格中放置任何内容,因此它会将空格值放入 pdf 中。我回到计算中,如果没有确定一个值,我就为这个值放置一个空格。比如——

        If dtRaw.Rows(0).Item("ParameterName").ToString() <> "NOY" Then
            drCalcRow("NAAQGreater") = iNAAQS
            drCalcRow("NAAQGreater80") = iNAAQS80
        Else
            drCalcRow("NAAQGreater") = " "
            drCalcRow("NAAQGreater80") = " "
        End If

它可以工作,但是它给代码增加了很多。如果有一次可以检查它在编写 pdf 时何时循环遍历行,那就太好了。我尝试了几件事,但没有任何效果,甚至没有产生影响。

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    相关资源
    最近更新 更多