mohd yasar

mohd yasar

  • NA
  • 6
  • 11.2k

itextsharp html image to pdf

May 27 2015 8:10 AM
sir
in blow code i want to convert html table to pdf and then e-mail,my code is working properly but i have a little problem.
i want to convert image into pdf in table cell(i am using image control) but not working meanwhile i can convert image without using image control.
bottom line is that i want to show image with in table cell (it comes by image control or any thing else.) 
 
 
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("[email protected]");
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("[email protected]", "123");
sc.Send(mailmess);
}
}
}
 

Answers (2)