Asp.net iTextSharp PDF中缺少超链接文本

Asp.net iTextSharp PDF中缺少超链接文本,asp.net,itext,Asp.net,Itext,我正在尝试使用iTextSharp将网格视图导出为pdf报告。gridView包含一个pdf文件中未显示的超链接字段 <asp:GridView ID="GridView1" runat="server" CssClass="table table-striped" Style="font-family: sans-serif; font-size: medium" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Cell

我正在尝试使用iTextSharp将网格视图导出为pdf报告。gridView包含一个pdf文件中未显示的超链接字段

<asp:GridView ID="GridView1" runat="server" CssClass="table table-striped" Style="font-family: sans-serif; font-size: medium" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" Width="903px" DataKeyNames="ID" >
<AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:BoundField DataField="Start_Date" HeaderText="Start_Date" SortExpression="Start_Date" />
    <asp:BoundField DataField="End_Date" HeaderText="End_Date" SortExpression="End_Date" />
    <asp:HyperLinkField DataTextField="Agenda_Title" DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/Views/Portal/ViewAgenda.aspx?ID={0}" HeaderText="Title" ItemStyle-Width="150px"></asp:HyperLinkField>
    <asp:BoundField DataField="Start_Time" HeaderText="Start_Time" SortExpression="Start_Time" />
    <asp:BoundField DataField="End_Time" HeaderText="End_Time" SortExpression="End_Time" />
</Columns>
</asp:GridView>
以下是我对报告的看法:

“标题”列始终显示为空

请帮我解决这个问题。谢谢。

在这行:

PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
对于link列,我认为不会填充
tblCell.Text
。相反,您需要通过检查属性来检查
tblCell
子控件。通过迭代
tblCell.Controls
您应该能够在那里找到您的链接控件,可能类似于
tblCell.Controls[0]

只需在调试器中该行的标题列上停止,然后查看
tblCell.Controls
包含的内容。然后将适当的链接控件强制转换为调试器中显示的实际链接控件类型,并访问链接控件的Text属性

<asp:Button ID="exportToPDF" Text="Export To PDF" CssClass="btn btn-default" runat="server" ForeColor="#ffffff" BackColor="#b52e31" OnClick="exportToPDF_Click" />
protected void exportToPDF_Click(object sender, EventArgs e)
    {
        PdfPTable pdfTable = new PdfPTable(GridView1.HeaderRow.Cells.Count);

        foreach (TableCell headerCell in GridView1.HeaderRow.Cells)
        {
            Font font = new Font();
            font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor);

            PdfPCell pdfCell = new PdfPCell(new Phrase(headerCell.Text, font));
            pdfCell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor);
            pdfTable.AddCell(pdfCell);
        }

        foreach(GridViewRow gridviewRow in GridView1.Rows)
        {
            foreach(TableCell tblCell in gridviewRow.Cells)
            {
                Font font = new Font();
                font.Color = new BaseColor(GridView1.RowStyle.ForeColor);

                PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
                pdfCell.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor);
                pdfTable.AddCell(pdfCell);
            }
        }

        Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        PdfWriter.GetInstance(pdfDocument, Response.OutputStream);

        pdfDocument.Open();
        pdfDocument.Add(pdfTable);
        pdfDocument.Close();

        Response.ContentType = "application/pdf";
        Response.AppendHeader("content-disposition", "attachment;filename=Agenda.pdf");
        Response.Write(pdfDocument);
        Response.Flush();
        Response.End();
    }
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));