简体   繁体   中英

Adding a custom font to a paragraph in iTextSharp

My goal is to add a custom font to a paragraph using iTextSharp however it does not seem to work, I have tried with both .ttf and .otf font types however neither worked.

My code:

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

The output of the PDF file uses the default font instead of the azonix font . The font is also inside of the same directory as the executable as shown in the picture:

图片

Other information:

  • Windows 10 Pro
  • .NET Framework 4.7.2
  • Windows Forms Application

You first add text to the paragraph and, thereafter, set the font:

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

To have the font applied to the text, though, set the font together with the text:

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

or before it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM