How to export PDF FILE by click on particular Row
In this article i will explain how to export pdf file with click on particular row's button with click on it generate a report in pdf format.
First of all for export pdf file from asp.net a third party tool is required,because Dotnet don't do this directly so we use itextsharp for that.
I give a link from that download itextsharp.dll file and give a reference of it in your project.
http://sourceforge.net/projects/itextsharp/
Download it from this link.
Suppose we have a gridview in this data are bind from database .I Found in most of Projects that in gridview there is a button required of generate report with click on this button.I give a Example of that in attachement,check it
In Report also added gridview data,so here i give you a simple solution for that,
I give you a code with use of it click on that button you found report in pdf format.
make design page as per your requirement and bind your gridview as per Requirement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using DataLayer;
using System.Text;
using System.Xml.Linq;
using System.Collections;
namespace EventManagerApp.SuperAdmin
{
public partial class ListOfInvoices : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
grdBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void txtSearchInvoice_TextChanged(object sender, EventArgs e)
{
grdBind();
}
protected void grdBind()
{
try
{
EventManagerDataContext db = new EventManagerDataContext();
var q = from a in db.EMR_INVOICEs
join b in db.EMR_ACCOUNTs on a.Account_ID equals b.Account_ID
select new
{
CompanyName = b.CompanyName,
Number = a.Invoice_number,
Invoice_date = a.Invoice_date,
Invoice_total = a.Invoice_total,
Invoice_subject = a.Invoice_subject,
Invoice_description = a.Invoice_description,
Payment_date = a.Payment_date,
Invoice_ID = a.Invoice_ID
};
grid.DataSource = q.ToList();
grid.PageSize = int.Parse(drpPageSize.SelectedValue);
grid.DataBind();
}
}
catch
{
throw;
}
}
protected void grid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
EventManagerDataContext db = new EventManagerDataContext();
var q = from a in db.EMR_INVOICEs
join b in db.EMR_ACCOUNTs on a.Account_ID equals b.Account_ID
select new
{
CompanyName = b.CompanyName,
Number = a.Invoice_number,
Invoice_date = a.Invoice_date,
Invoice_total = a.Invoice_total,
Invoice_subject = a.Invoice_subject,
Invoice_description = a.Invoice_description,
Payment_date = a.Payment_date,
Invoice_ID = a.Invoice_ID
};
grid.DataSource = q.ToList();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void lnkPdf_Click(object sender, EventArgs e)
{
Button lnkPDf = (Button)sender;
GridViewRow clickedRow = (GridViewRow)lnkPDf.NamingContainer;
Label Invoice_date = (Label)clickedRow.FindControl("lbldate");
string dateofinvoice = Invoice_date.Text;
LinkButton CompanyName = (LinkButton)clickedRow.FindControl("lblaccount");
string nameofcompany = CompanyName.Text;
Label Number = (Label)clickedRow.FindControl("lblnumber");
string invoicenumber = Number.Text;
Label Invoice_total = (Label)clickedRow.FindControl("lblamount");
string totalofinvoice = Invoice_total.Text;
Label Invoice_subject = (Label)clickedRow.FindControl("lblsubject");
string subjectofinvoice = Invoice_subject.Text;
Label Invoice_description = (Label)clickedRow.FindControl("lblDescription");
string des = Invoice_description.Text;
Label Payment_date = (Label)clickedRow.FindControl("lblPaymentDate");
string dateofpayement = Payment_date.Text;
int index = clickedRow.RowIndex;
Session["Invoice_date"] = dateofinvoice;
Session["CompanyName"] = nameofcompany;
Session["Number"] = invoicenumber;
Session["Invoice_total"] = totalofinvoice;
Session["Invoice_subject"] = subjectofinvoice;
Session["Invoice_description"] = des;
Session["Payment_date"] = dateofpayement;
Response.Redirect("Report.aspx");
}
}
Here i store gridview values into session so that i can use it in another paGE for make report
I redirect this in ReportPage Because css is not generally supported to export pdf file.in most of cases it will create some error.so i redirect it to another page.
Now code for Reprt.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using DataLayer;
using System.Text;
using System.Xml.Linq;
using System.Collections;
namespace EventManagerApp.SuperAdmin
{
public partial class Report : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
lnkPdf_Click();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void lnkPdf_Click()
{
string dateofinvoice = (string)Session["Invoice_date"];
string nameofcompany = (string)Session["CompanyName"];
string invoicenumber = (string)Session["Number"];
string totalofinvoice = (string)Session["Invoice_total"];
string subjectofinvoice = (string)Session["Invoice_subject"];
string des = (string)Session["Invoice_description"];
string dateofpayement = (string)Session["Payment_date"];
Document doc = new Document();
var titleFont = FontFactory.GetFont("Arial", 16, Font.BOLD);
var subTitleFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
var boldTableFont = FontFactory.GetFont("Arial", 10, Font.BOLD);
var endingMessageFont = FontFactory.GetFont("Arial", 8, Font.ITALIC);
var bodyFont = FontFactory.GetFont("Arial", 10, Font.NORMAL);
try
{
PdfWriter.GetInstance(doc, System.Web.HttpContext.Current.Response.OutputStream);
doc.Open();
string imageFilePath = System.Web.HttpContext.Current.Server.MapPath("~/Images/temp-logo.png");
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
jpg.ScaleToFit(80f, 60f);
//Give space before image
jpg.SpacingBefore = 0f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.HEADER;
doc.Add(jpg);
doc.Add(new Paragraph("Factuuradres: DevRepublic", titleFont));
doc.Add(new Paragraph(nameofcompany, subTitleFont));
doc.Add(new Paragraph("dddd", subTitleFont));
doc.Add(new Paragraph("8011 LD", subTitleFont));
Paragraph p = new Paragraph("eventmanagement.nl", subTitleFont);
Paragraph q = new Paragraph("e-mail: info@eventmanagement.nl:", subTitleFont);
Paragraph r = new Paragraph("Kvk-nummer:", subTitleFont);
p.Alignment = Element.ALIGN_RIGHT;
q.Alignment = Element.ALIGN_RIGHT;
r.Alignment = Element.ALIGN_RIGHT;
doc.Add(p);
doc.Add(q);
doc.Add(r);
Paragraph date = new Paragraph(new Phrase("Datum:" + DateTime.Now, subTitleFont));
doc.Add(date);
Paragraph invoice_number = new Paragraph(new Phrase(invoicenumber, subTitleFont));
doc.Add(invoice_number);
Paragraph topic = new Paragraph(new Phrase(subjectofinvoice, subTitleFont));
doc.Add(topic);
Phrase phrase1 = new Phrase(Environment.NewLine);
doc.Add(phrase1);
PdfPTable table = new PdfPTable(3);
table.TotalWidth = 400f;
PdfPCell cell2 = new PdfPCell(new Phrase("Los- of maandtarief", new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK)));
PdfPCell cell1 = new PdfPCell(new Phrase("Specificaties Diensten", new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK)));
PdfPCell cell3 = new PdfPCell(new Phrase("Totaal", new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK)));
PdfPCell cell4 = new PdfPCell(new Phrase(des, new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK)));
PdfPCell cell5 = new PdfPCell(new Phrase(totalofinvoice, new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK)));
PdfPCell cell6 = new PdfPCell(new Phrase(totalofinvoice, new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK)));
cell1.BackgroundColor = new BaseColor(204, 204, 204);
cell2.BackgroundColor = new BaseColor(204, 204, 204);
cell3.BackgroundColor = new BaseColor(204, 204, 204);
cell4.BackgroundColor = new BaseColor(255, 255, 255);
cell5.BackgroundColor = new BaseColor(255, 255, 255);
cell6.BackgroundColor = new BaseColor(255, 255, 255);
table.AddCell(cell2);
table.AddCell(cell1);
table.AddCell(cell3);
table.AddCell(cell4);
table.AddCell(cell5);
table.AddCell(cell6);
doc.Add(table);
Phrase phrase2 = new Phrase(Environment.NewLine);
doc.Add(phrase2);
PdfPTable table2 = new PdfPTable(4);
table2.TotalWidth = 400f;
table2.LockedWidth = true;
PdfPTable tb = new PdfPTable(1);
tb.AddCell("Totaal (excl. BTW)");
tb.AddCell("vat…….BTW (21%)");
tb.AddCell("TOTAAL ");
doc.Add(tb);
Phrase phrase3 = new Phrase(Environment.NewLine);
doc.Add(phrase3);
Paragraph heading = new Paragraph("Wij verzoeken u het totaalbedrag binnen 14 dagen aan ons over te maken ", new Font(Font.FontFamily.HELVETICA, 14f, Font.BOLD));
Paragraph heading123 = new Paragraph("onder vermelding van het factuurnummer ", new Font(Font.FontFamily.HELVETICA, 14f, Font.BOLD));
doc.Add(heading);
heading123.SpacingAfter = 10;
doc.Add(heading123);
Paragraph space = new Paragraph(" ");
for (int i = 0; i <= 10; i++)
{
doc.Add(space);
}
Paragraph heading1 = new Paragraph("Adviestariefvergelijker.nl, Ton Albertsplan 3, 2728 AZ, Zoetermeer, telefoon 079-88 97 653, e-mail info@adviestariefvergelijker.nl, KvK 56259379, BTW nr: NL8520451899B01, ABN Amro Bank te Zoetermeer: 59.65.81.904, IBAN: 37ABNA0596581904, BIC voor ABN Amro Bank : ABNANL2A", endingMessageFont);
doc.Add(heading1);
doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename= Evenmanager.pdf");
System.Web.HttpContext.Current.Response.Write(doc);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
//Log error;
}
finally
{
doc.Close();
}
}
}
}
for result of this please check the attachement.