Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I am facing a problem when trying Export to PDF from popup window.when click on the button for export to pdf it gives error "The document has no pages.".Please try to help me out.i am using iTextSharp.
I am Helpless.It is very urgent.Thanks in Advance

Code give in below section.
XML
<table>
          <tr>
               <asp:ImageButton ID="ImageButtonPDF" runat="server" AlternateText="View" ToolTip="Click to view PDF report"
                     ImageUrl="~/Graphics/Buttons/printpreview.gif" OnClick="ImageButtonPDF_Click"  />
         </tr>



    <tr>
                                        <td>
                                            <asp:GridView ID="Gridview1" BorderStyle="None" runat="server" AutoGenerateColumns="true"
                                                OnRowDataBound="gvRedemptionFunding_RowDataBound" ShowFooter="true"  Gridlines="none">
                                                <EmptyDataTemplate>
                                                    <%= sbEmptyGridTemplate %>
                                                </EmptyDataTemplate>
                                                <HeaderStyle CssClass="reportHeaderTitle" HorizontalAlign="right" />
                                                <RowStyle CssClass="reportGridItem" HorizontalAlign="right" />
                                                <FooterStyle CssClass="reportGridItem" HorizontalAlign="right" Height="30px" Font-Bold="true" />
                                            </asp:GridView>
                                        </td>
                                    </tr>
</table>




Code Behind

C#
Response.ContentType = "application/pdf";


        Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        HtmlForm frm = new HtmlForm();
        Gridview1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(Gridview1);
        frm.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();

Edit
No need to mention urgent
Posted
Updated 6-Mar-12 18:35pm
v5
Comments
Mahmud Hasan 6-Mar-12 8:14am    
Edited the question for formatting. Are you using any 3rd party pdf conversion library?

 
Share this answer
 
Comments
bubai banerjee 7-Mar-12 0:41am    
Not solved my Problem.
bubai banerjee 7-Mar-12 0:46am    
Thanks for your time and effort
Try this

C#
Gridview1.RenderControl(hw);


instead of creating form and adding Gridview to it.

C#
HtmlForm frm = new HtmlForm();
       Gridview1.Parent.Controls.Add(frm);
       frm.Attributes["runat"] = "server";
       frm.Controls.Add(Gridview1);
       frm.RenderControl(hw);
 
Share this answer
 
Comments
bubai banerjee 7-Mar-12 0:45am    
The document has no pages.Still i am getting error when try to export pdf from
popUp window.As i don't know what the problem is or using "iTextSharp" PDF giving error
bubai banerjee 7-Mar-12 0:46am    
Thanks for your time and effort

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900