Html转换为PDF不显示Unicode字符

时间:2013-12-26 11:18:32

标签: c# asp.net pdf unicode itextsharp

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());

1 个答案:

答案 0 :(得分:1)

Here is the few steps to display unicode characters in converting Html to Pdf

  1. 创建HTMLWorker
  2. 注册unicode字体并指定它
  3. 创建样式表并将编码设置为Identity-H
  4. 将样式表分配给html解析器
  5. 检查以下代码

        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();
    
  6. 检查以下链接以获取更多理解....

    使用此方法在从HTML转换为PDF时也会显示印地语,土耳其语和特殊字符。查看下面的演示图片。

    enter image description here