0

I have used itextsharp dll to generate the PDF. But am stuck up all the time it says like Asp control must be placed inside a form tag with runat=server.

My master page has form tag with runat=server but sill it gives me the exception at pnlMain.RenderControl telling server controls has to be inside of form tag. Any idea on the same? Thanks in advance

My Currentview.aspx page is like this

<asp:panel runat="server" id="pnlMain">
    <div id="dvEditpageheader1"/>
    <p>...</P>
    div id="dvEditpageheader2"/>
    <p>...</P>
    div id="dvEditpageheader3"/>
    <p>...</P>
<asp:Button runat="server" ID="btnPDF" Text="Save to PDF" OnClick="btnPDF_Click" />
</asp:panel>

and my C# code under button click event is

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
pnlMain.RenderControl(htmlTextWriter);

StringReader stringReader = new StringReader(stringWriter.ToString());
Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter.GetInstance(Doc, Response.OutputStream);

Doc.Open();
htmlparser.Parse(stringReader);
Doc.Close();
Response.Write(Doc);
Response.End()
Rakesh
  • 87
  • 1
  • 3
  • 9
  • Possible duplicate of [How to convert HTML to PDF using iTextSharp](http://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp) (Please stop posting questions about `HTMLWorker`. We've been informing developers *for several years now* that `HTMLWorker` is no longer supported!) – Bruno Lowagie Feb 16 '16 at 07:28
  • ... and that `XMLWorker` should be used instead. – Amedee Van Gasse Feb 16 '16 at 07:54
  • Besides using `XMLWorker` instead of `HTMLWorker`, the bigger question is why try and add another `form` tag when there's already one in your `master page`? The `master page` should have a [ContentPlaceHolder](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.contentplaceholder.aspx) or similar to add any server or user controls. Until you get that part straightened out, **nothing** will work. – kuujinbo Feb 17 '16 at 02:58
  • @kuujinbo My master page has the content place holder and form tag also. But when I run the application why it asking at my currentview.aspx page throwing the exception as textbox control has to be inside of the form tag. – Rakesh Feb 17 '16 at 04:37
  • uuuh...yes I uderstood that perfectly from your original question. I don't think you understood my comment, though. [**See here**](http://www.w3schools.com/aspnet/aspnet_masterpages.asp). – kuujinbo Feb 17 '16 at 04:53
  • @kuujinbo I have modified the question, Am sorry, may be I was not clear in explaining my problem. I have modified the question, Could you please have a look. Thanks – Rakesh Feb 17 '16 at 09:20
  • I can't see your master page. **(1)** Does is have a `ContentPlaceHolder`? And **(2)** does `Currentview.aspx page` inherit from that master page, **and also** us the ` – kuujinbo Feb 18 '16 at 00:51

0 Answers0