Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to generate pdf in asp.net page the page contain contentplaceholder i googled and tried the below code but its showing error like this.

>>>Extender control 'Search' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().
Parameter name: extenderControl <<<<

in this code im getting error in this line
C#
this.Page.RenderControl(hw);


C#
//code//
Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            this.Page.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();
Posted
Updated 17-Oct-12 19:26pm
v3
Comments
Dee_Bee 18-Oct-12 1:35am    
If you want the file to be opened on the client side, your best bet is to create and HTTP Handler and set the appropriate mime type on your response before streaming it out from your handler.

Code to stream a file out to client.

C#
public void ProcessRequest(HttpContext context)
{
   int newsId = int.Parse(context.Session["newsId"].ToString());
   int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
   string dirPathForTextFiles =  ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
   DataTable UpdatedDateTable = (DataTable)context.Session["theLastUpdatedTextFile"];
   UpdatedDateTable.AcceptChanges();
   context.Session.Add("currentTextFile", UpdatedDateTable);
   List<string> l = new List<string>(UpdatedDateTable.Rows.Count);

   try
   {

      l.Add(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
       context.Response.ContentType = getContentType(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
       using (FileStream fs = new FileStream(l[0], FileMode.Open, FileAccess.Read))
       {
          long chunkSize = fs.Length;
          byte[] buf = new byte[chunkSize];
          int bytesRead = 1;
          bytesRead = fs.Read(buf, 0,(int)chunkSize);
          if (bytesRead > 0) context.Response.OutputStream.Write(buf, 0, buf.Length);
          context.Response.OutputStream.Flush();
      }

  }
  catch (IOException e)
  {
     string message = e.Message;
  }
}
 
Share this answer
 
 
Share this answer
 

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