【问题标题】:Very slow printing from WPF从 WPF 打印非常慢
【发布时间】:2011-06-11 17:33:42
【问题描述】:

我最近花了很多时间试图弄清楚为什么我正在处理的应用程序(.Net 4.0,WPF 前端)的打印速度如此之慢,而且我完全没有想法(打印 150 页需要 25 分钟以上)。

我已经尝试了各种打印方法(PrintDialog、XpsDocumentWriter、VisualsToXpsDocument),既可以使用直接来自控件的矢量数据,也可以先渲染控件(RenderTargetBitmap)并发送图像,但每种方法给出的结果大致相同.

有趣的是,当使用 VisualsToXpsDocument 进行批量写入时,我可以在打印框架处理 21 页的时间内创建 186 页的内容。这里确实有问题。

为了确保这不仅仅是应用程序中某些控件的复杂性问题,我创建了一个独立的演示应用程序,其中仅包含一个数据网格,其中填充了 4000 行静态数据和大约 8 列。数据网格本身没有性能问题,只是打印。这是我一直在使用的最被接受的方法,但效果不佳。

        this.writer 
          = PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);

        PrintingDocumentPaginator paginator 
          = new PrintingDocumentPaginator(this.PrintConfiguration, 
                contentSize, pageSize, contentRect, this.printSource, false);

        this.writer.WritingProgressChanged += this.OnPrintingProgressChanged;
        this.writer.WritingCompleted += this.OnPrintingCompleted;
        this.writer.WritingCancelled += this.OnPrintingCanceled;

        this.writer.WriteAsync(paginator, 
                this.PrintConfiguration.PrintTicket, paginator.PageCount);

或者,如果我使用以下代码,对 EndBatchWrite() 的调用将很快被命中,而其余的打印过程需要更长的时间。

        this.writer 
          = PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);

        PrintingDocumentPaginator paginator 
            = new PrintingDocumentPaginator(this.PrintConfiguration, 
                    contentSize, pageSize, contentRect, 
                    this.printSource, this.useVectorData);

        this.writer.WritingProgressChanged += this.OnPrintingProgressChanged;
        this.writer.WritingCompleted += this.OnPrintingCompleted;
        this.writer.WritingCancelled += this.OnPrintingCanceled;

        VisualsToXpsDocument sdf 
          = (VisualsToXpsDocument)this.writer.CreateVisualsCollator();

        for (int i = 0; i < paginator.PageCount; i++)
        {
            sdf.WriteAsync(paginator.GetPageVisual(i));
        }

        sdf.EndBatchWrite();

那么我在这里做错了什么?我是否向打印机发送了错误的数据?有什么我没看到的秘密吗?

编辑 - 这适用于物理打印机以及文件打印机,即 XPS 打印机、PDF 等。

干杯,

山姆。

【问题讨论】:

    标签: .net wpf performance printing


    【解决方案1】:

    这几乎就是我所做的,这对我来说真的很快:

            LocalPrintServer localPrintServer = new LocalPrintServer();
            System.Printing.PrintQueue pq = new System.Printing.PrintQueue(localPrintServer, localPrintServer.DefaultPrintQueue.FullName);
    
            System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(pq);
            PrintCapabilities pc = pq.GetPrintCapabilities();
    
            PageImageableArea pia = pc.PageImageableArea;
    
            if (docWriter != null)
            {
                DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
    
                // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
                paginator.PageSize = new System.Windows.Size(pia.ExtentWidth, pia.ExtentHeight);
    
                // Send content to the printer.
                docWriter.Write(paginator);
            }
    

    我不使用你使用的循环,因为我从来不需要它。我只是放手,并在它们稍后到达时处理任何错误(即在我已经事先检查了打印机状态之后)。要检查打印机状态,您只需查看您正在使用的打印机队列上的状态属性。

    我希望对您有所帮助。

    【讨论】:

    • 打印队列中出现的打印作业是否相当大? 3年前我遇到了这个问题,我几乎不记得我是如何解决它的。这与在某些情况下,WPF 打印系统会光栅化内容而不是生成可以直接转换为 postscript 的内容有关。
    猜你喜欢
    • 2016-10-10
    • 2015-05-07
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2013-11-06
    相关资源
    最近更新 更多