Spire.PDF是一款专业的PDF文档处理控件,该产品提供了几种字体类,如:PdfFont, PdfTrueTypeFont and PdfCjkStandardFont,使用这些字体类开发人员可以为PDF文件添加多种字体,在这篇文章将会介绍如何生成支持简体中文或繁体中文的字符,在使用这些字体时首先需要查看您的系统里是否安装了对应的字体包,如果没有安装可以参考从下面的链接下载字体文件:
http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=5508&fileID=5521
当安装了字体文件后,就可以通过下面的代码为PDF文件设置相应的字体了,具体如下:
using System.Drawing;
namespace Add_Chinese_Characters_to_PDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11f);
page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50);
page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70);
pdf.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");
}
}
}