WPF DocumentViewer,打印到PDF时打印失败,但仅从我的应用程序打印

xzabzqsa  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(300)

我的WPF应用程序视图使用DocumentViewer,我让用户打印。它在我的UI和DocumentViewer中显示完美。如果用户选择普通打印机,打印命令会产生所需的输出。不幸的是,如果用户选择“Microsoft Print to PDF”打印机,它只会创建一个空文档。我不知道为什么或如何解决它。
当我从 * 任何其他应用程序 * 使用相同的“打印到PDF”打印机时,它工作得很好。所以我不认为这是它的问题。但实际上,我在我的应用程序中所做的一切就是调用DocumentViewer.Print,所以我不知道还能做些什么来尝试让我的应用程序像其他所有应用程序一样使用它。
我能找到的关于这个主题的帖子大多是2010年以前的,没有什么帮助。有人有这方面的经验吗?

详细信息

我发现选择普通打印机和PDF打印机的调试器输出有很大的不同。但是,虽然有许多错误,我不知道该怎么办。
以下是成功打印的调试器输出。请注意,即使在此成功打印期间,DocumentViewer也会捕获两个看似无害的异常:
首先,我的代码调用DocumentViewer.Print并得到一个无害的(表面上)PrintQueueException

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

然后出现打印对话框。用户选择普通物理打印机,文档成功打印后会出现以下输出

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

此时,文档已成功打印。请注意,您在上面看到的“NotSupportedException”在调试器中显示了以下文本:“BitmapMetadata在BitmapImage上不可用。”但同样,它打印成功,所以我认为这是无害的。
当用户执行相同操作但选择***PDF打印机***时,
首先,我在对话框之前得到了相同的(看起来无害的)PrintQueueException。

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

然后,在对话框中,用户选择“Microsoft Print to PDF”,我在输出窗口中看到了这一点。

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

我尝试搜索以“win_musl::production”开头的错误消息,但没有找到太多。
有什么想法吗?

flvlnr44

flvlnr441#

我找到了一个变通办法。我正在回答我自己的问题,希望这可能会帮助其他人在未来谁可能会发现自己试图打印自己的documentViewer到PDF。
我在提取DocumentViewer的打印代码并将其复制到我自己的源代码时偶然发现了它。我想看看它是如何工作的,并稍微摆弄一下它。具体来说,我从MS Reference Source中提取了DocumentViewerBase.OnPrintCommand
凭直觉,我将调用XpsDocumentWriter.WriteAsync的行改为使用XpsDocumentWrite.Write
现在我的视图完美地打印出来了。输出中没有错误。
我的理论是,异步打印在我的情况下是一个坏主意,因为我打印的是活动WPF视图。所讨论的视图必须是交互式的,因为它有几个交互式控件,显示用户可以缩放或平移的图像,以使它们在打印前看起来与所需的一样。
这意味着用户可以在打印时对其进行操作。我怀疑这会导致ReachFramework.dll出现问题
无论如何,这修复了它,所以我可以接受它。如果同步打印最终需要很长时间,我可以有一个专门的“保存到PDF”按钮,我自己的这样做,让正常的打印走异步路线。

vngu2lb8

vngu2lb82#

我们遇到了一个类似的错误,我们的wpf xaml文件中的图像在运行时没有显示/打印:这似乎与2022年12月21日的 windows 更新相吻合。
我们将文件夹6.0.12从c:\program files\donet\shared和installed中移走
NET 6.0桌面运行时(v6.0.11)- Windows x64安装程序
我们的图像打印正常。

相关问题