Click here to Skip to main content
16,016,290 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Submitbtn_Click(object sender, EventArgs e)
 {
     {                    Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);

                StringWriter sw = new StringWriter();

                HtmlTextWriter w = new HtmlTextWriter(sw);
                print.RenderControl(w)

                string htmWrite = sw.GetStringBuilder().ToString();
                htmWrite = Regex.Replace(htmWrite, "", "");
                htmWrite = htmWrite.Replace("\r\n", "");
                StringReader reader = new StringReader(htmWrite);

                Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                string pdfFilePath = Server.MapPath(".") + "/PDFFiles";

                HTMLWorker htmlparser = new HTMLWorker(doc);
                PdfWriter.GetInstance(doc, Response.OutputStream);

                doc.Open();
                try
                {

                    htmlparser.Parse(reader);
                    doc.Close();
                    Response.Write(doc);
                    Response.End();
                }
                catch (Exception )
                { }
                finally
                {
                   doc.Close();
                }



        public override void VerifyRenderingInServerForm(Control control)
        {
             /* Verifies that the control is rendered */
        }
}
error shown is
RegisterForEventValidation can only be called during Render();
Posted

1 solution

Try setting EnableEventValidation="false" property to @Page directive of aspx page.

Refer RegisterForEventValidation can only be called during Render();[^]
 
Share this answer
 
Comments
Ashwini Thakare 6-Jun-14 2:08am    
pdf file got created but the page was blank. my data was not shown in it
Debug and see if you getting anything in htmWrite.
Ashwini Thakare 6-Jun-14 13:56pm    
error shown is:
Error 1 Object reference not set to an instance of an object.

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