iTextSharp 删除列

iTextSharp Delete column

我正在使用 iTextSharp 将 gridview 导出为 PDF,但是我有一列我不想包含,因为它包含值的超链接。在我程序的 gridview 中,我将显示的每一行的超链接替换为文本 'Details'。 PDF 不需要此列。我试过了 -

    grdResults.Columns.RemoveAt(11)
    grdResults.DataBind()

    grdResults.Columns(11).Visible = False

之前

    Dim pdfTable As New PdfPTable(grdResults.HeaderRow.Cells.Count)

这是我正在使用的代码

   Protected Sub grdResults_RowDataBound(ByVal sender As Object, ByVal e   As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdResults.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim newHyperLink As New HyperLink()
            newHyperLink.Text = "Details"
            newHyperLink.NavigateUrl = "Details.aspx" & Server.HtmlDecode(e.Row.Cells(11).Text)
            e.Row.Cells(11).Controls.Add(newHyperLink)
        End If
    End Sub

 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()

更新 - 感谢您提供代码建议 - 使用下面发布的按索引排除我能够删除超链接,但是该列仍然存在并且单元格值移动了一个单元格。第二行的开头应该是第一行的最后一个值,依此类推。我将代码更改为

            Idx += 1

            If Idx = 11 Then
                tableCell.Text = "Details"
                'Continue For
            End If

它现在将单词 "Details" 放在每一行的列中,但是与 gridview 及其超链接不同,pdf 中的这一列只占用 space。

您应该能够将它排除在您的 For Each 循环中。 Continue For 将跳过当前 For 代码的其余部分,并转到集合中的下一个项目。

For Each tableCell As TableCell In gridViewRow.Cells

    ''//Exclude based on ID
    If tableCell.ID = "Your ID" Then
        Continue For
    End If

    ''//Exclude based on header ID
    If tableCell.AssociatedHeaderCellID.Contains("Your Header ID") Then
        Continue For
    End If

    ''//Exclude based on visibility
    If Not tableCell.Visible Then
        Continue For
    End If
Next

如果这不起作用,您可以根据索引排除:

For Each gridViewRow As GridViewRow In grdResults.Rows
    ''//Reset the counter on each inner for
    Dim Idx = -1
    For Each tableCell As TableCell In gridViewRow.Cells
        //Increment our counter
        Idx += 1

        ''//Exclude based on index
        If Idx = 11 Then
            Continue For
        End If
    Next

Next