2017-02-18 54 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