我正在使用iTextSharp将HTML内容转换为PDF文件,但是当阿拉伯语的HTML内容是这样时,我遇到了一个问题:
那就是我的代码:
Public Shared Function ExportToPDF(lang As String) As String
Try
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As New HtmlTextWriter(stringWrite)
divExport.RenderControl(htmlWrite)
Dim text As String
text = String.Format("<html><head><style type='text/css'>{0}</style></head><body>{1}</body></html>", "body{font-color:red;}", stringWrite.ToString)
Dim sr As New StringReader(text)
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.End()
Catch ex As Exception
End Try
Return Nothing
End Function
divExport:
<div id="divExport">
<h1>Header</h1>
<table>
<tr>
<td>Questions</td><td>الاسئلة</td>
</tr>
</table>
<h2>Questions Answers</h2>
<table>
<tr>
<td>Device Type</td><td>جهاز الكمبيوتر</td>
</tr>
</table>
</div>
最佳答案
XMLWorker
并非旨在能够处理阿拉伯语文本,据我所知(前iText员工),它无法处理阿拉伯语文本。
更好的解决方案是使用pdfHTML
(属于iText 7产品系列,并将HTML5和CSS3转换为PDF)。
为了正确呈现阿拉伯语(以及其他非西方脚本),您还需要pdfCalligraph
,它专门用于正确呈现非西方脚本。
This link提供有关pdfCalligraph
的更多说明
关于c# - 使用iTextSharp将HTML转换为PDF不支持阿拉伯语,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53519009/