JS-Support @Userware
Site Admin
Posts: 1129
Joined: Tue Apr 08, 2014 3:42 pm


Postby JS-Support @Userware » Fri Feb 26, 2016 11:09 am

Please find below a class that adds print support to your app.

The code requires CSHTML5 v1.0 Beta 7.2 or newer.

To use, simply add a new class named "PrintHelper.cs" to your project, and copy/paste the following code:

Code: Select all

using System;
using System.Windows;
using Windows.UI.Xaml;

namespace CSHTML5.Extensions.Printing
    public static class PrintHelper
        public static void Print(FrameworkElement frameworkElement)
            if (CSharpXamlForHtml5.DomManagement.IsControlInVisualTree(frameworkElement))
                if (!CSHTML5.Interop.IsRunningInTheSimulator)
                    var htmlDiv = CSHTML5.Interop.GetDiv(frameworkElement);

                    var printPreview ='about:blank', 'print_preview', 'resizable=yes,scrollbars=yes,status=yes');
                    var printDocument = printPreview.document;
                    printDocument.write('<!DOCTYPE html>'+
                    printPreview.print();", htmlDiv);
                    MessageBox.Show("Printing is not available when running inside the Simulator.");
                throw new Exception("You can only print an element that is in the visual tree. To fix the issue, add the FrameworkElement to the visual tree before printing it.");

How to use?
To use the class, just call the method "CSHTML5.Extensions.Printing.PrintHelper.Print(frameworkElement)", passing the FrameworkElement that you want to print.

How does it work internally?
The class works by copying the portion of the HTML DOM that corresponds to your FrameworkElement into a new browser window, and then calling the "Print Dialog" of the browser.


Posts: 51
Joined: Wed Jan 16, 2019 12:48 am

Re: PrintHelper

Postby fangeles » Fri Oct 16, 2020 1:21 am

Hi JS-Support,

Is there a way to keep the font style of text when using this extension? it looks like the font style (and some styles) are lost when the framework element transferred/copied to a new window..

Thank you.

Return to “Extensions and Plugins for CSHTML5”

Who is online

Users browsing this forum: No registered users and 2 guests