我必須使用itextSharp在VB.Net和MSSQL 2005中開發的Web應用程序中創建運行時pdf。如何在itextSharp中設置HTML中的字體爲PDF
HTML保存在數據庫中。其中包含古吉拉特語,印地語和英語內容。
誰能告訴我如何爲html設置字體以及應該使用哪種字體顯示Enlgish,Gujarati和Hindi 我試過Arial Unicode MS,但是它不顯示印地文準確。
預先感謝您
這裏是我使用的HTML字符串轉換成PDF文件,用戶可以保存在本地機器上的方法的代碼。
Private Sub ExporttoPDF(ByVal FullHtml As String, ByVal fileName As String)
Try
Response.Clear() ' Clear Response and set content type and disposition so that user get save file dialogue.
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.pdf", fileName))
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sr As StringReader = New StringReader(FullHtml)
Dim pdfDoc As iTextSharp.text.Document = New iTextSharp.text.Document(PageSize.A4.Rotate, 10, 10, 10, 10)
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
Dim fontpath As String = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\fonts\ARIALUNI.TTF"
' "ARIALUNI.TTF" file copied from fonts folder and placed in the folder
Dim bf As BaseFont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
FontFactory.RegisterDirectory(System.Web.HttpContext.Current.Request.PhysicalApplicationPath , True)
FontFactory.Register(fontpath, "Arial Unicode MS")
FontFactory.RegisterFamily("Arial Unicode MS", "Arial Unicode MS", fontpath)
'parse html from String reader "sr"
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
Catch ex As Exception
Throw ex
End Try
End Sub
這裏是我如何使用代碼
dim htmlstring as string = "<html><body encoding=""" + BaseFont.IDENTITY_H + """ style=""font-family:Arial Unicode MS;font-size:12;""> <h2> set Font in itextSharp for HTML to PDF </h2> <span> I (aneel/અનિલ/अनिल) am facing problem to create a pdf from html that contains enlish, ગુજરાતી, हिंदी and other unicode characters. </span> </body></html>"
ExporttoPDF(htmlstring ,"sample.pdf")
在結果 爲古吉拉特它顯示અનલિ其中如預期的那樣અનિલ
凡爲印地文它顯示अनलि其中作爲它應該是有效的。
這個問題的靈感來自Vinit_Patel從這裏:http://stackoverflow.com/questions/21423993/itextsharp-htmlstring-to-pdf-unicode-are-missing – user2913925 2014-04-21 03:49:13