Hello,
I'm hoping that someone can help me with an issue I'm having drawing text including unicode characters with values over 0x10000. I am using a font that includes these characters, but they are rendering with two question mark boxes, which leads me to believe that PdfSharp may be treating these values as two separate characters. Is there some other configuration option I am missing to get these characters to work correctly?
I've included some code below that I've written in a standalone C# console app that shows the problem. I am using version 1.50.5147 of the PdfSharp NuGet package.
Code:
class Program
{
static void Main(string[] args)
{
GlobalFontSettings.FontResolver = new FontResolver();
var pdf = new PdfDocument();
var page = pdf.AddPage();
page.Orientation = PdfSharp.PageOrientation.Portrait;
page.Size = PdfSharp.PageSize.Letter;
using (var pdfDraw = XGraphics.FromPdfPage(page))
{
// I tried using this in the XFont c-tor and it didn't make any difference
// var pdfFontOptions = new XPdfFontOptions(PdfFontEncoding.Unicode);
var font = new XFont("Symbola", 12, XFontStyle.Regular);
// Rendering this character does not work - I see two [?][?] boxes in the PDF.
pdfDraw.DrawString(System.Net.WebUtility.HtmlDecode(" 🖳 "), font, XBrushes.Black, new XPoint(100, 200), StringFormats.BaseLineLeft);
pdfDraw.DrawString("\U0001F5B3", font, XBrushes.Black, new XPoint(100, 300), XStringFormats.BaseLineLeft);
// Rendering this character (< 0x10000) works as expected.
pdfDraw.DrawString(System.Net.WebUtility.HtmlDecode(" ►"), font, XBrushes.Black, new XPoint(200, 200), XStringFormats.BaseLineLeft);
pdfDraw.DrawString("\U000025BA", font, XBrushes.Black, new XPoint(200, 300), XStringFormats.BaseLineLeft);
pdf.Save("C:\\temp\\result.pdf");
}
}
public class FontResolver : IFontResolver
{
public byte[] GetFont(string faceName)
{
if(faceName == "Symbola")
{
return System.IO.File.ReadAllBytes("C:\\temp\\Symbola.ttf");
}
return null;
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if(familyName == "Symbola")
{
return new FontResolverInfo("Symbola");
}
return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
}
}
}