TextBox controls are not Working with Export To PDF(iTextSharp) - asp.net

I have an HTML form, which contains lables and textboxes.
After filling this form, it is exported to PDF.
All the label Texts are exported. But the textbox text is not exported to PDF.
Code
protected void btnExportPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=DecForm.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.divToPdf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10, 10, 2, 10);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Why are the textbox text not exported through to PDF?

I think that when you're rendering divToPDF you are getting a fresh cut of the html and it does not have the values there were populated on the page. You may want to look at using the divToPDF is you'll want to look at accessing the InnerHtml or OuterHtml property and use that.

Try this.
List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlString), null);
//htmlString is your form html
foreach (IElement el in elements)
{
pdfDoc.add(el);
}

Related

how to convert aspx page to pdf with gridview and css class design

How to convert .aspx page with css class design to pdf format which contains asp.net controls.
I have used some code but its not covert the page with proper allignment and design.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
a.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
I have used this code..
Can you help me.please.

How can i export html table which is created dynamically to pdf format in asp.net

I have created html table dynamically in asp.net. Now i want to export that displaying table into pdf format. I have used following code to export to pdf format.
protected void BtnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=SearchBooking.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//tblPayslip.AllowPaging = false;
tblPayslip.DataBind();
tblPayslip.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);
Response.Write(pdfDoc);
pdfDoc.Close();
Response.End();
}
and its giving me error as
The document has no pages.
Have you tried closing your document before writing it in response?
Change
Response.Write(pdfDoc);
pdfDoc.Close();
To
pdfDoc.Close();
Response.Write(pdfDoc);

Export to pdf button click event automatically calls 3 times+itext dll+asp.net,c#

I am usinng iText dll to export html to pdf.
an asp button is used for exporting.pdf is generating but the button click event is automatically calling 3 times.
here is the code
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvDetails.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 100f,0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
I would check the code that wires up the event handler.
It may seem like it's wired up multiple times, which results in the method being invoked multiple times

aspx to pdf using itextSharp 5.3.0

I am using this dll iTextSharp 5.3.0 to make a pdf file .
Is there a way to convert full .aspx page in pdf ? My page has grids and server side code .
This is my code:
protected void Button1_Click(object sender, EventArgs e)
{
createPDF(Server.MapPath("Default.aspx"));
}
private void createPDF(string html)
{
TextReader reader = new StringReader(html);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://test.pdf", FileMode.Create));
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
List<IElement> p = HTMLWorker.ParseToList(new StreamReader(html), new StyleSheet());
for (int k = 0; k < p.Count; k++)
{
document.Add((IElement)p[k]);
}
worker.EndDocument();
worker.Close();
document.Close();
}
It's working but the file test.pdf is just plain text. The html isn't well interpreted, my grids are missing and my server side values (the values from the grids) are also missing .
I also tried the codes from here:
http://forums.asp.net/t/1199774.aspx
and here:
Problem with HTMLParser in Itextsharp
Thanks in advance!
This is my honest advice! Don't waste your time on the HTMLWorker.ParseToList. It has a very elementary HTML parser.
Try this packge and you will never look back!
https://github.com/pruiz/WkHtmlToXSharp
ITextSharp only renders inline css, it is giving problem while adding CSS files.
System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=BookingDetails.pdf");
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.CreateBookingMainDiv.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(new Rectangle(922,1296),7f,7f,7f,0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
//HtmlPipeline
CssAppliers ca = new CssAppliersImpl();
//ICssFile cfile = new CssFileProcessor();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(ca);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
//CSS stuff
//var cssResolver = new StyleAttrCSSResolver();
//var DamcoCss = XMLWorkerHelper.GetCSS(new FileStream(HttpContext.Current.Server.MapPath("~/css/damco.css"), FileMode.Open));
ICssFile cfile = new CssFileImpl();
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
//String DamcoCss = HttpContext.Current.Server.MapPath("~/css/damco.css");
//String BootStrapCss = HttpContext.Current.Server.MapPath("~/css/bootstrap.css");
//String BootStrapCssTheme = HttpContext.Current.Server.MapPath("~/css/bootstrap-theme.css");
//Add the external CSS file
//cssResolver.AddCssFile(DamcoCss, true);
//cssResolver.AddCssFile(BootStrapCss, true);
//cssResolver.AddCssFile(BootStrapCssTheme, true);
//Pipeline
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer)));
//XMLWorker
XMLWorker worker = new XMLWorker(pipeline, true);
//and...we parse
XMLParser parser = new XMLParser(true, worker);
//parser.AddListener(worker);
parser.Parse(sr);
parser.Flush();
pdfDoc.Close();
System.Web.HttpContext.Current.Response.Write(pdfDoc);
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
//System.Web.HttpContext.Current.Response.End();
Use XMLWorker instead of HTMLWorker. works like charm.

Merging 2 gridviews into one generated PDF file using iTextSharp

I'm using 2 gridviews. The first one has paging enabled, and the number of rows allowed per page is one. The second gridview takes values from the first row as time range, and everytime I change the page on gridview1 the second GV changes it's content automatically based on GV1's values.
Previously, I was able to generate a PDF file using iTextSharp with only one gridview and no paging enabled. But now I'm struggling first of all with the paging enabled, and second with merging both gridviews into one pdf file.
Anybody know how can I do that?
Thanks in advance.
EDIT: This is the code I use to generate a PDF file from a gridview using iTextSharp.
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gvReportes.AllowPaging = false;
gvReportes.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvReportes.HeaderRow.Style.Add("font-size", "7.20px");
gvReportes.HeaderRow.Style.Add("color", "#284775");
gvReportes.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvReportes.Style.Add("font-size", "6px");
gvReportes.RenderControl(htextw);
Document document = new Document();
string path = "path.pdf";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
Response.Write(document);
document.Close();
And this is how the gridviews are displayed, as you can see the values "Salida" and "Llegada" on GV1 work as a time range to display data on GV2.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //ma
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=injoin.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.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();
}

Resources