Csharp/C#教程:从FlowDocument创建XPS文档并即时附加它分享


从FlowDocument创建XPS文档并即时附加它

我有一个FlowDocument,我想转换为XPS文档并将其附加到电子邮件并将它们一起发送。 我正在使用此代码

public static MemoryStream FlowDocumentToXPS(FlowDocument flowDocument, int width, int height) { MemoryStream stream = new MemoryStream(); using (Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite)) { using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Maximum)) { XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false); DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator; paginator.PageSize = new System.Windows.Size(width, height); rsm.SaveAsXaml(paginator); rsm.Commit(); } } stream.Position = 0; Console.WriteLine(stream.Length); Console.WriteLine(stream.Position); return stream; } 

然后我使用以下代码附加它:

 Attachment xps = new Attachment(FlowDocumentToXPS(FD, 768, 676), "FileName.xps", "application/vnd.ms-xpsdocument"); 

其中FD是我要转换的FlowDocument,我接收到附加的0.0KB大小的XPS文件,并且无法使用XPS Viewer打开,我在这里缺少什么?

编辑:有效的最终代码,请参阅注释

提前致谢

已解决,请查看问题post下的评论以及已在post上进行的编辑

上述就是C#学习教程:从FlowDocument创建XPS文档并即时附加它分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/960724.html

(0)
上一篇 2021年11月25日
下一篇 2021年11月25日

精彩推荐