用到WEB打印這方面內容,沒想中間遇到很多問題,曾在網上找過許多解決方案,有些控件但是國內的基本都是需要註冊收費的,只有國外的一個ScriptX是免費的,看到許多人用,但這個控件被IE等衆多的安全工具所困擾,常常需要設置一堆東西,對於電腦水平不是很高客戶來說確實很麻煩,不過我也只用它去掉了IE打印的頁眉頁腳,打印還是用的IE的WEBBROWSER,不說費話了,下面先介紹一下我思路。
由於是要套打,所以套打的頁面不可能給用戶看,需要單獨製作一個套打的頁面,只有要打印的內容,位置用 <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;
}
剩下的就等着打印機幹活吧。