microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Question: Printing in WinUI 3 Desktop

Open bridgesquared opened this issue 3 years ago • 34 comments

Hi all

I'm looking for guidance on printing from WinUI 3 Desktop please.

I tried the PrintHelper from Community Toolkit 8 preview 4 and get a System.Runtime.InteropServices.COMException: 'Interface not registered (0x80040155)' I put this down to issues with getting the Windows.Graphics.Printing.PrintManager as reported in #4375 and #4363.

So I suppose I'm trying to answer a few questions:

  1. Is the PrintManager + PrintDocument combination going to be the preferred printing technique for WinUI 3 Desktop (as per UWP)
  2. If not what is plan for WinUI 3 Desktop?
  3. Is there a vision of when the capability will be available? there's nothing Print specific on the roadmap at the moment
  4. Has anyone found workarounds/solutions to getting printing working in Preview 4?

I would welcome any thoughts, pointers etc. please. I know there's a huge amount of working going on to get the controls working properly, but Print has just got to the top of my stack and I'm a bit worried that it has fallen through the cracks which is going to cause issues as I can't ship without it.

BTW. We do have some fallback ideas, maybe WebView HTML generation or PDF generation, but to be honest I rather liked the UWP approach to printing and really hope it makes it through to WinUI 3 Desktop.

Thanks for your time, and all your hard work. I do think this unified approach is the right way to go and I can't wait to get away from NET Native and shipping modern apps with up-to-date backing tech (NET5/6 + WinUI)

bridgesquared avatar Mar 04 '21 12:03 bridgesquared

I agree that at least adding printing APIs to the roadmap would be very helpful to plan for WinUI 3.0 adoption.

FreddyDgh avatar Mar 04 '21 16:03 FreddyDgh

@MikeHillberg and @codendone do you have any insights here?

StephenLPeters avatar Mar 05 '21 00:03 StephenLPeters

Thank your for opening this issue. I myself planned to do it some time ago, but back then I was busy and I forgot about it. AFAIK, there is the interface IPrintManagerInterop which has the methods: GetForWindow and ShowPrintUIForWindowAsync, which can take hwnds as parameters. Also, according to the docs, these are for desktop apps only. There is also this managed wrapper I found sometime ago, but if I recall correctly I was unable to make it work in a demo WPF app.

XamDR avatar Mar 05 '21 18:03 XamDR

If it is the intended default, I hope the API sees improvements to large document handling. Printing a 100+ page document quickly leads to out-of-memory scenarios in UWP as you need to render all the pages up front and in-memory in XAML before handing off to be printed. Some form of "on demand" callback would help work around that.

JohnnyWestlake avatar Mar 06 '21 22:03 JohnnyWestlake

I'm sorry, the PrintDocument API hasn't been updated for WinUI3 yet, and it's an accident that the API is showing up right now. I'll look into the roadmap, but it's not something we're actively working on yet.

@JohnnyWestlake, you should be able to hand off a page at a time rather than all 100 at once. After each call to PrintDocument.AddPage you don't need the keep the pageVisual around.

MikeHillberg avatar Mar 18 '21 04:03 MikeHillberg

I'm sorry, the PrintDocument API hasn't been updated for WinUI3 yet, and it's an accident that the API is showing up right now. I'll look into the roadmap, but it's not something we're actively working on yet.

Hi @MikeHillberg, do you have an update on where printing sits in the roadmap please. I really need to get some printed output from a Win UI 3 (Reunion 0.5) Desktop app and would appreciate guidance from the team please. Thanks

bridgesquared avatar Apr 06 '21 08:04 bridgesquared

Hi, any updates on this, or is there any workaround? At now we try to find a way to print documents in our application

alexdi220 avatar Jun 23 '21 10:06 alexdi220

Hi @MikeHillberg, We are testing a port of an app from UWP to WinUI Desktop. PrintManager is also what we require too. It will be great to let us know what the future guidance for this is. Thanks.

BarcodeResource avatar Jul 29 '21 02:07 BarcodeResource

Windows App SDK is now 1.0 stable and this bug still prevents printing (and using PrintHelper from Community Toolkit). Any news?

MartinRothschink avatar Nov 25 '21 13:11 MartinRothschink

@MartinRothschink et al.

You can vote on this and other things you want here:

https://portal.productboard.com/winappsdk/1-windows-app-sdk/c/50-support-printmanager-api

kmgallahan avatar Nov 25 '21 15:11 kmgallahan

Thanks, voted.

MartinRothschink avatar Nov 25 '21 15:11 MartinRothschink

Still no updates?

NoI3ody avatar Feb 05 '22 16:02 NoI3ody

Working with WinUI and still waiting on PrintDocument to be updated to WinUI.

Wandtket avatar Mar 28 '22 14:03 Wandtket

same

ezexe avatar Apr 13 '22 20:04 ezexe

I just tested (Windows 10 21H1) and PrintDocument works :

WinUI_Printing

castorix avatar Apr 14 '22 06:04 castorix

I just tested (Windows 10 21H1) and PrintDocument works :

WinUI_Printing

can you maybe share your code for your testproject, so we can investigate what we maybe do wrong?

NoI3ody avatar Apr 18 '22 00:04 NoI3ody

can you maybe share your code for your testproject, so we can investigate what we maybe do wrong?

I made a test sample : WinUI3_Print.zip (Windows App SDK 1.1.0-preview1, .NET 6, VS 2022, Windows 10 21H1)

WinUI3_Print

castorix avatar Apr 18 '22 18:04 castorix

Sorry this stupid question, but I am not very experienced with VS. I tried to re-compile this project using VS2022 and I've got some errors "spacename Forms not found" while calling PrintDialog. Similarly, I've got an error with System.Drawing.Common, this could be resolved by installing a package via NuGet-PackageManager. For the Forms I cannot find any source and the assembly is marked with warn sign. Some SDKs are also marked with the warn sign, see below. I would appreciate any help. image

hgkujawa avatar Apr 27 '22 07:04 hgkujawa

Sorry this stupid question, but I am not very experienced with VS. I tried to re-compile this project using VS2022 and I've got some errors "spacename Forms not found" while calling PrintDialog. Similarly, I've got an error with System.Drawing.Common, this could be resolved by installing a package via NuGet-PackageManager. For the Forms I cannot find any source and the assembly is marked with warn sign. Some SDKs are also marked with the warn sign, see below. I would appreciate any help. !

I have put in comments (at beginning of MainWindow.xaml.cs) the links to the 3 Assemblys to add, depending on .NET 6 version For example, I had .NET 6.0.3 and now I have .NET 6.0.4 So I must remove the 3 Assemblys ([Right-click][Remove] on each) and add them ([Dependencies][Right-click][Add COM Reference...][Browse] from : "C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Drawing.Common.dll" "C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Windows.Forms.dll" "C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Resources.Extensions.dll"

castorix avatar Apr 27 '22 08:04 castorix

Could get it to work with your example @castorix, but with the PrintHelper from the Windows Community Toolkit, I still get the same error as the @bridgesquared

NoI3ody avatar Apr 27 '22 08:04 NoI3ody

Ok, I've got it working, but... these are old-style dialogs, which I do not really want in my WnUI3(!) app 😊 So I will rather wait, until the PrintManager is working, similarly to UWP apps.

hgkujawa avatar Apr 27 '22 14:04 hgkujawa

Ok, I've got it working, but... these are old-style dialogs, which I do not really want in my WnUI3(!) app 😊 So I will rather wait, until the PrintManager is working, similarly to UWP apps.

Instead of default dialogs, you can use your own windows Test with random controls + PrintPreviewControl :

PrintPreview_Custom

castorix avatar Apr 28 '22 10:04 castorix

@marb2000 has an app with a printing sample you can find here: https://github.com/marb2000/PrintSample

Generally, *ForCurrentView() methods don't work in WinUI 3 desktop apps, which don't have a core window. Instead, there is a COM interop you can use: IPrintManagerInterop

var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
printMan = PrintManagerInterop.GetForWindow(hWnd);
await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)

This should work in WinAppSDK version 1.1.1 and downlevel.

We will update documentation to reflect this.

krschau avatar Jun 15 '22 16:06 krschau

Getting System.ArgumentException: 'Value does not fall within the expected range.' On this line : await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)

Redp87 avatar Jun 16 '22 14:06 Redp87

Getting System.ArgumentException: 'Value does not fall within the expected range.' On this line : await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)

The sample works for me on Windows 11 (21H2), but gives me the same System.ArgumentException as above when running on Windows 10 (21H2). Is printing support intended to be for Windows 11 only?

FreddyDgh avatar Jun 16 '22 16:06 FreddyDgh

Yes I'm on Windows 10 when I get this error.

Redp87 avatar Jun 16 '22 16:06 Redp87

Nop. It should be working on Windows10 too. My team is investigating why these APIs don't work on Win10 given they were there since long time ago. Maybe there is some piece of the puzzle that we lost.

marb2000 avatar Jun 16 '22 19:06 marb2000

had to change the first line, instead of using "this" i used "App.StartupWindow"

var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(App.StartupWindow); printMan = PrintManagerInterop.GetForWindow(hWnd); await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)

At least the printUI does show up, but i got some problems with my dispatcher now... Made my repo public, maybe someone can investigate this. Note I am a beginner.

NoI3ody avatar Jun 18 '22 14:06 NoI3ody

So I was trying to print with System.Windows.Forms.PrintDialog and when I try to render a UIElement (page to print) to bitmap with RenderTargetBitmap I get the same error : Value does not fall within the expected range, in renderBitmap.GetPixelsAsync(). So I think the problem is in the WinUI UIElements. Hope this can pinpoint the problem and you guys can help solve it.

Redp87 avatar Jun 21 '22 09:06 Redp87

@Redp87 @NoI3ody @FreddyD-GH It looks like there is a bug in Windows 10 (internal link) that's preventing the IPrintManagerInterop solution from working. We're investigating servicing the fix downlevel, and will update here when we know more about that.

krschau avatar Jun 22 '22 18:06 krschau