2017-02-18 80 views
0

我想使用iTextSharp将网格视图导出到pdf报告。 gridView包含一个不在pdf文件中显示的超链接字段。在iTextSharp中缺少超链接文本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> 

我附上一个SqlDataSource到使用SELECT命令来获取填充GridView控件GridView控件。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ArtistManagementSystem %>" SelectCommand="SELECT [Start Date] AS Start_Date, [End Date] AS End_Date, Title AS Agenda_Title, [Start Time] AS Start_Time, [End Time] AS End_Time, ID FROM Agenda WHERE ([Event ID] = @Event_ID) ORDER BY Start_Date, Start_Time"> 
<SelectParameters> 
    <asp:QueryStringParameter Name="Event_ID" QueryStringField="evtID" Type="String" /> 
</SelectParameters> 

然后一个按钮到GridView导出为PDF。

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

这是我看到我的报告: enter image description here

标题栏始终显示为空。

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

在这一行

回答

0

PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text)); 

的链接栏,我不认为你会得到填充tblCell.Text。相反,您需要通过检查tblCell.Controls属性来检查tblCell子控件。通过迭代tblCell.Controls你应该能够在那里找到你的链接控制,可能就像tblCell.Controls[0]

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

HTH