http://ltc603.javaeye.com/blog/123191#
對JS的列印方法總結一下,方便日後查閱。
一.用JS內建函數列印
直接調用Java代碼
- <a href="javascript:window.print();">列印</a>
二.IEWebBrowser組件
介紹
http://support.microsoft.com/default.aspx?scid=kb%3BEN-US%3BQ267240#top
http://support.microsoft.com/kb/q247671/#appliesto
Java代碼
- <OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=開啟>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=關閉所有>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存新檔>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=列印>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接列印>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=預覽列印>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=版面設定>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=屬性>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全選>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=重新整理>
- <input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=關閉>
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT> <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=開啟> <input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=關閉所有> <input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存新檔> <input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=列印> <input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接列印> <input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=預覽列印> <input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=版面設定> <input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=屬性> <input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全選> <input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=重新整理> <input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=關閉>
三.使用ScriptX.cab控制項
1.下載ScriptX.cab控制項
官網http://www.meadroid.com/scriptx/index.asp
2.使用object元素,修改codebase,classid的值
這裡調用控制項ScriptX.cab Java代碼
- <OBJECT id="factory" style="DISPLAY: none" codeBase="${rootUrl}js/smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT>
這段代碼用來載入cab檔案,clsid和codebase必須要和你下載的cab中的資訊對應,否則組件會載入錯誤,這兩項其實不難找,只要你用winrar開啟你下載的cab檔案,然後找到副檔名是.inf的檔案,然後開啟之,就能看到了。
3.調用控制項指令碼
Print.js檔案 Java代碼
- function setPrintBase(headerText,footerText,rootUrl) {
-
- // -- advanced features ,未曾使用過,有待確認。
-
- //factory.printing.SetMarginMeasure(2); // measure margins in inches
-
- //factory.SetPageRange(false, 1, 3);// need pages from 1 to 3
-
- //factory.printing.printer = "HP DeskJet 870C";
-
- //factory.printing.copies = 2;
-
- //factory.printing.collate = true;
-
- //factory.printing.paperSize = "A4";
-
- //factory.printing.paperSource = "Manual feed"
-
- var header = (headerText==null||headerText=="")?'預設頁首':headerText;
-
- var footer = (footerText==null||footerText=="")?'預設頁角':footerText;
-
- factory.printing.header = "&b"+header+"&b" ;
-
- factory.printing.footer = "&b"+footer;
-
- factory.printing.portrait = true;
-
- factory.printing.leftMargin =10.00;
-
- factory.printing.topMargin =10.00;
-
- factory.printing.rightMargin =10.00;
-
- factory.printing.bottomMargin =10.00;
-
- }
function setPrintBase(headerText,footerText,rootUrl) { // -- advanced features ,未曾使用過,有待確認。 //factory.printing.SetMarginMeasure(2); // measure margins in inches //factory.SetPageRange(false, 1, 3);// need pages from 1 to 3 //factory.printing.printer = "HP DeskJet 870C"; //factory.printing.copies = 2; //factory.printing.collate = true; //factory.printing.paperSize = "A4"; //factory.printing.paperSource = "Manual feed" var header = (headerText==null||headerText=="")?'預設頁首':headerText; var footer = (footerText==null||footerText=="")?'預設頁角':footerText; factory.printing.header = "&b"+header+"&b" ; factory.printing.footer = "&b"+footer; factory.printing.portrait = true; factory.printing.leftMargin =10.00; factory.printing.topMargin =10.00; factory.printing.rightMargin =10.00; factory.printing.bottomMargin =10.00; }
例子Java代碼
- <%@ page contentType="text/html;charset=GBK"%>
-
- <html>
- <head>
- <meta http-equiv="imagetoolbar" content="no">
- <script language="javascript" src="print.js"></script>
- <style media="print">
- .Noprint {DISPLAY: none;}
- </style>
- <title>列印測試</title>
- </head>
- <OBJECT id="factory" style="DISPLAY: none" codeBase="smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT>
-
- <script defer>
- function window.onload() {
- setPrintBase('頁首','頁尾');
- }
- </script>
- <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
- <center class="Noprint">
- <input type=button value="列印" onclick="factory.printing.Print(true)">
- <input type=button value="版面設定" onclick="factory.printing.PageSetup()">
- <input type=button value="預覽列印" onclick="factory.printing.Preview()">
- <input type="button" value="關閉" onclick="window.close();">
- </center>
- <center>
- <table width="100%" border="0" cellpadding="0" cellspacing="0">
- <tr><td align="center"><b>內容</b></td></tr>
- </table>
- </center>
- </body>
- </html>
<%@ page contentType="text/html;charset=GBK"%> <html> <head> <meta http-equiv="imagetoolbar" content="no"> <script language="javascript" src="print.js"></script> <style media="print"> .Noprint {DISPLAY: none;} </style> <title>列印測試</title> </head> <OBJECT id="factory" style="DISPLAY: none" codeBase="smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT> <script defer> function window.onload() { setPrintBase('頁首','頁尾'); } </script> <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"> <center class="Noprint"> <input type=button value="列印" onclick="factory.printing.Print(true)"> <input type=button value="版面設定" onclick="factory.printing.PageSetup()"> <input type=button value="預覽列印" onclick="factory.printing.Preview()"> <input type="button" value="關閉" onclick="window.close();"> </center> <center> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr><td align="center"><b>內容</b></td></tr> </table> </center> </body> </html>
四.對比
1.Window.print調用方便,但功能簡單
2.功能更強大,但使用IEWebBrowser有時會報JS沒有許可權的錯誤。
3.ScriptX控制項功能也比較強大,目前在使用這種方式。