IE下实现打印功能

先贴代码:

<html>
<head>
    <style type="text/css">
        *{margin:0px;padding:0px;}
        .noprint{margin:20px 0px;}
        .noprint input{padding:5px 10px;margin:10px 5px;}
        #p{display:none;}
    </style>
    
    <!--该样式表设置的是打印时的样式,要放到其他style的下面,否则会被覆盖-->
    <style media="print">
        .noprint { display : none;}
        #p{display:block;}
    </style>

    <script>
        function doPrintSetup() {
            IEPrinter.setup();
        }
        function doPrintPreview() {
            IEPrinter.preview();
        }
        function doPrint() {
            IEPrinter.print();
        }
        
        ////边距设置时要注意数值单位
        var IEPrinter = (function(){
            var HKEY_Root, HKEY_Path, HKEY_Key
                ,HKEY_Root = "HKEY_CURRENT_USER"
                ,HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"
                ,Wsh,PRINT_SETTING={},printWB;
            
            window.onload= Init;
            
            ////保存默认值
            function Init(){
                ////生成Object标签
                
                var html = "<object id='printWB' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2' style='none'></object>";
                document.body.innerHTML += html;
                printWB = document.getElementById("printWB");
                try {
                    Wsh = new ActiveXObject("WScript.Shell");
                }catch (e) {
                    return alert("请使用IE浏览器");
                }
                
                //页眉
                PRINT_SETTING.Header = ReadReg("header");
                //页脚
                PRINT_SETTING.Footer = ReadReg("footer");
                //下页边距
                PRINT_SETTING.MarginBottom = ReadReg("margin_bottom");
                //左页边距
                PRINT_SETTING.MarginLeft = ReadReg("margin_left");
                HKEY_Key = "margin_right";
                //右页边距
                PRINT_SETTING.MarginRight = ReadReg("margin_right");
                //上页边距
                PRINT_SETTING.MarginTop = ReadReg("margin_top");
                //【启用缩小字体填充】yes|no
                PRINT_SETTING.ShrinkToFit = ReadReg("Shrink_To_Fit");
                //【打印背景颜色和图像】yes|no
                PRINT_SETTING.PrintBackground = ReadReg("Print_Background");
            }
            
            function SetPrintOpt(opt) {
                //设置页眉
                SetReg("header",opt.Header);
                //设置页脚
                SetReg("footer",opt.Footer);
                //设置下页边距
                SetReg("footer",opt.MarginBottom);
                //设置左页边距
                SetReg("margin_left",opt.MarginLeft);
                //设置右页边距
                SetReg("margin_right",opt.MarginRight);
                //设置上页边距
                SetReg("margin_top",opt.MarginTop);
                //设置【启用缩小字体填充】
                SetReg("Shrink_To_Fit",opt.ShrinkToFit);
                //设置【打印背景颜色和图像】
                SetReg("Print_Background",opt.PrintBackground);
            }
            
            function ReadReg(key){
                return Wsh.RegRead(HKEY_Root + HKEY_Path + key);
            }
            
            function SetReg(key,val){
                Wsh.RegWrite(HKEY_Root + HKEY_Path + key,val);
            }
            
            return {
                setInitOption:SetPrintOpt
                ////打开打印设置窗口
                ,setup:function(){
                    printWB.ExecWB(8, 1);
                }
                ,preview:function(){
                    printWB.ExecWB(7, 1);
                }
                ,print:function(){
                    printWB.ExecWB(6, 6);
                }
            };
        }());
    </script>
</head>
<body>
    <div id="noprint" class="noprint">
        <p>点击【打印】按钮前要更改浏览器设置:IE7及IE8用户,请先选择浏览器的“工具”-->“Internet选项”-->“安全”-->“自定义级别”中的</p>
        <p>“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”以及“下载未签名的ActiveX控件”设置为“启用”或“提示”。</p>
        <p>IE8以上用户,请先选择浏览器右上角的设置按钮然后选择“Internet选项”,后续操作同上。</p>
        <hr />
        
        <input type="button" value="打印设置" onclick="doPrintSetup();" />
        <input type="button" value="打印预览" onclick="doPrintPreview();" />
        <input type="button" value="打印准考证" onclick="doPrint();" />
    </div>
    <p id="p">ppp</p>
    <span>spannnn</span>
</body>
</html>

着重点有这么几个

1.

<style media="print">
   .noprint { display : none;}
   #p{display:block;}
</style>

该标签设置的样式只针对打印时的效果,所以可以设置某些打印时要隐藏,或者只有打印时才显示的内容。而且该标签最好放到其他样式表的后面,否则会被覆盖。

2.使用打印功能要对浏览器做相应的设置才可以。

IE7及IE8:“工具”-->“Internet选项”-->“安全”-->“自定义级别”中的“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”以及“下载未签名的ActiveX控件”设置为“启用”或“提示”。
IE8以上用户,请先选择浏览器右上角的设置按钮然后选择“Internet选项”,后续操作同上。

转载于:https://www.cnblogs.com/TiestoRay/p/3759896.html

IE下页面打印控件 类似于ScriptX控件,但本控件可免费使用 控件使用OCX技术,使控件加载到IE浏览器中 首次使用需要用户加载Activex控件,以后将自动加载运行 控件功能: 1.屏蔽IE打印时出现的打印设置框。 2.自动设置各打印控制参数。 3.实现自动打印。 使用方法 控件的ID为clsid:AE1A309B-6FFA-4FCF-B07F-CB97FFD56B1B 使用Object标签包裹即可。 如 <OBJECT ID="TestAX" classid="clsid:AE1A309B-6FFA-4FCF-B07F-CB97FFD56B1B" codebase="IEprint.ocx#version=" width=0 height=0 align=center hspace=0 vspace=0 ></OBJECT> 使用Javascript或VBScript程序进行调用 使用使,先得到打印控件Object对象实例如 a=document.getElementById("TestAX"); 参数内容 接着对a进行操作即可 a.Mar_left=0.0075; //左边界 a.Mar_Top=0.0075; //上边界 a.Mar_Right=0.0075; //右边界 a.Mar_Bottom=0.0075; //下边界 a.Orientation="纵向";//打印方向 a.Paper_Size="Folio"; //纸张大小 a.Header_Html="Headeraaaaaaaa"; //头部标注 a.Footer_Html="Footerssssssss"; //尾部标注 a.ApplySetup(); //应用上面的设置 函数方法 a.PrintWithOutSetup(); //无需设置即打印内容 a.PrintWithOutSetupPrintWithOutByID("cnnb"); //打印除ID为cnnb的其他内容。即过滤id为cnnb的所有内容 a.PrintWithOutSetupPrintByID("163");//打印id为163的内容。即过滤id为163以外的所有内容 a.PrintWithSetup(); //有设置选项的打印 a.PrintPreView();//打印预览 对本控件有建议的可以联系我,一般隐身存在。。。 chatop QQ948905
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值