Add PdfSharp font resolver for .NET 8 compatibility

Added a `PdfSharpFontResolver` class to enable font resolution
for PdfSharp in .NET 8, addressing the lack of system font
access. The resolver reads fonts from the Windows Fonts folder
and supports the Arial font family. Registered the resolver
globally in `Program.cs` using `GlobalFontSettings.FontResolver`.

Updated `Program.cs` with comments explaining the necessity of
the resolver for .NET 8. The resolver includes methods to map
font family names to specific font files and load font data.
Throws a `FileNotFoundException` if required fonts are missing.

Made minor formatting changes in `Program.cs` without altering
the `SwaggerDoc` description functionality.
This commit is contained in:
2026-07-01 01:25:24 +02:00
parent 01fc29f59e
commit 49ec9fbead
2 changed files with 51 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
using PdfSharp.Fonts;
namespace EnvelopeGenerator.Server.Services;
/// <summary>
/// PdfSharp 6.x IFontResolver for .NET 8.
/// PdfSharp cannot access system fonts on .NET Core/8 without an explicit resolver.
/// This implementation reads fonts directly from the Windows Fonts folder.
/// Register once at startup: GlobalFontSettings.FontResolver = PdfSharpFontResolver.Instance;
/// </summary>
public class PdfSharpFontResolver : IFontResolver
{
public static readonly PdfSharpFontResolver Instance = new();
private static readonly string FontsFolder =
Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
public FontResolverInfo? ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
var key = familyName.ToLowerInvariant() switch
{
"arial" => isBold ? "arialbd" : "arial",
_ => null
};
return key is null ? null : new FontResolverInfo(key);
}
public byte[] GetFont(string faceName)
{
var fileName = faceName switch
{
"arialbd" => "arialbd.ttf",
_ => "arial.ttf",
};
var path = Path.Combine(FontsFolder, fileName);
if (!File.Exists(path))
throw new FileNotFoundException(
$"Font file not found: {path}. " +
"Ensure Arial is installed on the server.");
return File.ReadAllBytes(path);
}
}