码迷,mamicode.com
首页 > 其他好文 > 详细

IE下实现打印功能

时间:2014-06-03 12:31:08      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

先贴代码:

bubuko.com,布布扣
<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>
bubuko.com,布布扣

着重点有这么几个

1.

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

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

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

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

IE下实现打印功能,布布扣,bubuko.com

IE下实现打印功能

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/TiestoRay/p/3759896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!