ITextSharp Error while exporting GridView to PDF - c#

I tried exporting a GridView to PDF. It gives the error:
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
It throws the error here
htmlparser.Parse(sr);

You should disable sorting and paging before the databinding and parsing.
Here is the code:
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.AllowSorting = false; <----
GridView1.DataBind();
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();

Related

PDF Exported Document Formatting

I am downloading data in Pdf form, but my document is not grid look
Below is the code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientRecord.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.Style.Add("font-size", "10px");
GridView1.Style.Add("border", "solid 2px blue");
GridView1.DataBind();
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();
Can anybody help me with that, plz?

Converting HTML to pdf and save it to given location without opening "Save As" or "Download" window

I am converting html into pdf. It is working fine. but when i try to save html into any location it gives me error Network path not found.
I am having some line of code
#region Trying to convert pdf
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringReader sr = new StringReader(htmlStringToConvert);
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
#endregion
I am getting error at htmlparser.Parse(sr).
Thanks in advance
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DivMaster.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 20, 20, 10, 20);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Add DivMaster (div) in design page and Add ur HTML code inside that DivMaster.
Note: Use % or px throughout HTML design.

Exporting ListView to PDF

I am creating an application in which I successfully exported my ListView to PDF format. But I have some problem regarding my PDF: the formatting of the PDF is very bad.
Take a look:
I am using the following code to export to PDF:
protected void ExportToPdf_Click(object sender, EventArgs e)
{
string File = "PDFCollegeDetails";
var result = GetData().ToList();
ExportListToPDF(result, File);
}
private void ExportListToPDF(List<CollegeDetail> result, string File)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=CollegeDetailsInPdf.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView gridView = new GridView();
gridView.DataSource = result;
gridView.DataBind();
gridView.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 50f, 50f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
gridView.AutoGenerateColumns = true;
}
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ZoneWiseReport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridViewZoneReport.AllowPaging = false;
this.BindGridView();
GridViewZoneReport.DataBind();
GridViewZoneReport.RenderControl(hw);
GridViewZoneReport.HeaderRow.Style.Add("width", "15%");
GridViewZoneReport.HeaderRow.Style.Add("font-size", "10px");
GridViewZoneReport.Style.Add("text-decoration", "none");
GridViewZoneReport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridViewZoneReport.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Font size too small: 0 error in c# in exporting a content from gridview to pdf

my code is:
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView2.AllowPaging = false;
GridView2.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new iTextSharp.text.Document(PageSize.A2);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("contentdisposition","attachment;filename=CustomerReport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}*/
please help me to resolve this. In this i am getting font size too small error.Error in htmlparser.parse()

how to convert html page to pdf with css using itextsharp?

This is my 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);** //the exception here
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
the error is :
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type
'iTextSharp.text.Paragraph'.
What is this exception ?
I don't know if this is the answer, but I found that iTextSharp is picky about having valid html. My tests had a table that was opened twice and never closed and that took about an hour for me to notice. The exception was very similar to the one you have there.
use can you A4 size width & height for html deigning .
use this set od code to create pdf from html.
String htmlText = "<div>Your HTML text here</div>";
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\outfile.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile(Request.PhysicalApplicationPath + "\\outfile.pdf");
Response.Flush();
Response.Clear();

Categories