介绍一种Web上打印技术
生活随笔
收集整理的這篇文章主要介紹了
介绍一种Web上打印技术
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、??? 介紹ScriptX控件 對于B/S架構的項目中,Web打印最是讓程序員頭痛的技術,在這次項目中運用了第三方控件ScriptX解決了此技術包括各種匯總表,詳細清單等等,但最大弊端就是打印的格式是死的,你要打印出怎樣的格式必須自己來做,還有就是打印分頁,它是按你設置的紙張大小來分的,所以必須人為控制,ScriptX控件所需文件ScriptX.cab。(ScriptX下載地址http://www.meadroid.com/scriptx/freedep.asp ) 打印功能使用: ⑴ 用戶在客戶端單擊“打印”按鈕,第一次使用打印會彈出如下對話框 ⑵ 單擊“是”按鈕,此時會把控件ScriptX下載到本機 ⑶ 在打印窗口中有大標題、打印條件、打印列表,單擊“打印”按鈕進入打印瀏覽界面。 ⑷ 單擊“Print”按鈕,聽打印機聲音響起。 二、??? 一個簡單的ScriptX控件使用實例 1.界面文件? WebExample.aspx? (其中kg.css是界面樣式在這里就不介紹了) <%@ Page language="c#" Codebehind="WebExample.aspx.cs" AutoEventWireup="false" Inherits="WebPrint.WebExample" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>一個簡單的ScriptX打印</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <LINK href="kg.css" type="text/css" rel="stylesheet"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="WebExample" method="post" runat="server"> <TABLE width="100%" height="100%"> <tr> <td height="100"> </td> </tr> <tr> <td style="HEIGHT: 14px" align="center"> <div id="divTableResult" name="divTableResult"> <asp:Label id="Label1" runat="server" Width="232px" Height="32px" Font-Size="Large">歡迎使用ScriptX打印</asp:Label> </div> </td> </tr> <tr> <td align="center" valign="top"> <div id="divChartResult" name="divChartResult"> <asp:Image id="Image1" runat="server" ImageUrl="win2000.gif"></asp:Image> </div> </td> </tr> <tr> <td align="center" valign="top"> <input id="btnPrint" style="WIDTH: 32px; HEIGHT: 19px" οnclick="printResult();" type="button" size="1" value="打印" name="btnPrint"> </td> </tr> </TABLE> </form> <script language="javascript"> function printResult() { //針對不同模塊,設置打印參數 var title = 'ScriptX打印';//報表名稱 var orien = 'true';//打印頁面方向,true:縱向打印/false橫向打印 //參數設置結束 var strURL; title = escape(title); //condition = escape(condition); strURL = 'printContainer2.aspx?title='+title+'&orien='+orien; window.open(strURL,'printResult','height=600, width=800, top=0, left=50,toolbar=no , menubar=no, scrollbars=yes, resizable=no, location=no, status=no'); } </script> </body> </HTML> 說明: ⑴ <div id="divTableResult" name="divTableResult">和<div id="divChartResult" name="divChartResult"> 是在printContainer2.aspx文件中定義的傳值標簽,因此必須要有。 ⑵ printResult()方法用來聲明傳值內容,具體在方法中已說明 2.打印界面?? printContainer2.aspx (其中print.css是打印樣式就不介紹了) <%@ Page language="c#" Codebehind="printContainer2.aspx.cs" AutoEventWireup="false" Inherits="WebPrint.printContainer2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <% string title = "查詢打印"; ‘用于title獲取標題 if(Request.QueryString["title"].ToString().Trim()!=""){ title = Request.QueryString["title"].ToString().Trim(); } ‘用于orientation獲取打印頁面方向,true:縱向打印/false橫向打印 string orientation = "true"; if(Request.QueryString["orien"].ToString().Trim()!=""){ orientation =? Request.QueryString["orien"].ToString().Trim(); } %> <html> <head> <title>ScriptX 打印</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta http-equiv="content-type" content="text/html; charset=gb2312"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <link href="kg.css" rel="stylesheet" type="text/css"> <link href="print.css" rel="stylesheet" type="text/css"? media="print" > <script? FOR=document event="onclick"> if(event.srcElement.tagName=='A'){ //alert('a click!'); event.cancelBubble = true; } </script> <style type="text/css"> a:link {text-decoration:none} a:visited {text-decoration:none} a:active {text-decoration:none} a:hover {text-decoration:none} A { COLOR: #000000;TEXT-DECORATION: none; FONT-SIZE: 8pt;FONT-FAMILY: "";} </style> </head> <body MS_POSITIONING="GridLayout" > <!--這里調用控件ScriptX.cab--> <object id="factory" viewastext? style="display:none" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" codebase="ScriptX.cab#Version=6,1,431,2"> </object> <SCRIPT defer> //用于設置打印參數 function printBase() { factory.printing.footer = "&b ScriptX 打印 &b" ????? //頁眉 factory.printing.footer = ""??????? ???????????????????????? //頁腳 factory.printing.portrait = <%=orientation%>???????????? //true為縱向打印,flase為橫向打印 factory.printing.leftMargin = 1.5?????????????????????????????? //左頁邊距 factory.printing.topMargin = 0.5?????????????????????????????? //上頁邊距 factory.printing.rightMargin = 0.5???????????????????????????? //右頁邊距 factory.printing.bottomMargin = 1.0????????????????????????? //下頁邊距 } </SCRIPT> <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bordercolor="#FFFFFF"> <tr bordercolor="#FFFFFF"> <td > <table width="70%" border="0" cellspacing="0" cellpadding="2" align="center"> <tr> <td align="center"><font size="5" face="宋體"><b><div? name="divTitle" id="divTitle"></div></b></font></td> </tr> <tr> <td align="center"> <div id="divReport" name="divReport"> <input type="button" name="cmdPRINT" id="cmdPRINT" οnclick="printReport()" value="打印"> <input type="button" οnclick="window.close();"? value="關閉"> </div> </td> </tr> </table> </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="2" align="center"> <tr><td align="center"> <!--? 用于顯示divTableContainer標簽的值 --> <div id="divTableContainer"? name="divTableContainer"></div> </td></tr> <tr><td align="center"> <!--? 用于顯示divChartContainer標簽的值 --> <div id="divChartContainer"? name="divChartContainer"></div> </td></tr> </table> </body> <script language="javascript"> //使界面最大化 maxWin(); var needcon=''; document.all.divTitle.innerText = unescape('<%=title%>'); //用于把父窗體的divTableResult和divChartResult標簽的值分別轉入divTableContainer 和divChartContainer標簽中 var tableContent = window.opener.document.all.divTableResult.innerHTML; tableContent = replaceAll(tableContent,"<A ","<span "); tableContent = replaceAll(tableContent,"</A> ","</span> "); tableContent = replaceAll(tableContent,"<a ","<span "); tableContent = replaceAll(tableContent,"</a> ","</span> "); tableContent = replaceAll(tableContent,"<input ","<font "); tableContent = replaceAll(tableContent,"<INPUT ","<font "); document.all.divTableContainer.innerHTML = tableContent; document.all.divChartContainer.innerHTML = window.opener.document.all.divChartResult.innerHTML; //用于調用設置打印參數的方法和顯示預覽界面 function printReport(){ printBase(); //window.print(); factory.printing.Preview(); } function replaceAll( str, from, to ) { var idx = str.indexOf( from ); while ( idx > -1 ) { str = str.replace( from, to ); idx = str.indexOf( from ); } return str; } function maxWin() { var aw = screen.availWidth; var ah = screen.availHeight; window.moveTo(0, 0); window.resizeTo(aw, ah); } </script> </html> 三、??? 參考文件
源代碼
源代碼
總結
以上是生活随笔為你收集整理的介绍一种Web上打印技术的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到别人家办白事预示什么
- 下一篇: 分类法过时了吗?【ZZ】