问题
In itextSharp (aka itext5), we can custom font throughout the overriding GetFont method on FontFactoryImp (looks like as code sample below), which means that each time the parser XHTML to PDF engine loop the tag containing font(associate with color, size) will get font correctly.
While in itext7, I can't find out a way to override GetFont(), So I have to include 2 fonts(font normal and bold) as well.
This way make my pdf file increase double size.
That's so terrible.
This is the code that I tried to override a method on itext 7 but it does not affect
public class FontProviderFactory : DefaultFontProvider
{
private static readonly string FontPath = HttpContext.Current.Request.PhysicalApplicationPath + "Areas\\font\\msyh.ttf";
private readonly FontProgram _fontProgram;
public FontProviderFactory(): this(false, false, false)
{
}
public FontProviderFactory(
bool registerStandardPdfFonts,
bool registerShippedFreeFonts,
bool registerSystemFonts): base(registerStandardPdfFonts, registerShippedFreeFonts, registerSystemFonts)
{
_fontProgram = FontProgramFactory.CreateFont(FontPath);
AddFont(_fontProgram);
}
public override FontSelectorStrategy GetStrategy(string text, IList<string> fontFamilies, FontCharacteristics fc, FontSet additonalFonts)
{
//fc.SetBoldFlag(fc.GetFontWeight() == 700);
return new ComplexFontSelectorStrategy(text, GetFontSelector(fontFamilies, fc, additonalFonts), this, additonalFonts);
}
}
Whereas the code that I used on itextSharp and it works fine for me
public class UnicodeFontFactory : FontFactoryImp
{
private static readonly string FontPath = HttpContext.Current.Request.PhysicalApplicationPath + "Areas\\font\\msyh.ttf";
private readonly BaseFont _baseFont;
public UnicodeFontFactory()
{
_baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true);
}
public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
{
return new Font(_baseFont, size, style, color);
}
}
来源:https://stackoverflow.com/questions/58194963/how-can-i-correctly-override-getstrategy-method-on-itext-7-for-net-c-look-al