Exporting ListView to PDF - c#

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

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?

Trying to Export Data Grid View to PDF using C#

Trying to Export the Data Grid View to a PDF file using C#
The btnexport_Click event calls the Method ExportGridToPDF();
Here is the code below:
private void btnexport_Click(object sender, EventArgs e)
{
ExportGridToPDF();
}
private void ExportGridToPDF()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GetStudent.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
dataGridView1.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();
dataGridView1.AllowPaging = false;
dataGridView1.DataBind();
}

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.

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()

ITextSharp Error while exporting GridView to PDF

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

Categories