Код: Выделить всё
using (PdfDocument document = new PdfDocument())
{
PdfPage page = document.AddPage();
...
XFont fontLabel = new XFont("OpenSans", 10, XFontStyleEx.Regular);
...
}
Код: Выделить всё
public class FileFontResolver : IFontResolver
{
public string DefaultFontName => throw new NotImplementedException();
public byte[] GetFont(string faceName)
{
// Extract embedded font and save to file if it doesn't exist
string fontPath = Path.Combine(FileSystem.AppDataDirectory, faceName);
if (!File.Exists(fontPath))
{
// Use embedded fonts localed at Resources/Fonts
using Stream fontStream = FileSystem.OpenAppPackageFileAsync(faceName).Result;
using FileStream outputStream = File.Create(fontPath);
fontStream.CopyTo(outputStream);
}
// Provide font byte array to XFont
using (var ms = new MemoryStream())
{
using (var fs = File.Open(fontPath, FileMode.Open))
{
fs.CopyTo(ms);
ms.Position = 0;
return ms.ToArray();
}
}
}
public FontResolverInfo? ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if (familyName.Equals("OpenSans", StringComparison.CurrentCultureIgnoreCase))
{
if (isBold)
{
return new FontResolverInfo("OpenSans-Semibold.ttf");
}
else
{
return new FontResolverInfo("OpenSans-Regular.ttf");
}
}
return null;
}
}
Код: Выделить всё
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
...
GlobalFontSettings.FontResolver = new FileFontResolver();
}
}
Код: Выделить всё
Exception android.runtime.JavaProxyThrowable:
at Microsoft.Maui.Storage.FileSystemImplementation.PlatformOpenAppPackageFile + 0x37 (Unknown Source)
at Microsoft.Maui.Storage.FileSystemImplementation.PlatformOpenAppPackageFileAsync + 0x0 (Unknown Source)
at Microsoft.Maui.Storage.FileSystemImplementation.OpenAppPackageFileAsync + 0x0 (Unknown Source)
at Microsoft.Maui.Storage.FileSystem.OpenAppPackageFileAsync + 0x5 (Unknown Source)
at VetExpertDiagnostics.SmartReader.PdfSharpClasses.FileFontResolver.GetFont + 0x14 (Unknown Source)
at PdfSharp.Fonts.FontFactory.ResolveTypeface + 0xde (Unknown Source)
at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom + 0x1c (Unknown Source)
at PdfSharp.Drawing.XFont.Initialize + 0x49 (Unknown Source)
at PdfSharp.Drawing.XFont..ctor + 0x2e (Unknown Source)
at PdfSharp.Drawing.XFont..ctor + 0xe (Unknown Source)
at VetExpertDiagnostics.SmartReader.ViewModelClasses.TestViewModel+d__49.MoveNext + 0xc2 (Unknown Source)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw + 0x11 (Unknown Source)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess + 0x3e (Unknown Source)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification + 0x2d (Unknown Source)
Код: Выделить всё
using Stream fontStream = FileSystem.OpenAppPackageFileAsync(faceName).Result
Я действительно не знаю, как решить эту проблему, и у меня закончились идеи.
Есть ли у кого-нибудь рабочее решение или мне следует начать искать альтернативы PdfSharp?>
Подробнее здесь: https://stackoverflow.com/questions/798 ... oid-to-cra
Мобильная версия