c# - 如何使用 iTextSharp 在 pdf 中显示中文字体?

标签 c# asp.net html pdf itextsharp

在下面的代码中,中文字体(包含 html 文本)不会在生成的 pdf 中显示。 我也在这种方法中尝试样式和字体。请帮忙解决这个问题。 在此先感谢大家。

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 错误。它应该是这样的:

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

您需要将上面 LoadTagStyle()third 参数更改为您正在使用的特定字体的正确名称。换句话说,将上面的“Arial Unicode MS”替换为您的字体的名称/值。或者你也可以使用上面的字体。

关于c# - 如何使用 iTextSharp 在 pdf 中显示中文字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8614295/

相关文章:

c# - 在 C# 对象上实现 INotifyPropertyChanged 后获取 "Key cannot be null"

c# - 使用 C# 中的 Windows 服务从 FTP 服务器复制并删除文件

jquery - 使用 jQuery 根据按钮文本更改按钮上的 CSS 属性

C# 打开图片文件夹

c# - 跨项目访问和更新变量 C#

c# - 如何阻止事件触发 + ASP.NET

asp.net - OpenAM、OpenId、REST API、内部应用程序 : how do I connect them all?

c# - 将 LINQ to EF 查询加速到最大

java - 如何将数据参数从 Java 代码发送到 HTML?

python - 从 html 转换为 pdf 的页码 - pdfkit,python/django