如何在WPF中生成和打印大型XPS文档?

bit*_*onk 10 memory printing wpf xps

我想从我的WPF应用程序生成(然后打印或保存)大型XPS文档(> 400页).我们有一些需要写入XPS的大量内存数据.

如果没有得到一个OutOfMemoryException怎么办呢?有没有办法可以把文件写成块?这通常是怎么做的?我不应该首先将XPS用于大文件吗?

这个根本原因OutOfMemoryException似乎是巨大的创造FlowDocument.我正在创建完整FlowDocument,然后将其发送到XPS文档编写器.这是错误的方法吗?

Che*_*eso 5

你怎么做呢?您没有显示任何代码.

我使用XpsDocumentWriter来编写块,如下所示:

FlowDocument flowDocument =  . .. ..;

// write the XPS document
using (XpsDocument doc = new XpsDocument(fileName, FileAccess.ReadWrite))
{
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
    DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;

    // Change the PageSize and PagePadding for the document
    // to match the CanvasSize for the printer device.
    paginator.PageSize = new Size(816, 1056);
    copy.PagePadding = new Thickness(72);  
    copy.ColumnWidth = double.PositiveInfinity;
    writer.Write(paginator);
}
Run Code Online (Sandbox Code Playgroud)

这不适合你吗?


ima*_*ima 3

我可以确认 XPS 不会长文档上引发内存不足。无论是在理论上(因为 XPS 上的操作是基于页面的,它不会尝试将整个文档加载到内存中),还是在实践中(我使用基于 XPS 的报告,并且看到失控的错误消息总计达数千条)页)。

难道问题出在一个特别大的页面上?例如,一张巨大的图像?大页面具有高 DPI 分辨率?如果文档中的单个对象太大而无法一次分配,则会导致内存不足异常。