Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringReader html = new StringReader(sb.ToString());
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
MemoryStream stream = new MemoryStream(byteArray);
Response.Clear();
using (iTextSharp.text.Document document = new iTextSharp.text.Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(
writer, document, stream, new System.Text.UTF8Encoding()
);
}
Response.End();
那么,因为我有
,所以pdf不显示unicode字符的原因可能是什么byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
和
.ParseXHtml(writer, document, stream, new System.Text.UTF8Encoding());
答案 0 :(得分:1)
Here is the few steps to display unicode characters in converting Html to Pdf
检查以下代码
TextReader reader = new StringReader(html);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.Create));
HTMLWorker worker = new HTMLWorker(document);
document.Open();
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle("body", "encoding", "Identity-H");
worker.Style = ST;
worker.StartDocument();
检查以下链接以获取更多理解....
使用此方法在从HTML转换为PDF时也会显示印地语,土耳其语和特殊字符。查看下面的演示图片。