PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Mon Oct 20, 2025 1:25 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Sep 25, 2025 9:56 am 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 11
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 29, 2025 8:25 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1062
Location: CCAA
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?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 29, 2025 8:44 am 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 11
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 29, 2025 1:04 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1062
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 02, 2025 10:51 am 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 11
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Oct 05, 2025 11:08 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1062
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sun Oct 05, 2025 7:13 pm 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 11
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 11749 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 06, 2025 7:59 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1062
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 06, 2025 9:45 am 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 11
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 11499 times ]
Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 10, 2025 9:39 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1062
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 10, 2025 10:05 am 
Offline

Joined: Tue Nov 01, 2022 3:34 pm
Posts: 11
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 9737 times ]
Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 10, 2025 11:53 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1062
Location: CCAA
A link for direct downloads:
http://th-soft.com/pdfsharp/downloads/E ... solver.zip

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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 116 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group