Very slow printing from WPF

Recently, I spent a lot of time figuring out why printing works so slowly in the application I'm working on (.Net 4.0, WPF interface) and I have all of the ideas (25 minutes to print 150 pages).

I tried various printing methods (PrintDialog, XpsDocumentWriter, VisualsToXpsDocument) both with vector data directly from the controls, and using the visualization of the controls (RenderTargetBitmap) and just sending images, but each method gives roughly the same results.

Interestingly, when using VisualsToXpsDocument for batch writing, I can create 186 page content while the print platform is processing 21 pages. Something is really wrong here.

To make sure that this is not just a problem with the complexity of some controls in the application, I created a separate demo application that contains only a data grid filled with 4000 rows of static data and about 8 columns. There are no performance issues in the data grid itself, only when printing. Here the most acceptable approach that I used gives bad results.

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

Alternatively, if I use the following code, the EndBatchWrite () call will be deleted very quickly, and the rest of the printing process will take much longer.

        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();

So what am I doing wrong here? Am I sending incorrect data to the printer? Is there any secret I don't see?

EDIT. This applies to physical printers, as well as file printers, such as XPS, PDF, etc.

Greetings

Sam.

+5
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);
        }

, , . , ( , ). , , .

, .

0

All Articles