1

When i debugging my app I got Exception when creating object of XFont. Any idea what is wrong? In all examples I found on github etc. it was exactly like in this code below.

System.TypeInitializationException: 'The type initializer for 'PdfSharpCore.Utils.FontResolver' threw an exception.'

document.Info.Title = "created with pdfsharp";

// Create an empty page
PdfPage page = document.AddPage();

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);

// Draw the text
gfx.DrawString("Test of PdfSharp on Android", font, 
                new XSolidBrush(XColor.FromArgb(0, 0, 0)), 
                10, 130);
// Save the document...
document.Save(Path.Combine(Path.GetTempPath(), Name));

1 Answers1

1

According to your code, I guess that you use PdfSharp.Xamarin.Forms library, now you want to use custom fonts for pdf, am I right?

if yes, please take a look the following code, implementing IFontResolver.

internal class FontProvider : IFontResolver
{
    #region Properties
    public string DefaultFontName
    {
        get { return "OpenSans"; }
    }
    #endregion

    #region Fields
    public ICustomFontProvider _fontProvider;
    public static readonly string[] DefaultFontFiles = new string[]
        {
            "OpenSans-Regular.ttf",
            "OpenSans-Bold.ttf",
            "OpenSans-Italic.ttf",
            "OpenSans-BoldItalic.ttf",
        };
    #endregion

    #region Ctor
    public FontProvider(ICustomFontProvider fontProvider)
    {
        _fontProvider = fontProvider;
    }
    #endregion

    #region IFontResolver implementation
    public byte[] GetFont(string faceName)
    {
        if (DefaultFontFiles.Contains(faceName) || _fontProvider == null)
        {
            var assembly = typeof(FontProvider).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream($"PdfSharp.Xamarin.Forms.DefaultFonts.{faceName}");
            using (var reader = new StreamReader(stream))
            {
                var bytes = default(byte[]);
                using (var memstream = new MemoryStream())
                {
                    reader.BaseStream.CopyTo(memstream);
                    bytes = memstream.ToArray();
                }
                return bytes;
            }
        }
        else
        {
            return _fontProvider.GetFont(faceName);
        }
    }

    public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
    {
        string fontName;
        if (familyName == DefaultFontName || _fontProvider == null)
            fontName = DefaultFontFiles[Convert.ToInt32(isBold) + 2 * Convert.ToInt32(isItalic)];
        else
            fontName = _fontProvider.ProvideFont(familyName, isBold, isItalic);

        return new FontResolverInfo(fontName);
    }
    #endregion
}

You can also write my own implementation of IFontResolver and assigning it to GlobalFontSettings.FontResolver.

Please take a look the following thread:

Loading a Font with PdfSharp .Net Standard preview from Xamarin.Forms fails: No appropriate font found

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16
  • What is all this? I just want to create a font. I'm using straight C#/.NET in a console app. No Xamarin or anything else like that. Why does doing their "Hello World" need to be this complicated? – Jonathan Wood Jan 21 '22 at 00:52