empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.28k stars 589 forks source link

Font must use GraphicsUnit.World #169

Closed chris-steema closed 1 year ago

chris-steema commented 2 years ago

Expected Behavior

I've modified the PdfSharpIssueSurrogate on a fork on my account here. This uses the Jiyu No Tsubasa font, which is a TrueType font (JiyunoTsubasa.ttf). As per the PDFsharp documentation here, I would expect this font to be used correctly.

Actual Behavior

The code throws the following error:

System.ArgumentException
  HResult=0x80070057
  Message=Font must use GraphicsUnit.World.
  Source=PdfSharp
  StackTrace:
   at PdfSharp.Drawing.XFont..ctor(Font font, XPdfFontOptions pdfOptions)
   at PdfSharpIssueSurrogate.Program.Main(String[] args) in C:\Users\Chris\source\repos\PdfSharpIssueSurrogate\PdfSharpIssueSurrogate\Program.cs:line 25

And this error is thrown whichever values I choose for PdfFontEncoding and PdfFontEmbedding.

Steps to Reproduce the Behavior

Please use the code above as specified.

ThomasHoevel commented 1 year ago

The font can be loaded using the IFontResolver interface. Tested with EZFontResolver. http://developer.th-soft.com/developer/2015/12/11/ezfontresolver-a-generic-font-resolver-for-pdfsharp-and-migradoc/

ThomasHoevel commented 1 year ago

In your code you are using these two lines where the error occurs: var font0 = new Font(privateFonts.Families[0], 20, FontStyle.Bold); XFont font = new XFont(font0, new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Default));

The error does not occur when you skip the Font object and create the XFont object directly: XFont font = new XFont((FontFamily)privateFonts.Families[0], 20, XFontStyle.Bold);

The XFontOptions can also be specified here: XFont font = new XFont((FontFamily)privateFonts.Families[0], 20, XFontStyle.Bold, new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Default));

ThomasHoevel commented 1 year ago

This is by design: Creating an XFont from Font works only when the font uses world co-ordinates.
Creating the XFont directly from the font name is an easy solution for installed fonts.
Implementing IFontResolver is another option.