using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; using System.Data; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser; using System.Net.Mail; using System.IO; public partial class Gridview_html_pdf : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {} protected void Button1_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { string dname = row.Cells[0].Text; string drollno = row.Cells[1].Text; string demail = row.Cells[2].Text; string dgender = row.Cells[3].Text; string dimage = row.Cells[4].Text; //Image1.ImageUrl = ("~/images/male.png"); calling through URL Label1.Text = dname; Label2.Text = drollno; Label3.Text = demail; Label4.Text = dgender; DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Roll-no"); dt.Columns.Add("email"); dt.Columns.Add("Gender"); //dt.Columns.Add("img"); dt.Rows.Add(); dt.Rows[0]["Name"] = Label1.Text; dt.Rows[0]["Roll-no"] = Label2.Text; dt.Rows[0]["email"] = Label3.Text; dt.Rows[0]["Gender"] = Label4.Text; //dt.Rows[0]["img"] = dimage; //Bind Datatable to Labels Label1.Text = dt.Rows[0]["Name"].ToString(); Label2.Text = dt.Rows[0]["Roll-no"].ToString(); Label3.Text = dt.Rows[0]["email"].ToString(); Label4.Text = dt.Rows[0]["Gender"].ToString(); //Image1.ImageUrl = Server.MapPath(".") + dimage; ; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); Panel1.RenderControl(htw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); MemoryStream ms = new MemoryStream(); PdfWriter.GetInstance(pdfDoc, ms); pdfDoc.Open(); htmlparser.Parse(sr); string imageURL = Server.MapPath(".") + dimage; iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL); //Resize image depend upon your need jpg.ScaleToFit(140f, 120f); //Give space before image jpg.SpacingBefore = 10f; //Give some space after the image jpg.SpacingAfter = 1f; jpg.Alignment = Element.ALIGN_LEFT; pdfDoc.Add(jpg); pdfDoc.Close(); byte[] bytes = ms.ToArray(); MailMessage mailmess = new MailMessage(); mailmess.From = new MailAddress("qqq@gmail.com"); mailmess.To.Add(new MailAddress(demail)); mailmess.Subject = "Employee Information"; mailmess.Body = "Please find the attached pdf document "; mailmess.Attachments.Add(new Attachment(new MemoryStream(bytes), "EmployeeInformation.PDF")); mailmess.IsBodyHtml = true; SmtpClient sc = new SmtpClient("smtp.gmail.com", 587); sc.EnableSsl = true; sc.Credentials = new System.Net.NetworkCredential("qqq@gmail.com", "123"); sc.Send(mailmess); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)