Trying to Export Data Grid View to PDF using C# - 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();
}

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

Error in creating pdf file using iTextSharp

I have one Gridview in my asp .Net project (c#) and i want to export it to pdf
for this purpose i'm using iTextSharp as bellow:
public override void VerifyRenderingInServerForm(Control control)
{ /* Do nothing */ }
protected void Button2_Click1(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=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
ASPxGridView1.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();
}
when i test it , i get this error:
The UNC path should be of the form \server\share
anybody pls help me?

Categories