Export Panel Content to PDF not working

2.4k views Asked by At

I want to export panel content in pdf but i am getting error, in panel i have table and Gridview.

ASPX Page :

         <asp:Panel ID = "pnl_export" runat = "server" >
                <table>
                <tr>
                    <td>
                        <asp:Label ID="rptname" runat="server" Text=""></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbid" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lbexp" runat="server" Text="Report Exported On : "></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbdate" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lfrm" runat="server" Text="From : "></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="fdate" runat="server"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lto" runat="server" Text="To : "></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="tdate" runat="server"></asp:Label>
                    </td>
                </tr>
                </table>
                <asp:GridView ID="GridView1" runat="server" Width="63%" align="Center" PageSize="20"
                    AllowPaging="true" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
   <PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last"
                        NextPageText="Next" PreviousPageText="Prev" Position="TopAndBottom" />

                </asp:GridView>
                </asp:Panel>

Code Behind for pdf Export :

         protected void pdfExport_Click(object sender, EventArgs e)
        {
         using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
               // GridView1.AllowPaging = false;
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=AgentReport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                pnl_export.RenderControl(hw);
                String htmlString = sw.ToString().Substring(0, sw.ToString().Length - 1); 
                StringReader sr = new StringReader(htmlString);
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                pdfDoc.Open();
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

                htmlparser.Parse(sr);
                pdfDoc.Close();

                Response.Write(pdfDoc);
                Response.End();

            }

        }


    }

It's not working giving me error,

   Error : Unable to cast object of type 'iTextSharp.text.html.simpleparser.TableWrapper' to type 'iTextSharp.text.ITextElementArray'. 

HTML Content :

        <div id="pnl_export">

    <table>
        <tr>
            <td>
                <span id="rptname">Agent Report For AgentID : </span>
            </td>
            <td>
                <span id="lbid">RSBOM0400986</span>
            </td>
        </tr>
        <tr>
            <td>
                <span id="lbexp">Report Exported On : </span>
            </td>
            <td>
                <span id="lbdate">10:01:34 AM</span>
            </td>
        </tr>
        <tr>
            <td>
                <span id="lfrm">From : </span>
            </td>
            <td>
                <span id="fdate">25/11/2014</span>
            </td>
            <td>
                <span id="lto">To : </span>
            </td>
            <td>
                <span id="tdate">28/11/2014</span>
            </td>
        </tr>
        <div>
    <table cellspacing="0" rules="all" align="Center" border="1" id="GridView1" style="width:63%;border-collapse:collapse;">
        <tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
            <th scope="col">Sr.No</th><th scope="col">Sales Person</th><th scope="col">Remark</th><th scope="col">Date Visited</th>
        </tr><tr class="Textsmall" align="center" valign="middle" style="color:#333333;background-color:#F7F6F3;border-style:None;">
            <td>1</td><td>Manish Goswami-700145</td><td>fhvhvhffffffftt</td><td>26-Nov-14 5:53:49 PM</td>
        </tr><tr class="Textsmall" align="center" valign="middle" style="color:#284775;background-color:White;border-style:None;">
            <td>2</td><td>Manish Goswami-700145</td><td>remark km from my name of the way to</td><td>26-Nov-14 5:59:23 PM</td>
        </tr>
    </table>
</div>
    </table>

Please help me out to solve this error.Thanks in advance.

1

There are 1 answers

5
Hiral Nayak On BEST ANSWER

try this way

protected void pdfExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=AgentReport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    pnl_export.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}