PrintDialog.PrintDocument(DocumentPaginator, string) does not print to some printers on Win 7 or on Win 10 RRS feed

  • Question

  • I have a WPF application coded in C# and a few customers complained that they were unable to print from our SW. I did some initial research and many sources online stated that it was a printer driver issue (as was the case for the HP Officejet Pro 8620). And in fact if I did the workaround steps and installed another driver, printing magically worked. However, during Windows 10 testing I found that I was unable to print from ANY printers...

    The errors from the PrintService were always the same:

    The document , owned by xyz, failed to print on printer HP Officejet Pro 8620 (Copy 1). Try to print the document again, or restart the print spooler. 
    Data type: RAW. Size of the spool file in bytes: 1906. Number of bytes printed: 1906. Total number of pages in the document: 1. Number of pages printed: 0. Client computer: \\YYY. Win32 error code returned by the print processor: 2147500037. Unspecified error

    So I started stepping through the .NET code and I noted that behind the PrintDocument call, an XPS was generated as an intermediary using my custom DocumentPaginatorClass. It was defined like this:

    internal sealed class ReportDocumentPaginator : DocumentPaginator { private readonly ObservableCollection<ReportPage> m_document; /// <summary> /// Creates a ReportDocumentPaginator /// </summary> /// <param name="document">collection of Report Pages</param> /// <param name="pageSize">dimensions of the report pages</param> public ReportDocumentPaginator(ObservableCollection<ReportPage> document, Size pageSize) { m_document = document; PageSize = pageSize; } /// <summary> /// gets a particular page /// </summary> /// <param name="pageNumber">page number</param> /// <returns>FixedDocument associated with the page number</returns> public override DocumentPage GetPage(int pageNumber) {

    //Page is a FixedPage object return new DocumentPage(m_document[pageNumber].Page); } public override bool IsPageCountValid { get { return true; } } public override int PageCount { get { return m_document.Count; } } public override Size PageSize { get; set; } public override IDocumentPaginatorSource Source { get { return null; } } }

    According to all the examples online, this is pretty standard stuff. So I dug deeper and I decided to create the XPS myself and see if I could tweak how the document was created so that it could print. One override of XPsDocumentWriter.Write() looked promising: 

    // printDialog.PrintDocument(RptDocumentPaginator, m_Title); // old print call
                                    var writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
                           writer.Write((FixedDocumentSequence)rptDocument, printDialog.PrintTicket);

    All the sudden I was able to print again (I had to implement an explicit cast to a FixedDocumentSequence from my custom ReportDocument class to do it though. So it adds some complexity. So does that mean that there is a bug in my implementation of my derived DocumentPaginator class or is there a bug in .NET? 

    Friday, July 22, 2016 8:57 PM

All replies

  • Hi fotoflux,

    Thank you for your post.

    Since Our forum is discussing about NET Framework Base Classes (BCL)
    As your question is about WPF, we will help you move this case to WPF forum for dedicated support.

    Thank you for your understanding.

    Best regards,

    Cole Wu

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 25, 2016 6:06 AM
  • Thanks Cole! Hopefully somebody can answer my question in the proper forum.
    Wednesday, August 3, 2016 2:57 PM