Adding Header Page in Itextsharp when the value is from gridview - asp.net

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

Related

How to create a detailed report in ASP.NET

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

blank pdf coming while exporting to pdf in asp.net

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

how to convert aspx page to pdf with gridview and css class design

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.

How can i export html table which is created dynamically to pdf format in asp.net

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

Merging 2 gridviews into one generated PDF file using iTextSharp

I'm using 2 gridviews. The first one has paging enabled, and the number of rows allowed per page is one. The second gridview takes values from the first row as time range, and everytime I change the page on gridview1 the second GV changes it's content automatically based on GV1's values.
Previously, I was able to generate a PDF file using iTextSharp with only one gridview and no paging enabled. But now I'm struggling first of all with the paging enabled, and second with merging both gridviews into one pdf file.
Anybody know how can I do that?
Thanks in advance.
EDIT: This is the code I use to generate a PDF file from a gridview using iTextSharp.
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gvReportes.AllowPaging = false;
gvReportes.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvReportes.HeaderRow.Style.Add("font-size", "7.20px");
gvReportes.HeaderRow.Style.Add("color", "#284775");
gvReportes.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvReportes.Style.Add("font-size", "6px");
gvReportes.RenderControl(htextw);
Document document = new Document();
string path = "path.pdf";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
Response.Write(document);
document.Close();
And this is how the gridviews are displayed, as you can see the values "Salida" and "Llegada" on GV1 work as a time range to display data on GV2.
protected void Button1_Click(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=injoin.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
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();
}

Resources