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

2

7

I am currently assessing how to generate a PDF from Xamarin.Forms (currently running the app on Android, only) and checking the .NET Standard port of PdfSharp.

Drawing to the PDF and showing it works, but I'm having issues writing text to the document. When I am trying to load an XFont with the following code

var font = new XFont("sans-serif", 20);

it fails with the exception

System.InvalidOperationException: No appropriate font found.

According to these samples, it is supposed to work this way, but they are for PdfSharp.Xamarin and not the PdfSharp .NET Standard. According to this answer the "sans-serif" font family should be correct, but I've desperately tried other options, like "Roboto", but to no avail.

Is PdfSharp for .NET Standard compatible with Xamarin at all? (It lists PdfSharp.Xamarin as a source it has been created from, hence I assumed it.) Is there anything else I have missed?

EDIT

I tried PdfSharp.Xamarin and it did work. Obviously this is an issue with the .NET Standard port.

Intramural answered 8/2, 2018 at 6:49 Comment(0)
S
8

I had similar issue and i resolved it by writing my own implementation of IFontResolver and assigning it to GlobalFontSettings.FontResolver.

public class FileFontResolver : IFontResolver // FontResolverBase
{
    public string DefaultFontName => throw new NotImplementedException();

    public byte[] GetFont(string faceName)
    {
        using (var ms = new MemoryStream())
        {
            using (var fs = File.Open(faceName, FileMode.Open))
            {
                fs.CopyTo(ms);
                ms.Position = 0;
                return ms.ToArray();
            }
        }
    }

    public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
    {
        if (familyName.Equals("Verdana", StringComparison.CurrentCultureIgnoreCase))
        {
            if (isBold && isItalic)
            {
                return new FontResolverInfo("Fonts/Verdana-BoldItalic.ttf");
            }
            else if (isBold)
            {
                return new FontResolverInfo("Fonts/Verdana-Bold.ttf");
            }
            else if (isItalic)
            {
                return new FontResolverInfo("Fonts/Verdana-Italic.ttf");
            }
            else
            {
                return new FontResolverInfo("Fonts/Verdana-Regular.ttf");
            }
        }
        return null;
    }
}

Then tell PDFSharp to use it:

GlobalFontSettings.FontResolver = new FileFontResolver();
Semicentennial answered 14/6, 2018 at 7:39 Comment(2)
What do you do where the line fails? Something like this? var font = new XFont(FontResolver("sans-serif", 20));Cynth
@Cynth No change needed when using the font. The FontResolver is a global setting and new XFont(...) will invoke it automatically. Just make sure you are using a font covered by your font resolver.Exeat
Q
-1

if you got access denied for the above code. please replace the code with

public byte[] GetFont(string faceName)
    {
        using (var ms = new MemoryStream())
        {
            using (var fs = File.OpenRead(faceName))
            {
                fs.CopyTo(ms);
                ms.Position = 0;
                return ms.ToArray();
            }
        }
    }
Quintile answered 25/5, 2023 at 13:15 Comment(1)
Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Phosphoresce

© 2022 - 2024 — McMap. All rights reserved.