套打的页面不可能给用户看,需要单独制作一个套打的页面,只有要打印的内容,位置用<DIV style="LEFT: 35mm; FONT: 9pt 宋体; POSITION: absolute; TOP: 75mm">打印内容</DIV>其中,left 是你打印纸从最左边到内容的距离,top 是从纸最上边到打印内容的距离,这个距离要用尺量好,并且减去19.05,这个数是IE页面设置中页距的上下左右的默认值,不减就对不准了。
下面是在网上找到一个脚本,专门是用来调用套打页面的,并根据我的需要做了点改动。
window.print = printFrame;
// main stuff
function printFrame(frame, onfinish) {
if ( !frame ) frame = window;
function execOnFinish() {
switch ( typeof(onfinish) ) {
case "string": alert('print... '); break;//execScript(onfinish)
case "function": onfinish();
}
if ( focused && !focused.disabled ) focused.focus();
}
if (( frame.document.readyState !== "complete") &&( !frame.document.confirm("The document to print is not downloaded yet! Continue with printing?") ))
{
execOnFinish();
return;
}
var eventScope = printGetEventScope(frame);
var focused = document.activeElement;
window.printHelper = function() {
execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
printFireEvent(frame, eventScope, "onafterprint");
printWB.outerHTML = "";
execOnFinish();
window.printHelper = null;
}
document.body.insertAdjacentHTML("beforeEnd",
"<object id=/"printWB/" width=0 height=0 /
classid=/"clsid:8856F961-340A-11D0-A96B-00C04FD705A2/">");
printFireEvent(frame, eventScope, "onbeforeprint");
frame.focus();
window.printHelper = printHelper;
setTimeout("window.printHelper()", 0);
}
function hideButton()
{
document.getElementById("btn_Back").style.display = "none";
}
function printFixedApp()
{
if(confirm('确定要打印吗?'))//这些是我自己加的,业务需要隐藏点过的按钮
{
document.getElementById("btn_Back").style.display = "none";
document.getElementById("btn_print").style.display = "none";
document.getElementById("btn_modify").style.display = "none";
printHidden("printPage_fixed.aspx");//里面是套打的页面
}
}
function printHidden(url) {
document.body.insertAdjacentHTML("beforeEnd",
"<iframe name=printHiddenFrame width=0 height=0></iframe>");
var doc = printHiddenFrame.document;
doc.open();
doc.write("<body οnlοad=/"parent.onprintHiddenFrame()/">");
doc.write("<iframe name=printMe width=0 height=0 src=/"" +
url + "/"></iframe>");
doc.write("</body>");
doc.close();
}
function onprintHiddenFrame() {
function onfinish() {
printHiddenFrame.outerHTML = "";
if ( window.onprintcomplete )
{
window.onprintcomplete();
alert("print completed!");
}
window.location.href = "ApplicationBillView_fixed.aspx";//打印完成后转到其它页
}
printFrame(printHiddenFrame.printMe, onfinish);
}
// helpers
function printIsNativeSupport() {
var agent = window.navigator.userAgent;
var i = agent.indexOf("MSIE ")+5;
return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}
function printFireEvent(frame, obj, name) {
var handler = obj[name];
switch ( typeof(handler) ) {
case "string": frame.execScript(handler); break;
case "function": handler();
}
}
function printGetEventScope(frame) {
var frameset = frame.document.all.tags("FRAMESET");
if ( frameset.length ) return frameset[0];
return frame.document.body;
}
window.print = printFrame;
// main stuff
function printFrame(frame, onfinish) {
if ( !frame ) frame = window;
function execOnFinish() {
switch ( typeof(onfinish) ) {
case "string": alert('print... '); break;//execScript(onfinish)
case "function": onfinish();
}
if ( focused && !focused.disabled ) focused.focus();
}
if (( frame.document.readyState !== "complete") &&( !frame.document.confirm("The document to print is not downloaded yet! Continue with printing?") ))
{
execOnFinish();
return;
}
var eventScope = printGetEventScope(frame);
var focused = document.activeElement;
window.printHelper = function() {
execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
printFireEvent(frame, eventScope, "onafterprint");
printWB.outerHTML = "";
execOnFinish();
window.printHelper = null;
}
document.body.insertAdjacentHTML("beforeEnd",
"<object id=/"printWB/" width=0 height=0 /
classid=/"clsid:8856F961-340A-11D0-A96B-00C04FD705A2/">");
printFireEvent(frame, eventScope, "onbeforeprint");
frame.focus();
window.printHelper = printHelper;
setTimeout("window.printHelper()", 0);
}
function hideButton()
{
document.getElementById("btn_Back").style.display = "none";
}
function printFixedApp()
{
if(confirm('确定要打印吗?'))//这些是我自己加的,业务需要隐藏点过的按钮
{
document.getElementById("btn_Back").style.display = "none";
document.getElementById("btn_print").style.display = "none";
document.getElementById("btn_modify").style.display = "none";
printHidden("printPage_fixed.aspx");//里面是套打的页面
}
}
function printHidden(url) {
document.body.insertAdjacentHTML("beforeEnd",
"<iframe name=printHiddenFrame width=0 height=0></iframe>");
var doc = printHiddenFrame.document;
doc.open();
doc.write("<body οnlοad=/"parent.onprintHiddenFrame()/">");
doc.write("<iframe name=printMe width=0 height=0 src=/"" +
url + "/"></iframe>");
doc.write("</body>");
doc.close();
}
function onprintHiddenFrame() {
function onfinish() {
printHiddenFrame.outerHTML = "";
if ( window.onprintcomplete )
{
window.onprintcomplete();
alert("print completed!");
}
window.location.href = "ApplicationBillView_fixed.aspx";//打印完成后转到其它页
}
printFrame(printHiddenFrame.printMe, onfinish);
}
// helpers
function printIsNativeSupport() {
var agent = window.navigator.userAgent;
var i = agent.indexOf("MSIE ")+5;
return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}
function printFireEvent(frame, obj, name) {
var handler = obj[name];
switch ( typeof(handler) ) {
case "string": frame.execScript(handler); break;
case "function": handler();
}
}
function printGetEventScope(frame) {
var frameset = frame.document.all.tags("FRAMESET");
if ( frameset.length ) return frameset[0];
return frame.document.body;
}