Print or PDF support [SOLVED]

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
dopre
Posts: 1
Joined: Sun Nov 22, 2015 5:28 am

Print or PDF support [SOLVED]

Postby dopre » Sun Nov 22, 2015 5:34 am

I started to look at a way to migrate my existing Silverlight Application to HTML5 and didn't find anything that would solve my priniting requirements so far.

I have the following scenarion:
The user drag and drop a set of price tags for some articles on a letter/A4 paper. He enters the price and hits the print button, the letter/A4 paper is printed.

Is it possible with C#/XAML for HTML5 to print the canvas directly on the printer or at least to create a pdf or jpg out of it that the user would be able to print?

Thanks,
Dopre

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

Re: Print or PDF support

Postby JS-Support @Userware » Tue Nov 24, 2015 1:12 am

Hi dopre and welcome to the forums.

Yes, this is possible. We are going to send you some code to print from a CSHTML5 app within a few days.

Regards,
JS-Support

CyborgDE
Posts: 75
Joined: Wed Aug 12, 2015 9:21 pm

Re: Print or PDF support

Postby CyborgDE » Fri Dec 04, 2015 6:17 am

Hello,

can you send me the code too ???

Regards, Uwe

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

Re: Print or PDF support

Postby JS-Support @Userware » Fri Dec 04, 2015 10:54 am

Hello,

We are still writing the sample code. I think it will be ready by the end of next week. Thanks for your patience.

Regards,
JS-Support

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

Re: Print or PDF support

Postby JS-Support @Userware » Mon Dec 07, 2015 3:15 am

Hello,

Please find below the code to print with C#/XAML for HTML5.

Just create a new class named "PrintHelper" in your project, and replace the class code with the following one:

Code: Select all

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

public static class PrintHelper
{
    public static void Print(UIElement uiElement)
    {
        if (CSharpXamlForHtml5.DomManagement.IsControlInVisualTree(uiElement))
        {
            if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript)
            {
                JSIL.Verbatim.Expression(@"
                    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();", CSharpXamlForHtml5.DomManagement.GetDomElementFromControl(uiElement));
            }
            else
            {
                MessageBox.Show("Printing is not available when running inside the Simulator.");
            }
        }
        else
            throw new Exception("You can only print a UIElement that is in the visual tree. To fix the issue, add the UIElement to the visual tree before printing it.");
    }
}


To use the class, just call the "PrintHelper.Print(uielement)" method, passing the UIElement that you want to print.

Regards,
The C#/XAML for HTML5 team

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

Re: Print or PDF support [SOLVED]

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



Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 38 guests

 

 

cron