seriverma says:
i have tried one article you have written:
http://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx.
but i here i dont want to open this pdf file. i want to attached and send this file via email using vb.net . please help me
Refer the below link for your reference how to attach Pdf file which is created using iTextSharp and implement code as per your code logic.
Also check how to send email with PDF attachment created by iTextSharp from Previous Article Code.
C#
Import NameSpace
using System.Net.Mail;
using System.Net;
SendEmail Event
protected void SendEmail(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
MemoryStream memoryStream = new MemoryStream();
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = "iTextSharp PDF";
mm.Body = "iTextSharp PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "TestPage.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
VB.Net
Import NameSpace
Imports System.Net.Mail
Imports System.Net
SendEmail Event
Protected Sub SendMail(sender As Object, e As EventArgs)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
Dim memoryStream As New MemoryStream()
PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Dim mm As New MailMessage("sender@gmail.com", "receiver@gmail.com")
mm.Subject = "iTextSharp PDF"
mm.Body = "iTextSharp PDF Attachment"
mm.Attachments.Add(New Attachment(New MemoryStream(bytes), "TestPage.pdf"))
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Sub