如何使用iTextSharp在pdf中显示中文字体? [英] How to do to display Chinese font in pdf using iTextSharp?

查看:485
本文介绍了如何使用iTextSharp在pdf中显示中文字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,中文字体(包含html文本)不显示在生成的pdf中. 我也在这种方法中尝试样式和字体.请帮助解决此问题. 在此先感谢所有人.

In the following code , chinese font( contained html text) doesnot display in pdf generated. I also try styles and font in this method. Please help to solve this problem. Thanks in advance to all.

public static bool GeneratedPDF(string strHTMLText, string filename, string action, string rpttype)
    {
        bool blnReturn = false;

        string fontpath = HttpContext.Current.Server.MapPath("~/files/fonts/");
        string filepath = HttpContext.Current.Server.MapPath("~/files/pdf/");

        BaseFont customfont = BaseFont.CreateFont(fontpath + "simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

        Font font = new Font(customfont, 12);


        //List<iTextSharp.text.IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new System.IO.StringReader(strHTMLText), null);


        iTextSharp.text.Document document = new iTextSharp.text.Document();

        if (rpttype.Trim().ToUpper() == "REPORT")
            document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A2.Rotate());

        else if (rpttype.Trim().ToUpper() == "GRID")
            document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate());

        else
            document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER);

        iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(filepath + "\\" + filename, FileMode.Create));
        document.Open();

        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();

        styles.LoadTagStyle("body", "font-family", "verdana");

        styles.LoadStyle("body", "font-size", "5px");


        List<iTextSharp.text.IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new System.IO.StringReader(strHTMLText), styles);
        for (int k = 0; k < htmlarraylist.Count; k++)
        {

            document.Add((IElement)htmlarraylist[k]);

        }

        iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();

        p.InsertRange(0, htmlarraylist);

        p.Font = font; // font does not work 
        document.Add(p);

        document.Close();

        blnReturn = true;

        return blnReturn;
    }

推荐答案

您将StyleSheet设置为错误.应该是这样的:

You set the StyleSheet wrong. It should be something like this:

string html = @"
<html><body>    
<p>使用iText做PDF文件輸出</p>    
<p>使用iText做PDF文件輸出</p>    
<p>使用iText做PDF文件輸出</p>    
<p>使用iText做PDF文件輸出</p>    
<p>使用iText做PDF文件輸出</p> 
</body></html>   
";
FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF"); 
StyleSheet style = new StyleSheet();
style.LoadTagStyle("body", "face", "Arial Unicode MS");
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(
    document, Response.OutputStream
  );
  document.Open();
  foreach(IElement element in HTMLWorker.ParseToList(
      new StringReader(html.ToString()), style))
  {
    document.Add(element);
  }
}

您需要将上方 third 参数更改为所使用的特定字体的正确名称.换句话说,将上面的"Arial Unicode MS"替换为字体的名称/值.或者,您也可以使用上面的字体.

You need to change the third parameter of LoadTagStyle() above to the correct name of the specific font you're using. In other words, replace "Arial Unicode MS" above with the name/value for your font. Or you can use the font above too.

这篇关于如何使用iTextSharp在pdf中显示中文字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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