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

发布于 2024-11-25 15:26:04 字数 1157 浏览 0 评论 0原文

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

public static Stream 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))
        {
            xpsDoc.AddFixedDocumentSequence();
            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();
        }

        return stream;
    }
}

然后我使用此代码附加它:

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

其中 FD 是我想要转换的 FlowDocument ,我收到附加的 0.0KB 大小的 XPS 文件,并且无法使用 XPS 查看器打开它,我是这里不见了?

I have a FlowDocument that I want to convert to an XPS Document and attach it to an e-mail and send it all together. I'm using this code

public static Stream 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))
        {
            xpsDoc.AddFixedDocumentSequence();
            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();
        }

        return stream;
    }
}

Then I attach it using this code:

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

where FD is the FlowDocument I want to convert , I'm receiving 0.0KB size XPS file attached and it can't be open with the XPS Viewer , what I'm missing here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2024-12-02 15:26:04

已解决,这是最终有效的代码:

    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。

Solved , this is the final code which worked:

    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;   
    }

Then I attach it using this code:

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

where FD is the FlowDocument I want to convert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文