0

Is there a way to accomplish the above.

Sam
  • 61
  • 4

1 Answers1

0

There is a PrintHelper object in Windows Community Toolkit, you only have to instantiate the PrintHelper object and call AddFrameworkElementToPrint method to add the mutiple XAML controls you want to print.

In addition, when you initialize the object, you need to pass a XAML panel that will be used to host printable control. It needs to be in your visual tree but can be hidden with Opacity = 0, like below. For more details, you can refer to this document.

MainPage.xaml:

<StackPanel>
    <Canvas x:Name="PrintCanvas" Opacity="0"/>
    <Button Click="Button_Click">click</Button>
</StackPanel>

.cs:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var printHelper = new PrintHelper(PrintCanvas);
    printHelper.AddFrameworkElementToPrint(new BlankPage1());
    printHelper.AddFrameworkElementToPrint(new BlankPage2());
    await printHelper.ShowPrintUIAsync("Title");
}
Faywang - MSFT
  • 5,798
  • 1
  • 5
  • 8
  • Thanks. If I need to print a portion of a page located in subgrid, how do I modify the above code. – Sam Dec 26 '19 at 23:09
  • The document mentions controls cannot be linked to a visual tree. It means their parent has to be null. If you want to print the subgrid, you can disconnect it before sending it to print or you can create a new one from scratch. If you don't want to use the above method, you need to implement the printing process manually without using the above Toolkit, I created a simple [sample](https://github.com/wcj233/PrintMutilXaml), you can check it. For more details, you can refer to this [document](https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/print-from-your-app) about printing. – Faywang - MSFT Dec 27 '19 at 08:37
  • @Faywang-MSFT I used your sample from GigHub and it works well. But how can we do the same when the app has `WebView` control with a webpage displayed in it? – nam Jul 02 '20 at 03:13
  • @nam You could use [WebViewBrush](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.webviewbrush?view=winrt-19041) to print WebView, create a Rectangle and render the content of webview into the Rectangle, then you could print this Rectangle instead. – Faywang - MSFT Jul 02 '20 at 07:23
  • @Faywang-MSFT I tried your suggestion. It shows the printed PDF with blurred content. I have posted the issue [here](https://stackoverflow.com/q/62701768/1232087). – nam Jul 02 '20 at 17:19