ItextSharp帮助:将html转换为pdf? [英] ItextSharp Help: converting html to pdf?

查看:137
本文介绍了ItextSharp帮助:将html转换为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用免费的itextsharp从html文本创建pdf文档,并通过电子邮件将其作为附件发送.电子邮件和附件可以正常工作,但pdf内容显示为原始html文本,而不是呈现的html.

我已经遵循了大多数在线教程/文档,但是没有取得任何进展.任何人都可以查看下面的代码并提出替代方案或指出我做错了什么吗?

后面的VB代码:

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

推荐答案

这是一个很棒的 iTextSharp 教程站点. a>
here is a great tutorial site for iTextSharp


这篇关于ItextSharp帮助:将html转换为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆