[java/VB script] IE 程式控制印表(不用按確定)



[java/VB script] IE 程式控制印表(不用按確定)

IE 程式控制印表(不用按確定)
來源:MSDN  cww實作


window.print() 一定會出現一個列印的對話方塊,而且沒有辦法省略,這是必然的結果,因為Browser
的環境原本就不允許Browser上的Script/Object等來存取Client 機器的資源,否則,隨便一個Web程式
就可以存取Client的Resource,那就太可怕了。但實際的應用上,有時就是要允許Web的程式來存取
Client的資源(如:File/Printer/Registry ....),這時我們的做法是在IE的安全設定上允許DownLoad/執行
某個站所發佈的ActiveX Control/Applet 等。



使用者按收費之後要自動印出收據,而Browser的做法是按收費的按鈕後,出現收據的Preview,還得
再按一下印表才能列出,或者使用另一篇文章的做法,不出現收據的Preview網頁,而直接出現列印
的對話方塊以要求由哪一台Printer印出等資訊。不管如何,使用者都要按兩次才能印出報表。試想 intranet的環境中,使用者會願意如此做嗎?


所以有另一個沒有辦法的辦法,那就是寫程式攔截 列印 的對話方塊,而後模擬選了哪一台印表機
與列印的範圍等資訊後,再自動按確定。但是這種做法不保證對新版的IE都有效。目前在Win9X上
的IE5.5 IE6都可以Work,但不適用於win2000,那需要另外再改程式。

要使用這ActiveX control項,需先取得WebPrint.CAB,並放到網站上,而Client端需設定允許
由該網站上DownLoad/執行 ActiveX Control項。請在 工具/Internet選項/安全性/自訂層級 中設定。



目前可設定的屬性有以下:

屬性名稱 說明  ie5.5 可用否 ie6 可用否
pntPrinterPort 設定列由哪一印表機Port印出(如:LPT1:)
pntPrinterName 設定列由哪一印表機Device印出(如:Stylus)
pntPageOption 1:列印全部頁   2:印 From 第n頁 to 第 m頁
pntPageFrom 上述2:之n
pntPageTo 上述2:之m
pntPageHeader 每一頁的Header
pntPageFooter 每一頁的Footer
pntLeftMargin 每一頁左邊界(mm)
pntRightMargin 每一頁右邊界(mm)
pntTopMargin 每一頁上邊界(mm)
pntBottomMargin 每一頁下邊界(mm)
pntOrientation 設定紙張1:直印  2:橫印 不可
pntPaperSize 設定紙張大小 不可
pntPaperSource 設定紙張來源 不可
pntAutoPrint True:自動按確定  False:不自動按確定


方法:
1.GetPrinterList() 取得該Client所有印表機名稱,每個印表機名稱中間用 ; 隔開
2.EnableCBTHook() :IE6要用之,且在window.print()前設定
3.BeforePrint() : IE5.5用之且在window.print()前設定
4.AfterPrint() :ie5.5用之,且在window.print()後設定
使用範例:
  1. <HTML>
  2. <HEAD>
  3. <SCRIPT LANGUAGE="VBScript">
  4. Sub doPrint()
  5. 'msgbox "Ready to Print"
  6. wbp.pntOrientation = 1
  7. wbp.pntTopMargin = 20.5
  8. wbp.pntPageOption = 2
  9. wbp.pntPageFrom = 1
  10. wbp.pntPageTo = 1
  11. wbp.pntPrinterName = window.cboPrinter.value
  12. wbp.pntAutoPrint = True
  13. if instr(1,window.navigator.appVersion,"MSIE 6.") > 0 then
  14. wbp.EnableCBTHook
  15. window.print()
  16. else
  17. wbp.BeforePrint
  18. window.print()
  19. wbp.AfterPrint
  20. end if
  21. End Sub
  22. </SCRIPT>
  23. <SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>
  24. <!--
  25. Sub window_onload
  26. dim opt, strPntList, aryPrinter, i, strPrinter
  27. strPntList = wbp.GetPrinterList()
  28. if len(strPntList) = 0 then exit sub
  29. aryPrinter = split(strPntList,";")
  30. for i = lbound(aryPrinter) to ubound(aryPrinter)
  31. strPrinter = aryPrinter(i)
  32. set opt= document.createElement("OPTION")
  33. opt.text = strPrinter
  34. opt.value = strPrinter
  35. window.cboPrinter.options.add(opt)
  36. next
  37. End Sub
  38. -->
  39. </SCRIPT>
  40. </HEAD>
  41. <BODY>
  42. <object classid="clsid:67885808-366A-4DE0-B18D-EECD9EE2A27B" id="wbp" width="0" height="0" CODEBASE="include/WebPrint.CAB#version=1,0,0,0">>
  43. </object>
  44. <INPUT TYPE="BUTTON" VALUE="Print" ONCLICK="doPrint()">
  45. This is a Test <script LANGUAGE=vbscript>document.write(window.navigator.appVersion)</script>
  46. <select name="cboPrinter">
  47. </select>
  48. </BODY>
  49. </HTML>
複製代碼


Javascript呼叫IE列印以及預覽列印的方法

整頁列印:
html部分:
<INPUT TYPE="button" value="整頁列印" onclick="print()">

Javascript部分:
N/A

部分列印(只列印div包起來的網頁):
html部分:
<div id="block">
<P><img id="ruten" name="ruten" SRC="http://www.ruten.com.tw/imgs/2008/logo.gif"></P>
</div>
<input type="button" value="部分列印" onclick="printScreen(block)">

Javascript部分:
//列印div包起來的部分並且列印完畢後自動關閉列印網頁
function printScreen(block){
var value = block.innerHTML;
var printPage = window.open("","printPage","");
printPage.document.open();
printPage.document.write("<HTML><head></head><BODY onload='window.print();window.close()'>");
printPage.document.write("<PRE>");
printPage.document.write(value);
printPage.document.write("</PRE>");
printPage.document.close("</BODY></HTML>");
}

整頁預覽列印:
html部分:
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<INPUT TYPE="button" value="整頁預覽" onclick="javascript:wb.execwb(7,1)">

Javascript部分:
N/A

部分預覽列印(只預覽div包起來的部份):
html部分:
<div id="block">
<P><img id="ruten" name="ruten" SRC="http://www.ruten.com.tw/imgs/2008/logo.gif"></P></div>
<input type="button" value="區塊預覽" onclick="previewScreen(block)">

Javascript部分:
//預覽div包起來的部分並且列印完畢後自動關閉列印網頁
function previewScreen(block){
var value = block.innerHTML;
var printPage = window.open("","printPage","");
printPage.document.open();
printPage.document.write("<OBJECT classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2' height=0 id=wc name=wc width=0></OBJECT>");
printPage.document.write("<HTML><head></head><BODY onload='javascript:wc.execwb(7,1);window.close()'>");
printPage.document.write("<PRE>");
printPage.document.write(value);
printPage.document.write("</PRE>");
printPage.document.close("</BODY></HTML>");
}