2015-12-15 67 views
0

我下載了最後一個版本的iTextSharp dll。iTextSharp導出asp網絡面板

我用這個出口在PDF這個簡單的標籤Panel在c#中。

<asp:Panel ID="views" runat="server"> 
    <fieldset style="margin-left: 50px"> 
     <legend style="font-weight: bold; color: Red; margin-left: 10px;">Testing<br /> 
      <br /> 
      1. testing tble</legend> 
     <br /> 
     <table> 
      <tr> 
       <td> 
        <label for="Zone"> 
         Zone<br /> 
        </label> 
        <asp:Label ID="Zone" runat="server"></asp:Label></td> 
       <td> 
        <label for="cp"> 
         cp<br /> 
        </label> 
        <asp:Label ID="cp" runat="server"></asp:Label></td> 
       <td> 
        <label for="LinesName"> 
         LinesName<br /> 
        </label> 
        <asp:Label ID="LinesName" runat="server"></asp:Label></td> 
       <td> 
        <label for="LinesCode"> 
         LinesCode<br /> 
        </label> 
        <asp:Label ID="LinesCode" runat="server"></asp:Label></td> 
      </tr> 
      <tr> 
       <td> 
        <label for="cl"> 
         cl<br /> 
        </label> 
        <asp:Label ID="cl" runat="server"></asp:Label></td> 
      </tr> 
     </table> 
    </fieldset> 
</asp:Panel> 

,並使用此代碼隱藏用於填充Panel

using (OdbcCommand command = 
      new OdbcCommand(sql, cn)) 
    { 
     try 
     { 
      command.Parameters.AddWithValue("param1", Decrypt(Request.QueryString["id"].ToString())); 
      command.Connection.Open(); 

      using (OdbcDataAdapter da = 
       new OdbcDataAdapter(command)) 
      { 
       using (OdbcDataReader sdr = command.ExecuteReader()) 
       { 
        while (sdr.Read()) 
        { 
         Zone.Text = sdr["Zone"].ToString(); 
         cp.Text = sdr["cp"].ToString(); 
         LinesName.Text = sdr["LinesName"].ToString(); 
         LinesCode.Text = sdr["LinesCode"].ToString(); 
         cl.Text = sdr["cl"].ToString(); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      command.Connection.Close(); 
     } 

而這種代碼出口PDF

private void PdfFiles1() 
{ 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment;filename=" + Decrypt(Request.QueryString["id"].ToString()) + ".pdf"); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 
    views.RenderControl(hw); 
    StringReader sr = new StringReader(sw.ToString()); 
    Document pdfDoc = new Document(PageSize.A4, 30f, 10f, 10f, 10f); 
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
    pdfDoc.Open(); 
    htmlparser.Parse(sr); 
    pdfDoc.Close(); 
    Response.Write(pdfDoc); 
    Response.End(); 
} 

我沒有錯誤PDF回報但我只有第一排table

<tr> 
    <td> 
     <label for="Zone"> 
      Zone<br /> 
     </label> 
     <asp:Label ID="Zone" runat="server"></asp:Label></td> 
    <td> 
     <label for="cp"> 
      cp<br /> 
     </label> 
     <asp:Label ID="cp" runat="server"></asp:Label></td> 
    <td> 
     <label for="LinesName"> 
      LinesName<br /> 
     </label> 
     <asp:Label ID="LinesName" runat="server"></asp:Label></td> 
    <td> 
     <label for="LinesCode"> 
      LinesCode<br /> 
     </label> 
     <asp:Label ID="LinesCode" runat="server"></asp:Label></td> 
</tr> 

第二行不打印:

<tr> 
    <td> 
     <label for="cl"> 
      cl<br /> 
     </label> 
     <asp:Label ID="cl" runat="server"></asp:Label></td> 
</tr> 

請幫幫我,謝謝你這麼多提前。

+0

此外,刪除'Response.Write(pdfDoc);'行,因爲它沒有做你認爲它的事。 –

回答

0

您的HTML表格不平衡,iText期望表中的每個單元格都被計入。爲了解決這個問題,只是給你的第二個行單元格的正確數量:

<tr> 
    <td colspan="4"> 
     <label for="cl"> 
      cl<br /> 
     </label> 
     <asp:Label ID="cl" runat="server"></asp:Label></td> 
</tr> 

如果你不想跨越他們,你只需要包含空單元格:

<tr> 
    <td> 
     <label for="cl"> 
      cl<br /> 
     </label> 
     <asp:Label ID="cl" runat="server"></asp:Label></td> 
    <td></td> 
    <td></td> 
    <td></td> 
</tr> 

或者:

<tr> 
    <td> 
     <label for="cl"> 
      cl<br /> 
     </label> 
     <asp:Label ID="cl" runat="server"></asp:Label></td> 
    <td colspan="3"></td> 
</tr>