Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 序列化FixedDocument时出现FileFormatException_C#_Xpsdocument - Fatal编程技术网

C# 序列化FixedDocument时出现FileFormatException

C# 序列化FixedDocument时出现FileFormatException,c#,xpsdocument,C#,Xpsdocument,将固定文档序列化到XPS时,有时会出现FileFormatException异常,告诉我字体格式(我假设)不符合预期的文件格式规范(请参阅下面的异常) 神秘的部分是: 这种异常只会偶尔发生一次 这只会发生在某些FontFamily/Style/Weight组合中(斜体和粗体的Segoe用户界面似乎触发了这种情况) 有人知道为什么会发生这种情况(尤其是为什么它不会持续发生,但只是在不可预测的时间间隔内发生) 下面的最小可复制示例将在每次运行时触发异常4到5次(在我的Windows 10计算机上

将固定文档序列化到XPS时,有时会出现
FileFormatException
异常,告诉我字体格式(我假设)不符合预期的文件格式规范(请参阅下面的异常)

神秘的部分是:

  • 这种异常只会偶尔发生一次
  • 这只会发生在某些FontFamily/Style/Weight组合中(斜体和粗体的Segoe用户界面似乎触发了这种情况)
有人知道为什么会发生这种情况(尤其是为什么它不会持续发生,但只是在不可预测的时间间隔内发生)

下面的最小可复制示例将在每次运行时触发异常4到5次(在我的Windows 10计算机上,在.NET 4、4.6.1等中发生):

第一行可以转换为:

源代码中的注释似乎表明这调用了本机代码。字体驱动程序需要访问声明的字体。并非所有字体都支持所有变体。您应该确认您选择的字体系列已安装并支持斜体

如果api支持该选项,则应尝试指示已安装的特定字体(而不是系列),并支持所选的变体


祝你好运

您是否已将xaml中的区域设置设置为德语?i、 e.
xml:lang=“de de”
@XAMlMAX谢谢你的建议,我不太确定这会有什么帮助,但还是尝试了一下,没有成功。我可以复制。如果我删除了
FontStyle=FontStyles.Italic
,那么它就可以工作了。。。在我看来像个bug(你不是唯一一个:),你应该向Microsoft Connect@SimonMourier报告是的,它看起来确实像个bug。。。我会在报告之前等待一段时间,以防有人看到到底是什么地方出了问题(或者有一个很好的解决方法,目前我想我会重复这个过程,直到它起作用,这显然不是一个很好的修复)。XPS内部是一个固定文档的zip存档,其中包含用于文档演示的所有内容。它在内部尝试将字体作为内存流打包到xps zip存档中。您无法将字体打包到XPS存档中。字体可能会混淆。检查下面的URL和链接末尾的代码下载,看起来很旧的链接。也许,上述解决方案可以解决您的问题。
private void TestXpsSerialization(object a)
{
    for (int i = 0; i < 400; ++i)
    {
        TextBlock block = new TextBlock
        {
            Text = "Test",
            FontFamily = new FontFamily("Segoe UI"),
            FontStyle = FontStyles.Italic,
            FontWeight = FontWeights.Bold,
            Background = null,
            FontSize = 12
        };
        FixedDocument fixedDoc = new FixedDocument();

        PageContent pageContent = new PageContent();
        FixedPage fixedPage = new FixedPage();
        fixedPage.Children.Add(block);
        ((IAddChild) pageContent).AddChild(fixedPage);
        fixedDoc.Pages.Add(pageContent);
        using (MemoryStream documentStream = new MemoryStream())
        {
            string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
            Uri packageUri = new Uri(inMemoryPackageName);
            using (Package package = Package.Open(documentStream, FileMode.CreateNew))
            {
                MemoryStream resultStream = new MemoryStream();
                PackageStore.AddPackage(packageUri, package);
                using (XpsDocument xpsd =
                new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName))
                {
                    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsd);
                    writer.Write(fixedDoc);
                    package.Flush();

                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        SerializerWriter serializerWriter =
                        new XpsSerializerFactory().CreateSerializerWriter(outputStream);

                        bool success = true;
                        try
                        {
                            serializerWriter.Write(xpsd.GetFixedDocumentSequence());
                        }
                        catch (Exception e)
                        {
                            success = false;
                            Debug.WriteLine(e);
                        }
                        if (success)
                        {
                            outputStream.Seek(0, SeekOrigin.Begin);
                            outputStream.CopyTo(resultStream);
                        }
                    }
                }
                PackageStore.RemovePackage(packageUri);
                Debug.WriteLine(resultStream.Length);
            }
        }
    }
}
Ausnahme ausgelöst: "System.IO.FileFormatException" in PresentationCore.dll
System.IO.FileFormatException: Die Datei "pack://memorystream:,,62db450e-87fe-4246-a727-15ab02c5c55e.xps,/Resources/34890974-3e2d-4baf-9003-24c3375636b0.ODTTF" entspricht nicht der erwarteten Dateiformatspezifikation.
   bei MS.Internal.TrueTypeSubsetter.ComputeSubset(Void* fontData, Int32 fileSize, Uri sourceUri, Int32 directoryOffset, UInt16[] glyphArray)
   bei MS.Internal.FontFace.TrueTypeFontDriver.ComputeFontSubset(ICollection`1 glyphs)
   bei System.Windows.Media.GlyphTypeface.ComputeSubset(ICollection`1 glyphs)
   bei System.Windows.Xps.Serialization.FEMCacheItem.SubSetFont(ICollection`1 glyphs, Stream stream)
   bei System.Windows.Xps.Serialization.FEMCacheItem.Commit()
   bei System.Windows.Xps.Serialization.XpsFontSubsetter.CommitFontSubsetsSignal(FontSubsetterCommitPolicies signal)
   bei System.Windows.Xps.Serialization.XpsFontSerializationService.SignalCommit(Type type)
   bei System.Windows.Xps.Serialization.XpsSerializationManager.ReleaseXmlWriter(Type writerType)
   bei System.Windows.Xps.Serialization.DocumentSequenceSerializer.set_XmlWriter(XmlWriter value)
   bei System.Windows.Xps.Serialization.DocumentSequenceSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
   bei System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject)
   bei System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
   bei System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
   bei System.Windows.Xps.XpsDocumentWriter.Write(FixedDocumentSequence fixedDocumentSequence)
   bei System.Windows.Xps.Serialization.XpsSerializerWriter.Write(FixedDocumentSequence fixedDocumentSequence)
"System.IO.FileFormatException" in PresentationCore.dll
System.IO.FileFormatException: "pack://memorystream:,,62db450e-87fe-4246-a727-15ab02c5c55e.xps,/Resources/34890974-3e2d-4baf-9003-24c3375636b0.ODTTF" file does not conform to the expected file format specification.