ASP.NET - Exporting Textbox to pdf using iTextSharp

Asked By avula on 28-Oct-10 05:10 AM

i did ,exporting gridview to pdf, it  working well , but i need to export plain textboxes instead of gridview
here is code i changed


Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

//GridView1.AllowPaging = false;

//GridView1.DataBind();

//GridView1.RenderControl(hw);

TextBox1.Text = "SATHEESH KUMAR AVULA";

TextBox1.DataBind();

TextBox1.Visible = true;

TextBox1.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();



First it was given me Error  :-

RegisterForEventValidation can only be called during Render();


I solved it by making 

EnableEventValidation="false"

Than Second Error:-

The document has no pages.

pls give some solution for that am really stuck for long days , my destination is i want to download whole Membership form in pdf format how can i ??????

thanks

Anoop S replied to avula on 28-Oct-10 06:18 AM
The thing is that ASP.NET has a very strong page lifecycle. RegisterForEventValidation can only be called during the Render phase of the page lifecycle. That means it will fail when calling RenderControl() at any other moment.
However, my first focus would be on GridBuilding. If that has anything on it that can cause a PostBack, then that will have to be turned off before rendering.

You have to turn the eventValidation off in order for this to work.

You can do this in the web.config file but in this case the eventValidation will be turned off for all the pages.

 <pages enableEventValidation ="false" ></pages>

or Add the @Page Directive in code behind file.

 EnableEventValidation ="false"

<%@ Page Language="C#" AutoEventWireup="true" Debug ="true" EnableEventValidation ="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

http://jamboreebliss.com/2009/08/20/registerforeventvalidation-can-only-be-called-during-render-2/
avula replied to Anoop S on 28-Oct-10 07:44 AM

why my code accepting only gridview control why not textbox and other control , can i export textbox ,checkbox and other controls to pdf ,pls provide some solutions for that

thanks

Exporting Textbox to pdf using iTextSharp

visves replied to avula on 17-Nov-10 04:56 AM

I have created html content as

<b>Test Html Content</b> : <input type='text' />

I export the content to pdf using itextsharp

the pdf file did not export textbox, it only export text content, how to export html as it is to pdf with images, text boxes, input fields and css?

kindly help me, my code is as follows

Document doc1 = new Document();

string path = Server.MapPath("RegFormPdf");

PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));

doc1.Open();

string htmlText = "<b>Test Html Content</b> <input type='text' />";

System.Collections.Generic.List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

for (int k = 0; k < htmlarraylist.Count; k++)

{

doc1.Add((IElement)htmlarraylist[k]);

}

doc1.Close();