How to print WebView content in a Windows Store App?

How to print WebView content in a Windows Store App?

To print the content of a WebView in a Windows Store app, you can use the WebViewBrush class to create a bitmap of the WebView content and then print the bitmap using the PrintHelper class. Here's an example of how to do this:

  • First, create a WebViewBrush object and set its source to the WebView control you want to print:
WebViewBrush brush = new WebViewBrush(); brush.SetSource(webViewControl); 
  • Next, create a Canvas object and add the WebViewBrush object to it:
Canvas canvas = new Canvas(); canvas.Width = brush.Bounds.Width; canvas.Height = brush.Bounds.Height; canvas.Children.Add(brush); 
  • Create a PrintHelper object and set its print options:
PrintHelper printHelper = new PrintHelper(this); printHelper.AddFrameworkElementToPrint(canvas); printHelper.OnPrintFailed += PrintHelper_OnPrintFailed; printHelper.OnPrintSucceeded += PrintHelper_OnPrintSucceeded; 
  • Call the ShowPrintUIAsync method to show the print dialog and start the print job:
await printHelper.ShowPrintUIAsync("Print WebView"); 

Here's the full code for a PrintWebView method that prints the content of a WebView control:

private async void PrintWebView(WebView webViewControl) { // Create a WebViewBrush and set its source to the WebView control WebViewBrush brush = new WebViewBrush(); brush.SetSource(webViewControl); // Create a Canvas and add the WebViewBrush to it Canvas canvas = new Canvas(); canvas.Width = brush.Bounds.Width; canvas.Height = brush.Bounds.Height; canvas.Children.Add(brush); // Create a PrintHelper and set its print options PrintHelper printHelper = new PrintHelper(this); printHelper.AddFrameworkElementToPrint(canvas); printHelper.OnPrintFailed += PrintHelper_OnPrintFailed; printHelper.OnPrintSucceeded += PrintHelper_OnPrintSucceeded; // Show the print dialog and start the print job await printHelper.ShowPrintUIAsync("Print WebView"); } 

Note that you'll need to handle the OnPrintSucceeded and OnPrintFailed events of the PrintHelper class to handle success and failure cases of the print job.

Examples

1. "Windows Store App C# Print WebView Content"

Code:

using Windows.Graphics.Printing; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Printing; private void PrintWebViewContent(WebView webView) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView Content", (args) => { args.SetSource(PrintWebView(webView).PrintableArea, PrintWebView(webView)); }); }; PrintManager.ShowPrintUIAsync(); } private WebViewBrush PrintWebView(WebView webView) { WebViewBrush webViewBrush = new WebViewBrush(); webViewBrush.SetSource(webView); // Adjust size if needed webViewBrush.Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill; return webViewBrush; } 

Description: Use PrintManager and WebViewBrush to print the content of a WebView in a Windows Store App.

2. "C# Windows Store App WebView Print Preview"

Code:

private void WebViewPrintPreview(WebView webView) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView Preview", (args) => { args.SetSource(PrintWebView(webView).PrintableArea, PrintWebView(webView)); // Add print preview UI if needed args.SetPreviewPage(CreatePrintPreview(webView)); }); }; PrintManager.ShowPrintUIAsync(); } private FrameworkElement CreatePrintPreview(WebView webView) { WebViewBrush webViewBrush = PrintWebView(webView); Rectangle previewRectangle = new Rectangle { Width = webView.ActualWidth, Height = webView.ActualHeight, Fill = new ImageBrush { ImageSource = webViewBrush } }; // Customize print preview UI as needed return previewRectangle; } 

Description: Enhance the print task to include a print preview with custom UI.

3. "Windows Store App WebView Print Landscape Orientation"

Code:

private void PrintWebViewContentLandscape(WebView webView) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView Landscape", (args) => { args.SetSource(PrintWebView(webView).PrintableArea, PrintWebView(webView)); // Set landscape orientation args.Options.Orientation = PrintOrientation.Landscape; }); }; PrintManager.ShowPrintUIAsync(); } 

Description: Configure the print task for landscape orientation.

4. "C# Windows Store App WebView Print Specific Area"

Code:

private void PrintSpecificAreaOfWebView(WebView webView, double x, double y, double width, double height) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print Specific Area", (args) => { args.SetSource(PrintSpecificAreaOfWebView(webView, x, y, width, height).PrintableArea, PrintSpecificAreaOfWebView(webView, x, y, width, height)); // Additional print settings }); }; PrintManager.ShowPrintUIAsync(); } private WebViewBrush PrintSpecificAreaOfWebView(WebView webView, double x, double y, double width, double height) { WebViewBrush webViewBrush = new WebViewBrush { SourceName = webView.Name, Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill, AlignmentY = AlignmentY.Top, AlignmentX = AlignmentX.Left, Transform = new Windows.UI.Xaml.Media.MatrixTransform { Matrix = new Windows.UI.Xaml.Media.Matrix { M11 = 1, M22 = 1, OffsetX = -x, OffsetY = -y } } }; webViewBrush.Redraw(); return webViewBrush; } 

Description: Print a specific area of the WebView by adjusting the WebViewBrush properties.

5. "Windows Store App C# Print WebView with Header/Footer"

Code:

private void PrintWebViewWithHeaderFooter(WebView webView, string header, string footer) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView with Header/Footer", (args) => { args.SetSource(PrintWebViewWithHeaderFooter(webView, header, footer).PrintableArea, PrintWebViewWithHeaderFooter(webView, header, footer)); // Additional print settings }); }; PrintManager.ShowPrintUIAsync(); } private Grid PrintWebViewWithHeaderFooter(WebView webView, string header, string footer) { WebViewBrush webViewBrush = PrintWebView(webView); Grid printGrid = new Grid(); printGrid.Children.Add(new Rectangle { Width = webView.ActualWidth, Height = webView.ActualHeight, Fill = new ImageBrush { ImageSource = webViewBrush } }); // Add header and footer printGrid.Children.Add(new TextBlock { Text = header, VerticalAlignment = VerticalAlignment.Top }); printGrid.Children.Add(new TextBlock { Text = footer, VerticalAlignment = VerticalAlignment.Bottom }); return printGrid; } 

Description: Customize the print task to include header and footer content.

6. "C# Windows Store App WebView Print to PDF"

Code:

private async void PrintWebViewToPdf(WebView webView) { var printToPdfTask = new PrintToPdfTask("Print WebView to PDF", async (context) => { var printDocument = new PrintDocument(); printDocument.SetSource(PrintWebView(webView).PrintableArea, PrintWebView(webView)); var pdfDocument = new PdfDocument(printDocument); context.SetPdfDocument(pdfDocument); // Additional PDF settings }); await printToPdfTask.ExecuteAsync(); } 

Description: Use the PrintToPdfTask to print WebView content to a PDF file.

7. "Windows Store App C# WebView Print Margin Settings"

Code:

private void PrintWebViewWithMargins(WebView webView, double leftMargin, double topMargin, double rightMargin, double bottomMargin) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView with Margins", (args) => { args.SetSource(PrintWebViewWithMargins(webView, leftMargin, topMargin, rightMargin, bottomMargin).PrintableArea, PrintWebViewWithMargins(webView, leftMargin, topMargin, rightMargin, bottomMargin)); // Set print margins args.Options.SetMargins(new PrintMargin(leftMargin, topMargin, rightMargin, bottomMargin)); }); }; PrintManager.ShowPrintUIAsync(); } private Grid PrintWebViewWithMargins(WebView webView, double leftMargin, double topMargin, double rightMargin, double bottomMargin) { WebViewBrush webViewBrush = PrintWebView(webView); Grid printGrid = new Grid(); printGrid.Children.Add(new Rectangle { Width = webView.ActualWidth, Height = webView.ActualHeight, Fill = new ImageBrush { ImageSource = webViewBrush } }); // Additional content with margins return printGrid; } 

Description: Adjust print margins for the WebView content.

8. "C# Windows Store App WebView Print Multiple Pages"

Code:

private void PrintWebViewContentMultiplePages(WebView webView) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView Multiple Pages", (args) => { args.SetSource(PrintWebViewContentMultiplePages(webView).PrintableArea, PrintWebViewContentMultiplePages(webView)); // Additional settings for multiple pages }); }; PrintManager.ShowPrintUIAsync(); } private UIElement PrintWebViewContentMultiplePages(WebView webView) { WebViewBrush webViewBrush = PrintWebView(webView); Grid printGrid = new Grid(); printGrid.Children.Add(new Rectangle { Width = webView.ActualWidth, Height = webView.ActualHeight, Fill = new ImageBrush { ImageSource = webViewBrush } }); // Additional content for multiple pages return printGrid; } 

Description: Handle printing WebView content across multiple pages.

9. "C# Windows Store App WebView Print Landscape Orientation"

Code:

private void PrintWebViewLandscape(WebView webView) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView Landscape", (args) => { args.SetSource(PrintWebViewLandscape(webView).PrintableArea, PrintWebViewLandscape(webView)); // Set landscape orientation args.Options.Orientation = PrintOrientation.Landscape; }); }; PrintManager.ShowPrintUIAsync(); } private WebViewBrush PrintWebViewLandscape(WebView webView) { WebViewBrush webViewBrush = PrintWebView(webView); webViewBrush.Transform = new Windows.UI.Xaml.Media.MatrixTransform { Matrix = new Windows.UI.Xaml.Media.Matrix { M11 = 0, M22 = 1, M21 = 1, M12 = 0 } }; return webViewBrush; } 

Description: Configure the print task for landscape orientation with a rotated WebViewBrush.

10. "Windows Store App C# WebView Print with Page Range"

**Code:** ```csharp private void PrintWebViewWithPageRange(WebView webView, int startPage, int endPage) { PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += (s, e) => { PrintTask printTask = e.Request.CreatePrintTask("Print WebView with Page Range", (args) => { args.SetSource(PrintWebViewWithPageRange(webView, startPage, endPage).PrintableArea, PrintWebViewWithPageRange(webView, startPage, endPage)); // Set page range args.Options.PageRangeOptions.AllowAllPages = false; args.Options.PageRangeOptions.SetPageRange((uint)startPage, (uint)endPage); }); }; PrintManager.ShowPrintUIAsync(); } private UIElement PrintWebViewWithPageRange(WebView webView, int startPage, int endPage) { WebViewBrush webViewBrush = PrintWebView(webView); Grid printGrid = new Grid(); printGrid.Children.Add(new Rectangle { Width = webView.ActualWidth, Height = webView.ActualHeight, Fill = new ImageBrush { ImageSource = webViewBrush } }); // Additional content for specific page range return printGrid; } ``` **Description:** Set a specific page range for printing WebView content. 

More Tags

ocr html-generation log4net-appender translate-animation grails-orm matlab-table radix-sort endpoint libsass mac-address

More C# Questions

More Various Measurements Units Calculators

More Transportation Calculators

More Dog Calculators

More Financial Calculators