使用 MigraDoc 在 Xamarin Forms 上创建 PDF 时找不到合适的字体

发布于 2025-01-09 15:24:47 字数 748 浏览 0 评论 0原文

我正在尝试使用 MigraDoc 创建 pdf。以下是我正在使用的库的列表:

  • MigraDoc.DocumentObjectModel
  • MigraDoc.DocumentObjectModel.Tables
  • MigraDoc.Rendering

它在 printer.RenderDocument() 上引发错误。下面的代码

private async Task SavePDF()
        {
            filePath = emulatorFolderPath + "/Signed/" + _reportInformationViewModel.SelectedClient.Username + "-" + DateTime.Now.ToString("dd_MM_yyyy HH-mm") + ".pdf";
            MigraDocRendering.PdfDocumentRenderer printer = new MigraDocRendering.PdfDocumentRenderer
            {
                Document = document
            };
            printer.RenderDocument();
            printer.PdfDocument.Save(filePath);
        }

PS:我不需要使用私有字体。

I'm trying to create a pdf using MigraDoc. Here's a list of the libraries that I'm using:

  • MigraDoc.DocumentObjectModel
  • MigraDoc.DocumentObjectModel.Tables
  • MigraDoc.Rendering

It throws me an error on printer.RenderDocument(). Code below

private async Task SavePDF()
        {
            filePath = emulatorFolderPath + "/Signed/" + _reportInformationViewModel.SelectedClient.Username + "-" + DateTime.Now.ToString("dd_MM_yyyy HH-mm") + ".pdf";
            MigraDocRendering.PdfDocumentRenderer printer = new MigraDocRendering.PdfDocumentRenderer
            {
                Document = document
            };
            printer.RenderDocument();
            printer.PdfDocument.Save(filePath);
        }

PS: I don't need to use a private font.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2025-01-16 15:24:47

我已经通过实施 IFontResolver 解决了
我添加了一个包含 Open-Sans 字体的文件夹 Fonts
我创建了一个文件夹 Helpers,其中包含一个名为 GenericFontResolver 的类:

public class GenericFontResolver : IFontResolver
    {
        public string DefaultFontName => "OpenSans";

        public byte[] GetFont(string faceName)
        {
            if (faceName.Contains(DefaultFontName))
            {
                var assembly = typeof(ReportPreviewAndSignatureViewModel).GetTypeInfo().Assembly;
                var stream = assembly.GetManifestResourceStream($"PDFDemo.Fonts.{faceName}.ttf");

                using (var reader = new StreamReader(stream))
                {
                    var bytes = default(byte[]);

                    using (var ms = new MemoryStream())
                    {
                        reader.BaseStream.CopyTo(ms);
                        bytes = ms.ToArray();
                    }

                    return bytes;
                }
            }
            else
                return GetFont(DefaultFontName);
        }

        public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
        {
            var fontName = string.Empty;

            switch (familyName)
            {
                case "Open Sans":
                case "OpenSans":
                    fontName = "OpenSans";

                    if (isBold && isItalic)
                        fontName = $"{fontName}-BoldItalic";
                    else if (isBold)
                        fontName = $"{fontName}-Bold";
                    else if (isItalic)
                        fontName = $"{fontName}-Italic";
                    else
                        fontName = $"{fontName}-Regular";

                    return new FontResolverInfo(fontName);
                default:
                    break;
            }

            return null;
        }
    }

然后,在需要我添加的字体的类的构造函数上:

GlobalFontSettings。 FontResolver = new GenericFontResolver();

然后,当我创建表时,您必须添加:

Style style = document.Styles["Normal"];
style.Font.Name = "OpenSans";

I've resolved by implementing IFontResolver
I've added a folder Fonts that contains Open-Sans font.
I've created a folder Helpers that contains a class called GenericFontResolver:

public class GenericFontResolver : IFontResolver
    {
        public string DefaultFontName => "OpenSans";

        public byte[] GetFont(string faceName)
        {
            if (faceName.Contains(DefaultFontName))
            {
                var assembly = typeof(ReportPreviewAndSignatureViewModel).GetTypeInfo().Assembly;
                var stream = assembly.GetManifestResourceStream(
quot;PDFDemo.Fonts.{faceName}.ttf");

                using (var reader = new StreamReader(stream))
                {
                    var bytes = default(byte[]);

                    using (var ms = new MemoryStream())
                    {
                        reader.BaseStream.CopyTo(ms);
                        bytes = ms.ToArray();
                    }

                    return bytes;
                }
            }
            else
                return GetFont(DefaultFontName);
        }

        public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
        {
            var fontName = string.Empty;

            switch (familyName)
            {
                case "Open Sans":
                case "OpenSans":
                    fontName = "OpenSans";

                    if (isBold && isItalic)
                        fontName = 
quot;{fontName}-BoldItalic";
                    else if (isBold)
                        fontName = 
quot;{fontName}-Bold";
                    else if (isItalic)
                        fontName = 
quot;{fontName}-Italic";
                    else
                        fontName = 
quot;{fontName}-Regular";

                    return new FontResolverInfo(fontName);
                default:
                    break;
            }

            return null;
        }
    }

Then, on the constructor of the class that needs the font I've added:

GlobalFontSettings.FontResolver = new GenericFontResolver();

Then, when I'm creating the table you must add:

Style style = document.Styles["Normal"];
style.Font.Name = "OpenSans";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文