【Spire.PDF】无法显示中文的情况

在导出PDF报告时发现中文字符无法显示,

于是更换字体测试,发现是某些字体不支持中文

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Font font = new Font("Arial", 10, FontStyle.Regular);

PdfTrueTypeFont pdfTrueTypeFont = new  PdfTrueTypeFont(font, true);
PdfSolidBrush pdfSolidBrush = new PdfSolidBrush(Color.Black);

page.Canvas.DrawString("123ABC哈哈", pdfTrueTypeFont, pdfSolidBrush, new PointF(100,100));

string path = Application.StartupPath + "\\Reports\\" + "test" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
doc.SaveToFile(path);
doc.Close();
System.Diagnostics.Process.Start(path);//打开文档
  • Arial不能显示中文
    在这里插入图片描述

  • 换成Calibri测试,结果不能显示中文
    在这里插入图片描述

  • 换成Microsoft Yahei测试,结果能显示中文
    在这里插入图片描述

突然想起上次出现中文乱码的情况,于是猜测有没有跟出现中文乱码是编码格式类似的情况

于是查看Font类,
在这里插入图片描述
最后一个构造函数的参数最齐全,于是研究下这个
在这里插入图片描述
写成这样

Font font = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point, 1, true);

可以正常显示中文,
在这里插入图片描述
但是已经和原来的字体不一样了

所以,要显示中文,还是更换成别的字体

猜你喜欢

转载自blog.csdn.net/weixin_38211198/article/details/89575223