WPF DocumentPaginator 是否将所有 DocumentPages 保留在内存中直到完成打印作业?

发布于 2024-10-25 04:41:38 字数 254 浏览 0 评论 0原文

关于 WPF DocumentPaginator 的严格理论问题:

当使用 WPF DocumentPaginator 类打印多页文档时,分页器是否将其请求的所有 DocumentPages 保留在内存中的打印作业历史记录中,直到文档完成打印?或者在打印作业完成之前它是否曾经处理掉不再需要的任何DocumentPages(即释放内存)?有没有办法手动告诉分页器在打印过程中释放旧的/不需要的 DocumentPages 而不会出现异常?

非常感谢您对此的帮助!

Strictly theoretical question concerning WPF's DocumentPaginator:

When using the WPF DocumentPaginator class to print a multi-page document, does the paginator keep all of the DocumentPages it requests over the history of the print job in memory until the document finishes printing? Or does it ever dispose of any DocumentPages it no longer needs before the print job is finished (i.e. to free up memory)? Is there a way to manually tell the paginator to free old/unneeded DocumentPages in the course of printing without hitting an exception?

Many thanks for your help with this!

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

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

发布评论

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

评论(2

弥繁 2024-11-01 04:41:41

我遇到了和你完全相同的问题。

它不会处理之前加载的页面。为了解决这个问题,我所做的就是在 GetPage() 方法的末尾保留对已加载页面的引用,并在 GetPage 方法的开头处理最后加载的页面。

这里是你的附加问题的答案:

我的印象是 System.Windows.Controls.PrintDialog.Print(DocumentPaginator, title) 的实现是这样的:

Public void PrintDocument(DocumentPaginator paginator, string title)
{
   Dictionary<int, DocumentPage> pages = new Dictionary<int DocumentPage>();
   for(int i=0; i<paginator.PageCount(); i++)
   {
      pages.Add(i, paginator.GetPage(i));
      UnknownPrinterEngine.SendPageToPrinter(pages(i)); //this is just imagination
   }
}

如果实现确实是这样的,那么对每个已处理页面的本地引用保持活动状态(在字典中)直到方法执行完成。 -->没有内存会被释放。

我做了什么来避免这种情况(在扩展 DocumentPaginator 的类中实现 GetPage):

DocumentPage lastLoadedPage = null;
public DocumentPage GetPage(int pageNumber)
{
   if(lastLoadedPage != null)
   {
      lastLoadedPage.Dispose()
   }
   //MyPrintControl should be your custom UserControl which represents the page to print
   myPrintControl pageContent = new MyPrintControl();
   pageContent.Width = PageSize.Width;
   pageContent.Height = PageSize.Height;

   pageContent.Measure(PageSize);
   pageContent.Arrange(new Rect(new Point(0,0), PageSize));

   DocumentPage actualPage = New DocumentPage(pageContent);
   lastLoadedPage = actualPage;
   return actualPage;
}

最后,您应该实现 IDisposable 接口,并在 Dispose 方法中清理 lastLoadedPage 字段以释放最后一页的内存。

I had exactly the same problem as you have.

It doesnt dispose the Pages which got loaded earlier. What I did to solve this issue was to hold a reference to the loaded page at the end of the GetPage() Method and dispose the last loaded page in the beginning of the GetPage Method.

here the answer to your aditional question:

I have the impression the implementation of System.Windows.Controls.PrintDialog.Print(DocumentPaginator, title) is something like that:

Public void PrintDocument(DocumentPaginator paginator, string title)
{
   Dictionary<int, DocumentPage> pages = new Dictionary<int DocumentPage>();
   for(int i=0; i<paginator.PageCount(); i++)
   {
      pages.Add(i, paginator.GetPage(i));
      UnknownPrinterEngine.SendPageToPrinter(pages(i)); //this is just imagination
   }
}

if the implementation is really something like that, a local reference to each processed page stays alive (in the dictionary) until the method execution finished. --> No memory will get freed.

What i did to avoid that (GetPage implementation in the class which extends DocumentPaginator):

DocumentPage lastLoadedPage = null;
public DocumentPage GetPage(int pageNumber)
{
   if(lastLoadedPage != null)
   {
      lastLoadedPage.Dispose()
   }
   //MyPrintControl should be your custom UserControl which represents the page to print
   myPrintControl pageContent = new MyPrintControl();
   pageContent.Width = PageSize.Width;
   pageContent.Height = PageSize.Height;

   pageContent.Measure(PageSize);
   pageContent.Arrange(new Rect(new Point(0,0), PageSize));

   DocumentPage actualPage = New DocumentPage(pageContent);
   lastLoadedPage = actualPage;
   return actualPage;
}

And at the end you should implement the IDisposable interface and in the Dispose Method clean up the lastLoadedPage field to free the memory of the last page too.

内心荒芜 2024-11-01 04:41:41

查看System.Windows.Xps.VisualsToXpsDocument 这解决了我在创建大型 xps 文档时内存不足的问题。另请查看 PrintQueue.CreateXpsDocumentWriter(somePrintQueue) 。抱歉,我没有足够的代表来发表评论。

Look into System.Windows.Xps.VisualsToXpsDocument This fixes the problem I have running out of memory when creating large xps document. Also look into PrintQueue.CreateXpsDocumentWriter(somePrintQueue) . Sorry, I don't have enough reps to just make this a comment.

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