欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GWT通过JSNI打印Web页面内容

程序员文章站 2024-01-26 10:06:28
...
在GWT中通过JSNI调用JS脚本去执行web页面打印:
首先是预览打印界面,代码如下:

package net.carefx.sde.editor.web.editor.client.partogram;

import net.carefx.component.editor.web.client.jsni.PartogramScript;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
/**
* 产程图预览打印窗口
* @author icarefx-lqy
*
*/
public class PartogramPreview extends Dialog
{
//向产程图绘制程序发出请求资源:imgUrl + imgName = 请求地址
// private String imgUrl = "http://localhost:8080/chanchengtu/img?";
private String imgUrl = null;
private String imgName;
private PartogramScript script;

public PartogramPreview (String imgName, String imgUrl)
{
this.imgUrl = imgUrl;
this.imgName = imgName;
script = new PartogramScript();
initUI();
}

private void initUI ()
{
setSize (960, Window.getClientHeight () - 64);
setHeading("产程图预览");
setLayout(new FitLayout());

final LayoutContainer lc = new LayoutContainer();
lc.setId("imgContainner");//set div.id="imgContainner";
Image img = new Image();
img.setUrl(imgUrl+imgName);
lc.add(img);
lc.setScrollMode(Scroll.AUTO);
this.setButtons("");
this.setButtonAlign(HorizontalAlignment.CENTER);
this.add(lc);

Button print = new Button("打印");
print.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {
// TODO 调用打印函数
script.excutePrint(imgUrl+imgName);
}
});

Button cancel = new Button("取消");
cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {
// TODO 关闭预览打印
hide();
}
});
this.addButton(print);
this.addButton(cancel);

}
}


然后是JSNI调用:

package net.carefx.component.editor.web.client.jsni;
/**
* 产程图打印脚本
* @author icarefx-lqy
*
*/
public class PartogramScript {

public PartogramScript()
{}

public native void excutePrint(String id)
/*-{
$wnd._divprint(id);
}-*/;

public native void getElementById (String id)
/*-{
$wnd._getdivbyid(id);
}-*/;
}

JSNI调用的JS脚本:

/**
* 打印DIV包裹的内容
* @param printdiv
* @returns {Boolean}
*/
function _divprint (id)
{
var win = window.open('','','width=925,height=1055');;
var headstr = "<html><head><title></title></head><body>";
var img = "<img src='"+id+"' />";
var footstr = "</body></html>";
win.document.open("text/html","replace");
win.document.writeln(headstr);
win.document.writeln(img);
win.document.write(footstr);
win.document.close();
win.print();
}
相关标签: gwt