PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Error with setting UseWindowsFontsUnderWindows=true
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4883
Page 1 of 1

Author:  mce [ Thu Sep 25, 2025 9:56 am ]
Post subject:  Error with setting UseWindowsFontsUnderWindows=true

Hello,

Unfortunately I have ran into this exception with PDFSharp:

"Object synchronization method was called from an unsynchronized block of code"

And from the stacktrace:
    LineNumber: 220
    ColumnNumber: 51
    MethodName: set_UseWindowsFontsUnderWindows
    FileName: D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Fonts\GlobalFontSettings.cs
    NativeOffset: 225
    ILOffset: 106

I am using PDFSharp version 6.2.0 - but before I have used version 6.2.1. I have seen this exception with both versions.

Any suggestions to avoid this error?

Regards
Michael

Author:  TH-Soft [ Mon Sep 29, 2025 8:25 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

Hi!
mce wrote:
Any suggestions to avoid this error?
I think the issue is not with the code you are calling, but with the spot where you call it.
You do not share where you call it. Is it somewhere in the App object maybe?

Author:  mce [ Mon Sep 29, 2025 8:44 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

Hello TH-soft,

Thank you for reading and reply.

I have a routine which I call before I create an instance of PdfDocument. And this call is place just before "PdfDocument document = new PdfDocument();" like this:

Code:
Global.SetPDFSharpFonts();
PdfDocument document = new PdfDocument();

And the code in the routine SetPDFSharpFonts is:
Code:
public static void SetPDFSharpFonts()
{
    if (HttpContext.Current.Session["PDFSharpFonts"] == null)
    {
        GlobalFontSettings.UseWindowsFontsUnderWindows = true;
        HttpContext.Current.Session["PDFSharpFonts"] = "1";
    }
}

I hope that this is useful.

Thanks in advance.

Regards Michael

Author:  TH-Soft [ Mon Sep 29, 2025 1:04 pm ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

mce wrote:
I hope that this is useful.
Not really.
I understand now that your code runs on a web server.
Instead of using "UseWindowsFontsUnderWindows=true", you can create a FontResolver with the fonts you need.

"UseWindowsFontsUnderWindows=true" is a hack for local testing. It is not meant for production use.

Author:  mce [ Thu Oct 02, 2025 10:51 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

Hello TH-soft,

Thank you very much for this information...

But now I have tried several times with FontResolver in my .NET framework project version 4.7.2 - but unfortunately I can't get it to work :-(

I have based my trials from the file SubstitutingFontResolver.cs from the path "...\PDFsharp.Samples-master\src\samples\src\PDFsharp\src\Pdf.Fonts\PlatformAwareFontResolver". and tried to make a 4.7.2 version instead - but without success :-(

So my question is: Is there a sample for .NET 4.7.2 ?

Regards Michael

Author:  TH-Soft [ Sun Oct 05, 2025 11:08 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

mce wrote:
So my question is: Is there a sample for .NET 4.7.2?

The code that works for .NET 6 or 8 will also work for .NET Framework 4.x or .NETstandard 2.0.
Code that works on a local Windows machine may, however, fail on an Azure Webserver.
Do not assume that you can access the c:\Windows\Fonts folder or that there are specific fonts in it.

Author:  mce [ Sun Oct 05, 2025 7:13 pm ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

Hello again,

Thank you very much for this help... :-)

But unfortunately I got the attached error.

My code is like this:
Code:
static SubstitutingFontResolver()
{
    var fcp = Environment.GetEnvironmentVariable("FONTCONFIG_PATH");
    if ((!(fcp is null)) && !LinuxFontLocations.Contains(fcp))
        LinuxFontLocations.Add(fcp);
}

And it is called like this:
Code:
GlobalFontSettings.FontResolver = new SubstitutingFontResolver().SubstitutingFontResolver;


So how to overcome this error?

Regards Michael

Attachments:
Error.jpg
Error.jpg [ 61.79 KiB | Viewed 11622 times ]

Author:  TH-Soft [ Mon Oct 06, 2025 7:59 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

mce wrote:
So how to overcome this error?
I only see code snippets. What I see looks like a static constructor.
And the other code looks like invoking a dynamic constructor and then getting a property. I do not see any dynamic constructors or any properties in your code.

Maybe look at some sample implementations of FontResolvers to learn how it can be done.
viewtopic.php?f=8&t=3244
viewtopic.php?f=8&t=3976

Author:  mce [ Mon Oct 06, 2025 9:45 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

Hello again,

Unfortunately I have to ask further :-(

I have seen and checked the links...
I have tried to use the files "PDFSharpFonts.cs" and "SubstitutingFontResolver.cs" as basefiles - and among different lines modified the line "static readonly List<TypefaceInfo> TypefaceInfos =" so it handles "new List<TypefaceInfo>() { }" with the fonts in my copy of the file "SubstitutingFontResolver.cs"

Now it seems that I "maybe only" have one error left. The error is in the routine GetFontBytes where I can see the following line:
Code:
using (Graphics gTemp = SafeGraphics.CreateReferenceGraphics())
{
}

In the attached file I have taken a screenshot of the error that occurs. It is "SafeGraphics" which does not exists.

So how to overcome this error?

Thanks in advance.

Regards Michael

Attachments:
error.jpg
error.jpg [ 15.21 KiB | Viewed 11372 times ]

Author:  TH-Soft [ Fri Oct 10, 2025 9:39 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

mce wrote:
It is "SafeGraphics" which does not exists.
Please note that this sample code is for GDI. Using it with Core or WPF could be difficult.
The code for "SafeGraphics.CreateReferenceGraphics()" is missing and you must obtain a Graphics object (not a PDFsharp XGraphics object) the regular way.

I wrote EZFontResolver (EZ stands for Easy). If you have questions about that one, I'll try to answer those.

Author:  mce [ Fri Oct 10, 2025 10:05 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

Hello again,

Thank you for writing about EZFontResolver...

Unfortunately I can't access the link http://developer.th-soft.com/developer/ezfontresolver-a-generic-font-resolver-for-pdfsharp-and-migradoc/ Because I can see that there is a critical error on the website :( I have attached a screendump of the text which I see.

Can you assist?

Regards Michael

Attachments:
Critical Error.jpg
Critical Error.jpg [ 24.84 KiB | Viewed 9610 times ]

Author:  TH-Soft [ Fri Oct 10, 2025 11:53 am ]
Post subject:  Re: Error with setting UseWindowsFontsUnderWindows=true

A link for direct downloads:
http://th-soft.com/pdfsharp/downloads/E ... solver.zip

http://th-soft.com/pdfsharp/downloads/F ... erDemo.zip

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/