我试图使用PdfSharpCore和MigraDocCore在.net maui中用vspre2022创建一个pdf文件。
这是我的密码
using System;
using System.Diagnostics;
using System.Windows.Input;
using MigraDocCore.DocumentObjectModel;
using MigraDocCore.Rendering;
using PdfKit;
using PdfSharpCore;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
private void CreatePdf()
{ System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
PdfSharpCore.Pdf.PdfDocument document = new PdfSharpCore.Pdf.PdfDocument();
PdfSharpCore.Pdf.PdfPage page = document.AddPage();
PdfSharpCore.Drawing.XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
var ren = new PdfDocumentRenderer(true);
XFont font = new XFont("OpenSans-Semibold", 20, XFontStyle.Bold);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height));
}
但是我在XFont上有个错误
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
发布于 2022-09-08 19:24:31
您需要创建文件FileFontResolver。
public class FileFontResolver: IFontResolver
{
public FileFontResolver()
{
}
public string DefaultFontName => throw new NotImplementedException();
public byte[] GetFont(string faceName)
{
var assembly = this.GetType().GetTypeInfo().Assembly;
var directory = $"MyApp.Resources.Fonts.{faceName}.ttf";
var stream = assembly.GetManifestResourceStream(directory);
using (var reader = new StreamReader(stream))
{
var bytes = default(byte[]);
using (var ms = new MemoryStream())
{
reader.BaseStream.CopyTo(ms);
bytes = ms.ToArray();
}
return bytes;
}
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
return new FontResolverInfo(familyName);
}
}.
然后,我在app.xaml.cs中调用这个文件
public partial class App : Application
{
public App()
{
InitializeComponent();
GlobalFontSettings.FontResolver = new FileFontResolver();
MainPage = new AppShell();
}
}
https://stackoverflow.com/questions/72555031
复制相似问题