priyanka

priyanka

  • NA
  • 215
  • 123k

How to send email as well as gridview content in pdf format

Aug 5 2017 7:08 AM
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[8] {
new DataColumn("Product"),
new DataColumn("Description"),
new DataColumn("Qty"),
new DataColumn("Gross Amount"),
new DataColumn("Discount"),
new DataColumn("Taxable Value"),
new DataColumn("IGST"),
new DataColumn("Total")
});
dt.Rows.Add("P&Y Fashion Sling Bag BLACK P&YFASHION " +
"MESSENGER SLING BAG BLACK Z00084 | "+
"Z00084 | 10 day Replacement Guarantee **", "hand_messenger_bag | HSN: " +
"42023120 | IGST: 28%", 1, 1299, -0, 1014.84, 284.16, 1299);
gv.DataSource = dt;
gv.DataBind();
SendPDFEmail(dt);
}
}


public override void VerifyRenderingInServerForm(Control control)
{

/* Verifies that the control is rendered */

}

private void SendPDFEmail(DataTable dt)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{


gv.RenderControl(hw);
StringReader sr = 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(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();

MailMessage mm = new MailMessage("[email protected]", "[email protected]");
mm.Subject = "iTextSharp PDF";
mm.Body = "iTextSharp PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "iTextSharpPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "[email protected]";
NetworkCred.Password = "8235203785";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
 
I am able to export only gridview,not the whole content to pdf and send email in c#
 

Answers (5)