itextSharp显示null值

时间:2016-01-14 17:48:03

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

我正在使用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

1 个答案:

答案 0 :(得分:0)

我在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时循环遍历行的情况会很好。我尝试过几件事,但没有任何效果,甚至没有影响。

相关问题