Search code examples
itextpdfsharp

iTextSharp equivalent of XPdfFontOptions


I'm converting a C# app page which uses PdfSharp to iTextSharp & have found a line of code I can't see an obvious replacement for.

The existing code is

PdfSharp.Drawing.XPdfFontOptions options = new  PdfSharp.Drawing.XPdfFontOptions(PdfFontEncoding.Unicode, 
PdfFontEmbedding.Always);

Also, what if I want to use other, non-base fonts? I can see from the docs how to create one of the 16 types, however what if I want "Frutiger LT 45 Light"?

Thanks in advance.


Solution

  • Have a look at the examples from iText in Action — 2nd Edition Chapter 11: Choosing the right font; the .Net versions are available here.

    You'll see that fonts can be selected, configured, and used like this:

    public const string FONT = "c:/windows/fonts/arialbd.ttf";
    BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font f = new Font(bf, 12);
    document.Add(new Paragraph("Text", f));
    
    Font garamondItalic = FontFactory.GetFont(
      "Garamond", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, Font.ITALIC
    );
    document.Add(new Paragraph("Garamond-Italic", garamondItalic));
    

    Thus, you explicitly enter the encoding and embedding options in the font creation instead of via some font option object.

    BTW, here BaseFont does not refer to the standard 14 fonts which shall be available to the conforming reader according to ISO 32000-1:2008 (I assume that you mean those fonts when you talk about the 16 types) but instead is a base object from which fonts at given sizes are created.