i have one webform page . i have added everything in one div and wanted to export this div in pdf . while exporting in pdf . pdf is coming with empty.
protected void btnExport_Click(object sender, EventArgs e)
{
String DownloadFile = String.Format("{0}Targetkeywordsofdt-{1}", ddlproject.SelectedItem.Text, txtEndDate.Text.ToString() + ".pdf");
Response.AddHeader("content-disposition", "attachment;filename=" + DownloadFile);
Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
PdfReport.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);
string strpath = Server.MapPath("~") + "/Temp/" + ddlproject.SelectedItem.Text.Trim() + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strpath, FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
pdfDoc.Dispose();
Response.Write(pdfDoc);
Response.End();
}
Related
I want to make convert gridview to pdf, the code is run well, but iam confused how to make headerpage (not header column) on it, ill try to use stringbuilder but when i tried the gridview is not view in pdf, i also tried other way but still dont know how to make it works,
maybe u can teach me how to make it works?
in this code i tried to use chunk, but the chunk isnot show :(
protected void btnConvertPDF_Click(object sender, EventArgs e)
{
gridconvertPDF.AllowPaging = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm hf = new HtmlForm();
gridconvertPDF.Parent.Controls.Add(hf);
hf.Attributes["runat"] = "server";
hf.Controls.Add(gridconvertPDF);
hf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.Open();
Chunk c = new Chunk
("PEMINJAMAN INVENTARIS \n",
FontFactory.GetFont("Verdana", 25));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("Rizki Asriningtyas \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
pdfDoc.Add(p);
pdfDoc.Add(p1);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();}
my fault is make pdfwriter after add all the header content to pdf, here is the right code
protected void btnConvertPDF_Click(object sender, EventArgs e)
{
gridconvertPDF.AllowPaging = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm hf = new HtmlForm();
gridconvertPDF.Parent.Controls.Add(hf);
hf.Attributes["runat"] = "server";
hf.Controls.Add(gridconvertPDF);
hf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
Chunk c = new Chunk
("PEMINJAMAN INVENTARIS \n",
FontFactory.GetFont("Verdana", 25));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("Rizki Asriningtyas \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
pdfDoc.Add(p);
pdfDoc.Add(p1);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();}
I want to give a file name according to project name. This line below is naming pdf statically
Response.AddHeader("content-disposition", "attachment;filename=WeeklyReport.pdf");
output file name - weeklyreport.pdf
while giving dynamic name its not taking .pdf
Response.AddHeader("content-disposition", "attachment;filename=" + Projname + ".pdf");
output-TestingProject
here .pdf is missing , so not opening in pdf by default
To make pdf file and name it dynamically u can use like this
Response.AddHeader("content-disposition", Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}.pdf\"", Projname));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
design.RenderControl(htmlWrite);
string myText = stringWrite.ToString().Replace("&", "&");
StringReader sr = new StringReader(myText.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
Response.ContentType = "application/pdf";
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.End();
pdfDoc.Dispose();
I want to create a report with very detailed data in ASP.NET. This report should be exportable to PDF and Excel. Can you tell me tips on how to do this kind of report?
I'm having a problem linking up my datasets in ReportViewer.
You will find below a screenshot of what I want :
Using iTextSharp download:
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=this.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
GridView1.AllowPaging = false;
GridView1.Parent.Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document PDFdoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F);
iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(PDFdoc);
PdfWriter.GetInstance(PDFdoc, Response.OutputStream);
PDFdoc.Open();
htmlparser.Parse(sr);
PDFdoc.Close();
Response.Write(PDFdoc);
Response.End();
To Excel:
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "YourExcelFile.xls"));
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
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.
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);