Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MigraDoc PDF呈现程序遇到System.NullReferenceException_C#_Azure_Pdfsharp_Migradoc - Fatal编程技术网

C# MigraDoc PDF呈现程序遇到System.NullReferenceException

C# MigraDoc PDF呈现程序遇到System.NullReferenceException,c#,azure,pdfsharp,migradoc,C#,Azure,Pdfsharp,Migradoc,我在Azure上使用MigraDoc和PDF渲染器,它引发了System.NullReferenceException。这是一个例外: System.NullReferenceException: Object reference not set to an instance of an object. at PdfSharp.Fonts.OpenType.OpenTypeFontface.CetOrCreateFrom(XFontSource fontSource) at PdfSharp.D

我在Azure上使用MigraDoc和PDF渲染器,它引发了System.NullReferenceException。这是一个例外:

System.NullReferenceException: Object reference not set to an instance of an object.
at PdfSharp.Fonts.OpenType.OpenTypeFontface.CetOrCreateFrom(XFontSource fontSource)
at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
at PdfSharp.Drawing.XFont.Initialize()
at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()
编辑:我已经实现了一个MyFontResolver类,如下所示,我正在使用GlobalFontSettings.FontResolver=new MyFontResolver();在我的代码中,将字体解析器设置为类

public class MyFontResolver : IFontResolver
    {
        public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
        {
            // Ignore case of font names.
            var name = familyName.ToLower();

            // Deal with the fonts we know.
            switch (name)
            {
                case "ubuntu":
                    if (isBold)
                    {
                        if (isItalic)
                            return new FontResolverInfo("Ubuntu#bi");
                        return new FontResolverInfo("Ubuntu#b");
                    }
                    if (isItalic)
                        return new FontResolverInfo("Ubuntu#i");
                    return new FontResolverInfo("Ubuntu#");

                case "janitor":
                    return new FontResolverInfo("Janitor#");
            }

            // We pass all other font requests to the default handler.
            // When running on a web server without sufficient permission, you can return a default font at this stage.
            return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
        }

        private byte[] LoadFontData(string name)
        {
            var assembly = Assembly.GetExecutingAssembly();

            // Test code to find the names of embedded fonts - put a watch on "ourResources"
            //var ourResources = assembly.GetManifestResourceNames();

            using (Stream stream = assembly.GetManifestResourceStream(name))
            {
                if (stream == null)
                    throw new ArgumentException("No resource with name " + name);

                int count = (int)stream.Length;
                byte[] data = new byte[count];
                stream.Read(data, 0, count);
                return data;
            }
        }

        public byte[] GetFont(string faceName)
        {
            switch (faceName)
            {
                case "Arial#":
                    return LoadFontData("MyProject.fonts.arial.arial.ttf"); ;

                case "Arial#b":
                    return LoadFontData("MyProject.fonts.arial.arialbd.ttf"); ;

                case "Arial#i":
                    return LoadFontData("MyProject.fonts.arial.ariali.ttf");

                case "Arial#bi":
                    return LoadFontData("MyProject.fonts.arial.arialbi.ttf");
            }

            return null;
        }
    }
我现在得到一个单独的错误,我认为我的字体解析器应该修复

System.InvalidOperationException: Microsoft Azure returns STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L) from GetFontData. This is a bug in Azure. You must implement a FontResolver to circumvent this issue.
at PdfSharp.Drawing.XFontSource.ReadFontBytesFromGdi(Font gdiFont)
at PdfSharp.Fonts.PlatformFontResolver.CreateFontSource(String familyName, FontResolvingOptions fontResolvingOptions, Font& font, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, Boolean isBold, Boolean isItalic)
at EmailQueue.AddCopyToEFolder.MyFontResolver.ResolveTypeface(String familyName, Boolean isBold, Boolean isItalic)
at PdfSharp.Fonts.FontFactory.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
at PdfSharp.Drawing.XFont.Initialize()
at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()
System.invalidoOperationException:Microsoft Azure从GetFontData返回状态\u访问被拒绝((NTSTATUS)0xC000002L)。这是Azure中的一个bug。必须实现FontResolver以避免此问题。
在PdfSharp.Drawing.XFontSource.ReadFontBytesFromGdi(字体gdiFont)处
在PdfSharp.Fonts.PlatformFontResolver.CreateFontSource(字符串familyName、FontResolvingOptions、Font&Font、字符串typefaceKey)
在PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(字符串familyName、FontResolvingOptions、FontResolvingOptions、字符串Typeface键)
在PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(字符串familyName、布尔isBold、布尔ISTALIC)
在EmailQueue.AddCopyToEFolder.MyFontResolver.ResolveTypeface(字符串familyName、布尔isBold、布尔ISTALIC)
在PdfSharp.Fonts.FontFactory.ResolveTypeface(字符串familyName、FontResolvingOptions、FontResolvingOptions、字符串Typeface键)
在PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(字符串familyName,FontResolvingOptions FontResolvingOptions)
在PdfSharp.Drawing.XFont.Initialize()处
在MigraDoc.Rendering.FontHandler.FontToXFont(字体字体,PdfFontEncoding编码)
在migraphdoc.Rendering.ParagraphRenderer.get_CurrentFont()处
在migraphdoc.Rendering.ParagraphRenderer.InitFormat(区域区域,FormatInfo-previousFormatInfo)
在migraphdoc.Rendering.ParagraphRenderer.Format(区域区域,FormatInfo-previousFormatInfo)
在migraphics.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx,布尔顶级)
在migraphics.Rendering.FormattedDocument.Format(XGraphics gfx)中
在MigraDoc.Rendering.DocumentRenderer.PreparedDocument()处
在MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer处(布尔值prepareComplely)
在MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()处
在MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()处
在EmailQueue.AddCopyToEFolder.d_u3.MoveNext()中

感谢您提供的任何帮助

对于您在文档中使用的字体,您似乎必须使用
IFontResolver
界面

很可能您的应用程序没有访问Azure服务器上安装的TTF文件的权限,或者未安装所需的TTF

另请参见:


编辑:回答编辑后的问题:字体解析器将所有未处理的请求传递给
PlatformFontResolver.ResolveTypeface(familyName、isBold、isItalic)
显然在Azure上不起作用。因此,请将此调用替换为异常,并确保您的代码在本地计算机上运行而不会引发该异常-然后它也应该在Azure上运行。

我相信我已经添加了iOntresolver接口。是否有关于如何检查ttf文件权限的文档?在你提供的链接似乎没有列出它们之前,我从未将它们添加到azure。我刚刚将它们添加到项目中,但我不确定它们是否在azure服务器上。正确使用
IFontResolver
时,不会访问任何TTF文件。无法从代码片段中看出这一点。但最有可能的是
如果ontResolver
未正确使用,因此您会遇到系统异常。请注意注释“/”在web服务器上运行时,如果没有足够的权限,您可以在此阶段返回默认字体。“在上面显示的代码中……您在PDFsharp论坛上写道,您解决了此问题,但你没有分享你是如何解决的。分享解决方案可能会帮助在此处发现此问题的其他人。
System.InvalidOperationException: Microsoft Azure returns STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L) from GetFontData. This is a bug in Azure. You must implement a FontResolver to circumvent this issue.
at PdfSharp.Drawing.XFontSource.ReadFontBytesFromGdi(Font gdiFont)
at PdfSharp.Fonts.PlatformFontResolver.CreateFontSource(String familyName, FontResolvingOptions fontResolvingOptions, Font& font, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, Boolean isBold, Boolean isItalic)
at EmailQueue.AddCopyToEFolder.MyFontResolver.ResolveTypeface(String familyName, Boolean isBold, Boolean isItalic)
at PdfSharp.Fonts.FontFactory.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
at PdfSharp.Drawing.XFont.Initialize()
at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()