0

I am using MigraDoc and PdfSharp (.Net Standard), but when I'm trying to create a paragraph(section.AddParagraph("Text")), I get this error :

"No appropriate font found"

For example, when I'm adding a paragraph:

section.AddParagraph("Text"); 

I get this error:

"No appropriate font found"

Probably that's linked with using PdfSharp/MigraDoc .NET Standard port. If you have any solution, I'll be very thankful to know it.

PS: Sorry for my English!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vnf Env
  • 9
  • 3
  • You are not using the "official" versions of MigraDoc and PDFsharp under Windows, you are using an unnamed port on an unnamed operating system. It might help to name port and version to get platform-specific answers and platform-specific sample code. – I liked the old Stack Overflow Sep 30 '19 at 08:02

2 Answers2

0

Check your implementation of IFontResolver and make sure to use only fonts supported by your font resolver.

See also:
https://stackoverflow.com/a/29059207/162529
https://stackoverflow.com/a/36177630/162529

-1

Base on my test, I reproduce the error message, the error is caused by the Font.

enter image description here

Maybe you use Font in your code.

    Paragraph para = sec.AddParagraph("Text");//sec.AddParagraph();
        para.Format.Alignment = ParagraphAlignment.Justify;
        para.Format.Font.Name = "Tinos";
        para.Format.Font.Size = 12;

I add reference with the two .net standard package.

PDFSharp for .NET Standard 2.0 https://github.com/Didstopia/PDFSharp

MigraDoc for .NET Standard https://github.com/Connatix/MigraDoc

I use Font Tinos for example.

Create a FontResolver class first.

public class FontResolver : IFontResolver
{
    public string DefaultFontName => "Tinos";

    public byte[] GetFont(string faceName)
    {
        using (var ms = new MemoryStream())
        {
            var assembly = typeof(FontResolver).GetTypeInfo().Assembly;
            var resources = assembly.GetManifestResourceNames();
            var resourceName = resources.First(x => x == faceName);
            using (var rs = assembly.GetManifestResourceStream(resourceName))
            {
                rs.CopyTo(ms);
                ms.Position = 0;
                return ms.ToArray();
            }
        }
    }

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

You could get the Tinos Fonts file from the link below. https://github.com/Didstopia/PDFSharp/tree/master/Didstopia.PDFSharp.Tests/Fonts

Updataed:

Add the Fonts folder with files like below.

enter image description here

And add the code below to call FontResolver before you use the Font in your code.

   if (GlobalFontSettings.FontResolver == null)
        {
            GlobalFontSettings.FontResolver = new FontResolver();
        }        
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17