3

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.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57

2 Answers2

5

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();
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
-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();
            }
        }
    }
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit](https://stackoverflow.com/posts/76332709/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](https://stackoverflow.com/help/how-to-answer). – moken May 27 '23 at 09:40