Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 3-Oct-10 16:27pm
v3
Comments
HimanshuJoshi 3-Oct-10 22:27pm    
Edited for code block

1 solution

here is a great tutorial site for iTextSharp
 
Share this answer
 
Comments
Member 8450910 23-Feb-13 2:49am    
I want to convert Repeater Controls into PDF threw vb.net.

My code is
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Invoice.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim titleFont As Font = FontFactory.GetFont("Helvetica", 14, Font.BOLD, New iTextSharp.text.Color(0, 179, 212))
Dim textfont As Font = FontFactory.GetFont("Helvetica", 9, Font.NORMAL, Color.BLACK)

Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)

pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()



Problem is in Displaying data in PDf .it is not same as in aspx page.Please tell how can convert page into PDF in same format as in aspx page.


Thanks & Regards
Annu Rani

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900