WPF DocumentViewer.Print fails when printing to PDF but only from my application

1

My WPF app view uses DocumentViewer which I let the user Print. It displays perfectly in my UI and the DocumentViewer.Print command produces the desired output if the user chooses normal printer. Unfortunately, if the user chooses the "Microsoft Print to PDF" printer, it just creates an empty document. I can't figure out why or how to work around it.

When I use that same "Print to PDF" printer from any other application it works perfectly. So I don't think it's a problem with it. But literally all I am doing in my app is calling DocumentViewer.Print so I don't know what else to do to try to make my app work with it like all the others.

The only threads I can find on the topic mostly date to before 2010 and aren't very helpful. Does anyone have any experience with this?

More Info

I see a big difference in debugger output between choosing a normal printer and the PDF printer. But while there are many errors, I'm not sure what to do.

Here is the debugger output of a successful print. Note that there are two seemingly harmless exceptions caught by the DocumentViewer even during this successful print:

First my code calls DocumentViewer.Print and gets a harmless (apparently) PrintQueueException

Exception thrown: 'System.Printing.PrintQueueException' in ReachFramework.dll
PrintTicket provider failed to bind to printer. Win32 error: The printer name is invalid.

Then the Print Dialog appears. The user chooses the normal physical printer and the following output appears as the document is successfully printed

appendChild succeeded..
Element appended successfully...appendChild succeeded..
Element appended successfully...appendChild succeeded..
appendChild succeeded..
appendChild succeeded..
Element appended successfully...appendChild succeeded..
Element appended successfully...appendChild succeeded..
appendChild succeeded..
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll

At this point, the document has successfully printed. Note that the "NotSupportedException" you see above showed this text in the debugger: 'BitmapMetadata is not available on BitmapImage.' But again, it printed successfully so I took this as harmless.

When the user does the same thing but chooses the PDF Printer,

First, I get the same (seemingly harmless) PrintQueueException before the dialog.

Exception thrown: 'System.Printing.PrintQueueException' in ReachFramework.dll
PrintTicket provider failed to bind to printer. Win32 error: The printer name is invalid.

Then, in the dialog, the user chooses "Microsoft Print to PDF" and I see this in the output window.

Exception thrown at 0x00007FFF71C6A839 (KernelBase.dll) in MyApp.Mobile.exe: 0x000006BA: The RPC server is unavailable.
Exception thrown at 0x00007FFF71C6A839 (KernelBase.dll) in MyApp.Mobile.exe: 0x000006BA: The RPC server is unavailable.
onecoreuap\shell\ext\thumbnailcache\lib\thumbcacheobj.cpp(2076)\thumbcache.dll!00007FFF40D2157A: (caller: 00007FFF40D0CBF1) ReturnHr(1) tid(5920) 80070057 The parameter is incorrect.
onecoreuap\shell\ext\thumbnailcache\lib\thumbcacheobj.cpp(2076)\thumbcache.dll!00007FFF40D2157A: (caller: 00007FFF40D0CBF1) ReturnHr(2) tid(5404) 80070057 The parameter is incorrect.
onecoreuap\shell\ext\thumbnailcache\lib\thumbcacheobj.cpp(2076)\thumbcache.dll!00007FFF40D2157A: (caller: 00007FFF40D0CBF1) ReturnHr(3) tid(5404) 80070057 The parameter is incorrect.
CLR:(C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll) Rejecting native image because native image dependency C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll had a different identity than expected
CLR:(C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll) Rejecting native image because native image dependency C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll had a different identity than expected
onecoreuap\shell\ext\thumbnailcache\lib\thumbcacheobj.cpp(2076)\thumbcache.dll!00007FFF40D2157A: (caller: 00007FFF40D0CBF1) ReturnHr(4) tid(5404) 80070057 The parameter is incorrect.
Exception thrown at 0x00007FFF71C6A839 (KernelBase.dll) in MyApp.Mobile.exe: 0x000006BA: The RPC server is unavailable.
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFD690.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFC700.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFCA10.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFCA90.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFCDA0.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFC710.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFCA20.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFCA90.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: SplException::THResultException at memory location 0x00000047D2FFCDA0.
Exception thrown at 0x00007FFF71C6A839 in MyApp.Mobile.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
win_musl::production::ZipInterleaver::Rundown, m_activeParts != 0 is true, There are still parts open, package is invalid on close.
Exception thrown: 'System.Runtime.InteropServices.COMException' in ReachFramework.dll
Exception thrown: 'System.Printing.PrintingCanceledException' in ReachFramework.dll
Exception thrown: 'System.ArgumentException' in System.Printing.dll
Exception thrown: 'System.Printing.PrintingCanceledException' in System.Printing.dll

I've tried searching on the error messages that start with "win_musl::production" but am not finding much.

Any ideas?

wpf
pdf
printing
asked on Stack Overflow Dec 20, 2019 by Joe

1 Answer

1

I found a workaround. I am answering my own question in the hopes that this might help someone else in the future who might find themselves trying to print their own DocumentViewer to PDF.

I stumbled on it when I lifted DocumentViewer's printing code and copied it it to my own source. I wanted to see how it worked and monkey with it a bit. Specifically I lifted DocumentViewerBase.OnPrintCommand from the MS Reference Source.

On a hunch, I changed the line that calls XpsDocumentWriter.WriteAsync to use XpsDocumentWrite.Write instead.

Now my view prints out perfectly. No errors in the output.

My theory is that asynchronous printing is a bad idea in my case because I am printing an Active WPF View. The view in question must be interactive because it has several interactive controls showing images that the user can zoom or pan to get them looking just as desired before printing.

That means that the user could manipulate it while it was printing. I suspect this caused problems with the ReachFramework.dll

Anyway this fixes it so I can live with it. If synchronous printing ends up taking toolong, I could have a dedicated "Save To PDF" button of my own that does this and let normal printing go the Async route.

answered on Stack Overflow Dec 20, 2019 by Joe

User contributions licensed under CC BY-SA 3.0