从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打开,我在这里错过了什么?

编辑:工作的最终代码,请参阅评论

提前致谢


已解决,请参阅问题帖子下的评论以及帖子本身的编辑

链接地址: http://www.djcxy.com/p/8655.html

上一篇: creating an XPS Document from a FlowDocument and attach it on the fly

下一篇: Validate and submit a form without enter in an infinite cycle?