Error while converting gridview into pdf format using iTextsharp - c#

I am having gridview in which in few records it has image and in few records it didn't have image which gives error while converting it to pdf format.This code works fine if each row have images but not in case if any record didn't have image. Can anyone suggest me what to do with code so that my error can be resolve
here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
grdview();
string file = "testc" + ".pdf";
string attachment = string.Format("attachment;filename={0}", file);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfdoc = new iTextSharp.text.Document();
pdfdoc.SetMargins(0f, 0f, 0f, 0f);
pdfdoc.SetPageSize(iTextSharp.text.PageSize.A2);
HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
pdfdoc.Open();
htmlparser.Parse(sr);
pdfdoc.Close();
Response.Write(pdfdoc);
Response.End();
}
Image what error appears while conversion

Related

ItextSharp arabic letters not showing up at all

Im using the Itextsharp library to create a pdf document that contains both arabic and english letters. However, the arabic letters dont show at all!
tried other librarys like pdfsharp but to no avail. Some have suggested changing the font but im not sure exactly how to do that
Code is below
protected void BtnExportPDFReport_Click(object sender, EventArgs e)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
grdShowReport.AllowPaging = false;
grdShowReport.DataBind();
grdShowReport.RenderControl(hw);
StringReader srw = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(srw);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=DatabaseReports.pdf");
Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style> td { mso-number-format:\\#; } </style> ");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}

Export to PDF using iText not displaying complete data from GridView

I am trying to export my GridView to PDF. When I click the button I can see that the document downloading with around 977KB but when it has finished download and browse to the download folder the file size then is 1KB.
This is the code I am using:
protected void ExportToPDF(object sender, EventArgs e)
{
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);
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();
}
I am not sure what is going wrong here?
Thanks

Converting a modal dialog window to pdf

I'm trying to convert a modal dialog with 3 DataTables to pdf using iTextSharp:
DataTable dtTable = (DataTable)ViewState["LoanPlan"];
DataTable dtTax = (DataTable)ViewState["PlanTaxData"];
DataTable dtPlan = (DataTable)ViewState["PlanAllData"];
I'm using this code to try and accomplish it:
protected void buttonPDF_Click(object sender, EventArgs e)
{
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.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);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
But it seems that it's actually trying to convert the whole .aspx page to .pdf format. How should I go about targeting just the modal dialogue or just the 3 DataTables?

Export to pdf | How to show option to open and save

I have this working code for exporting to pdf. When I export it , it asks me location to save file. Instead , I want to have option of OPEN or SAVE . Currently what I am doing is :
protected void btnPdf_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);
GridView2.AllowPaging = false;
GridView2.DataBind();
GridView2.RenderControl(hw);
GridView2.HeaderRow.Style.Add("width", "15%");
GridView2.HeaderRow.Style.Add("font-size", "10px");
GridView2.Style.Add("text-decoration", "none");
GridView2.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView2.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();
}
If you have the file on the server
Response.TransmitFile(//FILE PATH);
If you don't
Response.BinaryWrite(//Pass Byte Array)

how to export gridview image and data to PDF using asp.net or Export gridview data to PDF using asp.net

protected void Button4_Click(object sender, EventArgs e)
{
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);
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);//this is the error line
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
You will have to use 3rd party control for that
Try using iText Sharp library its free
for more info go here

Categories

Resources