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

C# 如何使用iTextSharp在pdf中显示中文字体?,c#,asp.net,html,pdf,itextsharp,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.

在下面的代码中,中文字体(包含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;
    }
publicstaticbool-GeneratedPDF(字符串strHTMLText、字符串文件名、字符串操作、字符串类型)
{
bool blnReturn=false;
字符串fontpath=HttpContext.Current.Server.MapPath(“~/files/fonts/”);
字符串filepath=HttpContext.Current.Server.MapPath(“~/files/pdf/”);
BaseFont customfont=BaseFont.CreateFont(fontpath+“simhei.ttf”、BaseFont.IDENTITY、BaseFont.EMBEDDED);
Font Font=新字体(自定义字体,12);
//List htmlaraylist=iTextSharp.text.html.simpleparser.HTMLWorker.parsetList(new System.IO.StringReader(strHTMLText),null);
iTextSharp.text.Document Document=新的iTextSharp.text.Document();
如果(rpttype.Trim().ToUpper()=“报告”)
document=new iTextSharp.text.document(iTextSharp.text.PageSize.A2.Rotate());
else if(rptype.Trim().ToUpper()=“网格”)
document=new iTextSharp.text.document(iTextSharp.text.PageSize.A4.Rotate());
其他的
document=新的iTextSharp.text.document(iTextSharp.text.PageSize.LETTER);
iTextSharp.text.pdf.PdfWriter writer=iTextSharp.text.pdf.PdfWriter.GetInstance(文档,新文件流(filepath+“\\”+文件名,FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.StyleSheet styles=新的iTextSharp.text.html.simpleparser.StyleSheet();
样式。LoadTagStyle(“正文”、“字体系列”、“verdana”);
样式。加载样式(“正文”、“字体大小”、“5px”);
List htmlaraylist=iTextSharp.text.html.simpleparser.HTMLWorker.parsetList(新的System.IO.StringReader(strHTMLText),样式);
for(int k=0;k
您将
样式表设置错误。应该是这样的:

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);
  }
}
stringhtml=@”
使用文字做PDF文件輸出

使用文字做PDF文件輸出

使用文字做PDF文件輸出

使用文字做PDF文件輸出

使用文字做PDF文件輸出

"; 注册(“c:/windows/fonts/ARIALUNI.TTF”); 样式表样式=新样式表(); 样式。LoadTagStyle(“主体”、“面”、“Arial Unicode MS”); style.LoadTagStyle(“正文”,“编码”,BaseFont.IDENTITY_H); 使用(文档=新文档()){ PdfWriter writer=PdfWriter.GetInstance( 文档,Response.OutputStream ); document.Open(); foreach(HTMLWorker.parsetList中的IEElement元素( 新的StringReader(html.ToString()),样式) { 文件。添加(元素); } }
您需要将上面的
LoadTagStyle()
第三个参数更改为所使用的特定字体的正确名称。换句话说,将上面的“Arial Unicode MS”替换为字体的名称/值。或者你也可以使用上面的字体