xamarinxamarin.formsxamarin.androidpdfsharpmigradoc

No appropriate font found using MigraDoc to create a PDF on Xamarin Forms


I'm trying to create a pdf using MigraDoc. Here's a list of the libraries that I'm using:

It throws me an error on printer.RenderDocument(). Code below

private async Task SavePDF()
        {
            filePath = emulatorFolderPath + "/Signed/" + _reportInformationViewModel.SelectedClient.Username + "-" + DateTime.Now.ToString("dd_MM_yyyy HH-mm") + ".pdf";
            MigraDocRendering.PdfDocumentRenderer printer = new MigraDocRendering.PdfDocumentRenderer
            {
                Document = document
            };
            printer.RenderDocument();
            printer.PdfDocument.Save(filePath);
        }

PS: I don't need to use a private font.


Solution

  • I've resolved by implementing IFontResolver I've added a folder Fonts that contains Open-Sans font. I've created a folder Helpers that contains a class called GenericFontResolver:

    public class GenericFontResolver : IFontResolver
        {
            public string DefaultFontName => "OpenSans";
    
            public byte[] GetFont(string faceName)
            {
                if (faceName.Contains(DefaultFontName))
                {
                    var assembly = typeof(ReportPreviewAndSignatureViewModel).GetTypeInfo().Assembly;
                    var stream = assembly.GetManifestResourceStream($"PDFDemo.Fonts.{faceName}.ttf");
    
                    using (var reader = new StreamReader(stream))
                    {
                        var bytes = default(byte[]);
    
                        using (var ms = new MemoryStream())
                        {
                            reader.BaseStream.CopyTo(ms);
                            bytes = ms.ToArray();
                        }
    
                        return bytes;
                    }
                }
                else
                    return GetFont(DefaultFontName);
            }
    
            public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
            {
                var fontName = string.Empty;
    
                switch (familyName)
                {
                    case "Open Sans":
                    case "OpenSans":
                        fontName = "OpenSans";
    
                        if (isBold && isItalic)
                            fontName = $"{fontName}-BoldItalic";
                        else if (isBold)
                            fontName = $"{fontName}-Bold";
                        else if (isItalic)
                            fontName = $"{fontName}-Italic";
                        else
                            fontName = $"{fontName}-Regular";
    
                        return new FontResolverInfo(fontName);
                    default:
                        break;
                }
    
                return null;
            }
        }
    

    Then, on the constructor of the class that needs the font I've added:

    GlobalFontSettings.FontResolver = new GenericFontResolver();

    Then, when I'm creating the table you must add:

    Style style = document.Styles["Normal"];
    style.Font.Name = "OpenSans";