我正在尝试编写XPS文档。目前" doc"是一个FixedDocument,有一个PageContent对象,其子类型为FixedPage,有7个UIElements,因为它是" Children"。它扔了
error Message "Compressed part has inconsistent data length."
这是一个样本
private FixedDocument Document;
private DocumentViewer Viewer;
private FixedPage Page;
private PageContent Content;
首先,我成功地重置了文档和查看器:
private void ResetDocument()
{
Viewer = new DocumentViewer();
Document = new FixedDocument();
}
我创建了一个单一的新页面(我为多个页面获得了相同的错误)
public void NewPage()
{
Page = new FixedPage();
Content = new PageContent();
((IAddChild)Content).AddChild(Page);
Document.Pages.Add(Content);
}
我已成功将UIElements添加到页面
public void AddVisualToPage(UIElement element)
{
Page.Children.Add(element);
}
然后我在这部分得到了错误。
public void SaveCurrentDocument(string name)
{
Viewer.Document = Document;
string xpspath = Path.Combine(ReportsPath, name + ".xps");
var doc = (FixedDocument) Viewer.Document;
var xpsd = new XpsDocument(xpspath, FileAccess.ReadWrite);
XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(doc); //THIS IS THE LINE WHERE I GET THE ERROR
xpsd.Close();
}
这是我的堆栈跟踪。
at MS.Internal.IO.Packaging.CompressStream.UpdateUncompressedDataLength(Int64 dataLength)
at MS.Internal.IO.Packaging.CompressStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.Crc32Calculator.CalculateStreamCrc(Stream stream)
at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.CalculateCrc()
at MS.Internal.IO.Zip.ZipIOLocalFileBlock.UpdateReferences(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOBlockManager.SaveContainer(Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOBlockManager.SaveStream(ZipIOLocalFileBlock blockRequestingFlush, Boolean closingFlag)
at MS.Internal.IO.Zip.ZipIOFileItemStream.Flush()
at MS.Internal.IO.Packaging.CompressStream.Flush()
at MS.Internal.IO.Packaging.CompressStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at MS.Internal.IO.Zip.ZipIOLocalFileBlock.Dispose(Boolean disposing)
at MS.Internal.IO.Zip.ZipIOBlockManager.RemoveLocalFileBlock(ZipIOLocalFileBlock localFileBlock)
at MS.Internal.IO.Zip.ZipArchive.DeleteFile(String zipFileName)
at System.IO.Packaging.ZipPackage.IgnoredItemHelper.Delete(ValidatedPartUri partUri)
at System.IO.Packaging.ZipPackage.CreatePartCore(Uri partUri, String contentType, CompressionOption compressionOption)
at System.IO.Packaging.Package.CreatePart(Uri partUri, String contentType, CompressionOption compressionOption)
at System.Windows.Xps.Packaging.XpsManager.GeneratePart(ContentType contentType, Uri partUri)
at System.Windows.Xps.Packaging.XpsManager.GenerateUniquePart(ContentType contentType)
at System.Windows.Xps.Packaging.XpsDocument.AddFixedDocumentSequence()
at System.Windows.Xps.Serialization.XpsPackagingPolicy.AcquireXmlWriterForFixedDocumentSequence()
at System.Windows.Xps.Serialization.XpsSerializationManager.AcquireXmlWriter(Type writerType)
at System.Windows.Xps.Serialization.ReachHierarchySimulator.SimulateBeginFixedDocumentSequence()
at System.Windows.Xps.Serialization.ReachHierarchySimulator.BeginConfirmToXPSStructure(Boolean mode)
at System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
at System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
at System.Windows.Xps.XpsDocumentWriter.Write(FixedDocument fixedDocument)
我很困惑这个问题是什么......我完全不明白为什么它会压缩任何东西,(我猜XPS使用压缩数据?)数据长度如何不一致。
感谢您的帮助!