Tuesday, September 15, 2015

GWT code for printing

Step -1

Add following line in our HTML file

 iframe id="printIT" style="width:0;height:0;border:0"

Step -2
Add following native method in GWT client Entry point class.


public static native void printMethod(String html) /*-{
var frame = $doc.getElementById('printing');
if (!frame) {
$wnd.alert("Error:  Can't find printing frame. Make sure you had added iframe with id=printIT in HTML file");
return;
}
frame = frame.contentWindow;
var doc = frame.document;
doc.open();
doc.write(html);
doc.close();
frame.focus();
frame.print();
}-*/;


Step -3

Add following line of code on click on button or where you want to implement print functionality.

We had selected GWT in build example and try to print the text it is showing on dialogBox. We can use any widget text to print even images.

String printText = dialogBox.asWidget().getElement().getInnerHTML();
printMethod(printText);







No comments: