Page 1 of 1
Print or PDF support [SOLVED]
Posted: Sun Nov 22, 2015 5:34 am
by dopre
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
Re: Print or PDF support
Posted: Tue Nov 24, 2015 1:12 am
by JS-Support @Userware
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
Re: Print or PDF support
Posted: Fri Dec 04, 2015 6:17 am
by CyborgDE
Hello,
can you send me the code too ???
Regards, Uwe
Re: Print or PDF support
Posted: Fri Dec 04, 2015 10:54 am
by JS-Support @Userware
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
Re: Print or PDF support
Posted: Mon Dec 07, 2015 3:15 am
by JS-Support @Userware
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
Re: Print or PDF support [SOLVED]
Posted: Fri Feb 26, 2016 11:10 am
by JS-Support @Userware