PrintHelper

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

PrintHelper

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);

                    CSHTML5.Interop.ExecuteJavaScript(@"
                    var printPreview = window.open('about:blank', 'print_preview', 'resizable=yes,scrollbars=yes,status=yes');
                    var printDocument = printPreview.document;
                    printDocument.open();
                    printDocument.write('<!DOCTYPE html>'+
                    '<html>'+
                        $0.innerHTML+
                    '</html>');
                    printDocument.close();
                    printPreview.print();", htmlDiv);
                }
                else
                {
                    MessageBox.Show("Printing is not available when running inside the Simulator.");
                }
            }
            else
            {
                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.


JS-Support

fangeles
Posts: 52
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.

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

Re: PrintHelper

Postby JS-Support @Userware » Thu Dec 03, 2020 12:01 am

Hi,

A newer API is available which is documented at:
http://cshtml5.com/documentation/printing.aspx

Styles should be preserved with the API above. If they are not, can you please post a small sample project to reproduce the issue?

Thanks
Regards


Return to “Extensions and Plugins for CSHTML5”

Who is online

Users browsing this forum: No registered users and 2 guests

 

 

cron