开通VIP,畅享免费电子书等14项超值服
首页
好书
留言交流
下载APP
联系客服
2013.12.06
在项目开发中,经常会用到页面打印的功能,在ASP.NET环境下推荐一款web打印控件smsx.cab。
使用方法:一般会先定义一个用于打印的母版页(Print.Master),在母版页上做好布局,包括页面布局、js 的引用、smsx.cab控件加载、打印和预览按钮的放置。
下载链接:http://download.csdn.net/detail/nxgliming/6668385
母版页(Print.Master):
1 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Print.master.cs" Inherits="PdSecrity.Pages.Print.Print" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html xmlns="http://www.w3.org/1999/xhtml"> 5 <head runat="server"> 6 <title>打印</title> 7 <asp:ContentPlaceHolder ID="head" runat="server"> 8 </asp:ContentPlaceHolder> 9 <style media="print">10 .Noprint11 {12 display: none;13 }14 .w3cbbs15 {16 page-break-after: always;17 }18 </style>19 </head>20 <script src="<%= Page.ResolveClientUrl("~/PdSecrity/Scripts/jquery-1.10.2.min.js")%>"21 type="text/javascript"></script>22 <script src="<%= Page.ResolveClientUrl("~/PdSecrity/Scripts/Print.js")%>" type="text/javascript"></script>23 <body>24 <form id="form1" runat="server">25 <!--下面的这个objec标签就是加载t加载smsx.cab控件用-->26 <object id="factory" style="display: none" codebase="<%= Page.ResolveClientUrl("~/PdSecrity/Scripts/smsx.cab")%>#VVersion=6,6,440,26"27 classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext>28 </object>29 <script>30 $(document).ready(function () {31 //加载打印设置32 SetPrintSettings();33 });34 </script>35 <table align="center">36 <tr class="Noprint">37 <td>38 <input id="Button1" type="button" value="打印" class="btnbg" onclick="Print();" />39 <input id="Button2" type="button" value="预览" class="btnbg" onclick="Preview();" />40 </td>41 </tr>42 <tr>43 <td>44 <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">45 </asp:ContentPlaceHolder>46 </td>47 </tr>48 </table>49 </form>50 </body>51 </html>
Print.js,主页用于打印的一些设置
1 //预览 2 function Preview() { 3 document.all.factory.printing.Preview(); 4 } 5 6 function Print() { 7 document.all.factory.printing.Print(false); //无确认打印,true时打印前需进行确认 8 } 9 10 function PrintSetup() {11 document.all.factory.printing.PageSetup();12 }13 14 function SetPrintSettings() {15 // -------------------基本功能,可免费使用-----------------------16 document.all.factory.printing.header = ""; //页眉17 document.all.factory.printing.footer = ""; //页脚18 ////document.all.factory.printing.SetMarginMeasure(1); //页边距单位,1为毫米,2为英寸//边距设置,需要注意大部分打印机都不能进行零边距打印,即有一个边距的最小值,一般都是6毫米以上19 //设置边距的时候时候如果设置为零,就会自动调整为它的最小边距20 document.all.factory.printing.leftMargin = 7; //左边距21 document.all.factory.printing.topMargin = 7; //上边距22 document.all.factory.printing.rightMargin = 7; //右边距23 document.all.factory.printing.bottomMargin = 7; //下边距24 document.all.factory.printing.portrait = true; //是否纵向打印,横向打印为false25 //--------------------高级功能---------------------------------------------26 //document.all.factory.printing.printer = "EPSON LQ-1600KIII"; //指定使用的打印机27 //document.all.factory.printing.printer = "\\\\cosa-data\\HPLaserJ";//如为网络打印机,则需要进行字符转义28 ////document.all.factory.printing.paperSize = "A4"; //指定使用的纸张29 document.all.factory.printing.paperSource = "Manual feed"; //进纸方式,这里是手动进纸30 ////document.all.factory.printing.copies = 1; //打印份数31 ////document.all.factory.printing.printBackground = false; //是否打印背景图片32 ////document.all.factory.printing.SetPageRange(false, 1, 3); //打印1至3页33 //---------------------常用函数--------------------------------34 //document.all.factory.printing.Print(false); //无确认打印,true时打印前需进行确认35 //document.all.factory.printing.PrintSetup(); //打印设置36 //document.all.factory.printing.Preview(); //打印预览37 //document.all.factory.printing.WaitForSpoolingComplete(); //等待上一个打印任务完全送入打印池,在连续无确认打印时非常有用38 //document.all.factory.printing.EnumPrinters(index); //枚举已安装的所有打印机,主要用于生成打印机选择功能39 }
最后,让所需要打印的页面都引用这个母版页即可。
微信登录中...请勿关闭此页面