Search code examples
c#wpfprintingwpfdatagridsystem.printing

Printing WPF controls without using PrintDialog isn't fully rendering on sizes larger than NorthAmericanLetter sized paper


I have run into a problem that seems to have little information on the web and has been giving me trouble the past two days. When I print to an 8.5 x 11" piece of paper, the output is correct on all printers. When I print to 11 x 17", the output is correct on all virtual (doPDF7, XPS Document Writer) printers. The output on an actual physical printer (Xerox WorkCentre 7535) only renders, from the top-left corner, 8.5 x 11" worth of my control onto a 11 x 17" piece of paper. My control is properly sized (dimension of the 11 x 17") it just doesn't get completely rendered.

I have a Print Preview control implemented where the user can select a printer (PrintQueue) and the settings (PrintCapabilities) for their selected printer. Changing these settings will show up during run time so the user knows exactly what they are printing.

When the user then wants to print their document, I prepare the selected PrintQueue by merging the ticket (their selections in the print preview) with the PrintQueue.CurrentJobSettings.CurrentPrintTicket:

public void Prepare(PrintTicket ticketToMerge)
{
    // _printer is the PrintQueue
    // I've also tried to merge with UserPrintTicket, DefaultPrintTicket, and created a PrintTicket using XPS Document Writer printer default print ticket
    System.Printing.ValidationResult result = _printer.MergeAndValidatePrintTicket(_printer.CurrentJobSettings.CurrentPrintTicket, ticketToMerge);
    _printer.CurrentJobSettings.CurrentPrintTicket = result.ValidatedPrintTicket;
    _printer.Commit();

    // _printer.Refresh(); // This doesn't help it seems
}

then I call to print my DocumentPaginator:

public void Print(DocumentPaginator paginator)
{
    var writer = PrintQueue.CreateXpsDocumentWriter(_printer);
    writer.Write(paginator, _printer.CurrentJobSettings.CurrentPrintTicket);
}

and the actual printer calls DocumentPaginator.GetPage:

public override DocumentPage GetPage(int pageNumber)
{
    var view = new PrintingContent
    {
        // Create my control and make it the biggest size it can be without
        // going into the margins

        // Subtract the margins since they can't be printed in, these come from
        // _printerCapabilities.PageImageableArea
        Width = this.PageSize.Width - this.Margin.Left - this.Margin.Right,
        Height = this.PageSize.Height - this.Margin.Top - this.Margin.Bottom,

        // This might be a problem since the print preview control
        // is also using "this" but haven't found any evidence to conclude this thought
        DataContext = this 

    };

    view.Measure(new Size(view.Width, view.Height));
    view.Arrange(new Rect(new Point(), new Size(view.Width, view.Height)));
    view.UpdateLayout();

    var dg = UItilities.FindVisualChildren<DataGrid>(view).First();
    // dg.ItemsSource = (File as ParameterCollectionShell).GetParametersByPage(pageNumber) 
    // This didn't work so I tried giving the datagrid a new instance of List - this might be causing problems?
    var parameters = new List<ParameterViewModel>((File as ParameterCollectionShell).GetParametersByPage(pageNumber));
    dg.ItemsSource = parameters;

    // Doesn't seem to help
    // view.Measure(new Size(view.Width, view.Height));
    // view.Arrange(new Rect(new Point(), new Size(view.Width, view.Height)));
    view.UpdateLayout();

    //return new DocumentPage(view, 
    //                        this.PageSize, 
    //                        new Rect(new Point(), new Size(view.Width, view.Height)),  
    //                        new Rect(new Point(this.Margin.Left, this.Margin.Top), 
    //                        new Size(view.Width, view.Height)));
    return new DocumentPage(view);
}

Of course I have debugged this and checked sizes to make sure they are what they are supposed to be. Everything works printing to an .xps document or .pdf; but why doesn't it work when being sent to a physical printer?

For what it's worth, there is a DataGrid in my control and I had trouble with *-size spacing columns in the past, but they are all fixed width so I'm not sure if its related or not.


Solution

  • It turns out that it must have been a printer driver issue. I re-installed the driver for the same printer and after doing so everything worked correctly.

    I got in direct contact with Xerox's firmware team for the specific printer and they let me know that there was firmware updates for the printer and that it could be serviced to hopefully fix it. They couldn't pin point exactly what the issue is, but the issue is now in their bug system.

    When in doubt, re-install a printers driver and try printing again when you're having rendering problems. I'm not sure what invokes this bad behavior but printing to any other printer worked correctly; that's how I narrowed it down to a driver issue.