Ajit More

Ajit More

  • NA
  • 569
  • 334.3k

How to create pdf and save into database and also send mail?

Aug 8 2014 1:35 AM
Hi, Friends this is my code to generate pdf and send as attachment.
But i need to save into database also.
Following are the code please help me...?
========================================================================

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("DESCRIPTION"),
new DataColumn("QTY IN MTR"),
new DataColumn("RATE PER MTR")});
dt.Rows.Add("100 NB SCH 10", "12", "2715.00");
dt.Rows.Add("80 NB SCH 10","12", "2715.00");
dt.Rows.Add("15 NB SCH 10", "12", "2715.00");
dt.Rows.Add("80 NB SCH 10", "12", "2715.00");
SendPDFEmail(dt);
}
}

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

string customername = "XYZ";
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr><td align='left' style ='border-width: medium; border-color: #C0C0C0; border-bottom-style: groove;' colspan = '2'><img src='~/images/logo.png'/></td></tr>");
sb.Append("<tr><td colspan = '2'></td></tr>");
sb.Append("<tr><td>Dear &nbsp;");
sb.Append(customername);
sb.Append("</td><td align='rignt'><b>Date: </b>");
sb.Append(DateTime.Now);
sb.Append(" </td></tr>");
sb.Append("<tr><td colspan = '2'>We are send you quotation ............................................................................................................................................</td></tr>");
sb.Append("</table>");
sb.Append("<br />");
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th style = 'background-color: #9BCE58;color:#ffffff'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
StringReader sr = new StringReader(sb.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 = "Quotation";
mm.Body = "Please Find Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), customername+".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 = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}

Answers (1)