Hi,
I am trying to use the free itextsharp to create a pdf document from a html text and send it by email as an attachment. The Email and the attachment works fine but the pdf content shows as a raw html text instead of rendered html.
I have followed most of the online tutorials/documents but I am not making any progress. Can any one review the code below and suggest alternatives or point out what I am doing wrong please?
VB Code behind:
Dim sb As New StringBuilder
Dim sw As New StringWriter(sb)
Dim htmlTW As New HtmlTextWriter(sw)
Dim strHTMLInvoice As String
detsQuotations.RenderControl(htmlTW)
strHTMLInvoice = sb.ToString()
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient
Dim emailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
Dim newLine As String
Dim boldText, endBoldText, formatBody, endFormatBody As String
Dim Quotation As String
Dim view As Net.Mail.AlternateView
Dim mailclient As Net.Mail.SmtpClient
Dim msgText As New StringBuilder
newLine = "<br>"
boldText = ""
endBoldText = ""
formatBody = "<html><body> "
endFormatBody = "</body></html>"
Quotation = strHTMLInvoice
Dim msg As New Net.Mail.MailMessage(mailEmail, QuoteEmail)
msg.CC.Add(New System.Net.Mail.MailAddress(mailEmail))
msg.Bcc.Add(New System.Net.Mail.MailAddress(mailEmail))
msg.Subject = "Your Requested Quotation"
msg.IsBodyHtml = True
msgText.Append(formatBody & "<table><tr><td align="left" valign="top">" & QuoteCompany & "<br /> " & QuoteAddress & " <br /> " & QuoteCity & " <br /> " & QuotePostCode & " <br /><br />Dear " & QuoteContact & ",<br /><br />Please see below your requested quotation.</td><td align="left" valign="top"></td><td align="right" valign="top"></td></tr></table>" & endFormatBody)
msgText.Append(formatBody & "<table align="left"><tr><td align="left"><br /><table><tr><td> </td></tr></table>" & Quotation & "<br />This quotation is valid for 30 days from quote date " & "(" & QuoteDate & ")" & ". <br /> <br />Thank you for your request.<br /><br /><br />Kind Regards,<br />" & EmailSender & "<br />" & companyName & "<br />" & companyaddress & ", " & companycity & ", " & companypostcode & "<br /> Tel: " & companyPhone & ", Fax: " & companyFax & "<br /> Email: " & companyEmail & "<br />" & companyWeb & "</td></tr></table></br><br></br>" & endFormatBody)
msgText.Append("")
view = Net.Mail.AlternateView.CreateAlternateViewFromString(msgText.ToString(), Nothing, "text/html")
msg.AlternateViews.Add(view)
mailclient = New Net.Mail.SmtpClient()
mailclient.Host = mailSMTP
mailclient.Credentials = New Net.NetworkCredential(mailEmail, mailPassword)
view = Net.Mail.AlternateView.CreateAlternateViewFromString(msgText.ToString(), Nothing, "text/html")
msg.AlternateViews.Add(view)
'Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Try
Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(Server.MapPath("~\Documents\Quotation.pdf"), FileMode.Create))
'Open Document to write
doc.Open()
Dim paragraph As New Paragraph(msgText.ToString)
doc.Add(paragraph)
Response.Close()
Catch dex As DocumentException
'Handle document exception
Catch ioex As IOException
'Handle IO exception
Catch ex As Exception
'Handle Other Exception
Finally
'Close document
doc.Close()
End Try
msg.Attachments.Add(New Net.Mail.Attachment(Server.MapPath("~\Documents\Quotation.pdf")))
mailclient.Send(msg)
detsQuotations.Rows(0).Visible = True