将自定义字体添加到 iTextSharp 中的段落

Adding a custom font to a paragraph in iTextSharp

我的目标是使用 iTextSharp 向段落添加自定义字体,但它似乎不起作用,我尝试使用 .ttf.otf 两种字体类型,但均无效。

我的代码:

var pgSize = new iTextSharp.text.Rectangle(378, 576);
Document document = new Document(pgSize);

BaseFont customfont = BaseFont.CreateFont("Azonix.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
iTextSharp.text.Font azonix = new iTextSharp.text.Font(customfont, 12);

PdfWriter.GetInstance(document, new FileStream("someFile.pdf", FileMode.Create));

document.Open();

Paragraph p = new Paragraph("example paragraph");
p.Alignment = Element.ALIGN_CENTER;
p.Font = azonix;

document.Add(p);
document.Close();

PDF 文件的输出使用默认字体而不是 azonix font。 字体也与可执行文件位于同一目录中,如图所示:

其他信息:

您首先向段落添加文本,然后设置字体:

Paragraph p = new Paragraph("example paragraph");
...
p.Font = azonix;

不过,要将字体应用于文本,请将字体与文本一起设置:

Paragraph p = new Paragraph("example paragraph", azonix);

或之前:

Paragraph p = new Paragraph();
...
p.Font = azonix;
p.Add("example paragraph");