Question

I am using XMLWorker for parsing html. I have been having some issues with the fonts I define in the styles. For example, something simple as this:

<span style="font-family: Garamond">Foo Garamond</span>

wasn't not working.

I was using this as my css applier:

CssAppliers ca = new CssAppliersImpl();

In order to check if it was a problem with the encoding of the html, or any other issue..., I did my own implementation of the IFontProvider:

class MyFontProvider : IFontProvider
    {
        public bool IsRegistered(string fontname)
        {
            return false;
        }

        public Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
        {
            var font = FontFactory.GetFont(fontname, encoding, embedded, size, style, color);
            return new Font(font);
        }
    }

Then, this:

CssAppliers ca = new CssAppliersImpl(new MyFontProvider());

Great!!!, that works fine!!!, also if I passed to the constructor this:

CssAppliers ca = new CssAppliersImpl(new XMLWorkerFontProvider());

also works.

So, it is obvious that the default implementation of the font provider is not working. I defined it as this:

CssAppliers ca = new CssAppliersImpl();

or 

CssAppliers ca = new CssAppliersImpl(new FontFactoryImp());

, and neither worked.

My questions are:

  1. What possible explanation has this?
  2. Differences between XMLWorkerFontProvider and FontFactoryImp implementations
Was it helpful?

Solution

FontFactoryImp is part of iText/iTextSharp core. It provides access to the required core 14 fonts and allows you to optionally register additional font files and give them nice names. But unless you manually register a font file it will just return the "default" font.

XMLWorkerFontProvider is part of XMLWorker and is actually a subclass of FontFactoryImp. The biggest difference between the two is that if you use the empty constructor on XMLWorkerFontProvider it actually calls base.RegisterDirectories() which registeres every font in the system font folder. Depending on the number of fonts you have this could be expensive which is why the default version doesn't do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top