欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net中的“按需打印”(打印你需要打印的部分) 实现代码

程序员文章站 2024-02-29 23:35:04
过程很简单: 首先在asp.net页面中设定开始打印和结束打印的标记,为了确保该标记不在网页浏览时显示,我们采用""的方式。比如...

过程很简单:

首先在asp.net页面中设定开始打印和结束打印的标记,为了确保该标记不在网页浏览时显示,我们采用"<!-- html注释 -->"的方式。比如:<!--startprint-->和<!--endprint-->。

接着写相关的javascript代码,具体代码见后。

如果不想打印按钮也被打印出来,注意将打印按钮包含在<!--startprint-->和<!--endprint-->之外。

相关代码:

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="appealprint.aspx.cs" inherits="appeal.appealprint"

masterpagefile="masterpage.master" title="诉求打印" %>

<asp:content id="content1" contentplaceholderid="cphnavigation" runat="server">
</asp:content>
<asp:content id="content2" contentplaceholderid="cphcontent" runat="server">
<!--startprint-->
<style type="text/css">
body,table{
    font-size:12px;
}
table{
    table-layout:fixed;
    empty-cells:show;
    border-collapse: collapse;
    margin:0 auto;
}
td{
    height:20px;
}
h1,h2,h3{
    font-size:12px;
    margin:0;
    padding:0;
}


table{
    border:1px solid #cad9ea;
    color:#666;
}
table th{
    height:30px;
}
table td,table th{
    border:1px solid #cad9ea;
    padding:0 1em 0;
}
table tr{
    background-color:#f5fafe;
}
</style>
   <table class="border" cellspacing="0" cellpadding="2" style="margin: 0px auto; width: 98%;border:1px solid;border-collapse:collapse;" id="tabbtnprint">
    <tbody>
        <tr align="center">
            <td class="title"><strong>诉 求 信 息</strong></td>
        </tr>
        <tr>
            <td style="height: 25px">
            <table cellspacing="1" cellpadding="2" style="margin: 0px auto; width: 100%;border-collapse:collapse;">
                <tbody>
                    <tr class="tdbg">
                         <td width="30%" align="right" nowrap="nowrap">诉求主题:</td>
                         <td colspan="3">
                             <asp:label id="lbledittitle" runat="server" text="label"></asp:label>
                         </td>
                    </tr>
                    <tr class="tdbg">
                        <td align="right" width="30%" nowrap="nowrap">诉求类型:</td>
                        <td  colspan="3">
                              <asp:label id="lblappealcategory" runat="server" text=""/>
                        </td>
                    </tr>
                    <tr class="tdbg">
                        <td align="right" width="30%" nowrap="nowrap">诉求当前状态:</td>
                        <td colspan="3">
                              <asp:label id="lblstatus" runat="server" text="label"/>
                        </td>
                    </tr>
                    <tr class="tdbg">
                        <td align="right" style="width: 30%" nowrap="nowrap">诉求性质:</td>
                        <td style="width: 15%"><asp:label id="lblappealnature" runat="server" text="label"/></td>
                        <td align="right" style="width: 25%" nowrap="nowrap">被投诉人姓名:</td>
                        <td style="width: 30%"><asp:label id="lblbeappealname" runat="server" text="label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right" style="width: 30%" nowrap="nowrap">被投诉人地址:</td>
                        <td style="width: 15%"><asp:label id="lblbeappealaddress" runat="server" text="label"/></td>
                        <td align="right" style="width: 25%" nowrap="nowrap">投诉人:</td>
                        <td style="width: 30%"><asp:label id="lblusername" runat="server" text="label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right" style="width: 30%" nowrap="nowrap">诉求信息是否保密:</td>
                        <td style="width: 15%"><asp:label id="lblissecret" runat="server" text="label"/></td>
                        <td align="right" style="width: 25%" nowrap="nowrap">管理员是否同意公开:</td>
                        <td style="width: 30%" align="right"><asp:label id="lbladminispublic" runat="server" text="label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right" style="width: 30%" nowrap="nowrap">浏览数:</td>
                        <td style="width: 15%"><asp:label id="lblhits" runat="server" text="label"/></td>
                        <td align="right" style="width: 25%" nowrap="nowrap">回复数:</td>
                        <td style="width: 30%"><asp:label id="lblreply" runat="server" text="label"/></td>
                    </tr> 

                    <tr class="tdbg">
                        <td align="right" style="width: 30%" nowrap="nowrap">归档:</td>
                        <td style="width: 15%"><asp:label id="lblcategory" runat="server" text=""/></td>
                        <td align="right" style="width: 25%" nowrap="nowrap">诉求提交时间:</td>
                        <td style="width: 30%"><asp:label id="lblcreatetime" runat="server" text="label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right" style="width: 30%" nowrap="nowrap">诉求所在地域:</td>
                        <td colspan="3"><asp:label id="lblregion" runat="server" text=""></asp:label></td>
                    </tr>
                    <tr class="tdbg">
                        <td colspan="4"><asp:label id="label2" runat="server" text=""/></td>
                    </tr>
                    <tr class="tdbg">
                        <td colspan="4" align="left" valign="top" nowrap="nowrap">诉求内容:</td>
                    </tr>
                    <tr class="tdbg">
                        <td colspan="4" align="left" valign="top">
                            <pe:extendedliteral id="ltreditcontent" runat="server" htmlencode="false"/>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<!--endprint-->
   <br />
    <div id="divbtnprint" style="text-align:center">
        <input id="btnprint" type="button" value="打 印…" onclick="doprint();" />
    </div>
    <br />
 <script type="text/javascript">

<!--

     function doprint(){
        bdhtml = window.document.body.innerhtml;
        sprnstr = "<!--startprint-->";
        eprnstr = "<!--endprint-->";
        prnhtml = bdhtml.substr(bdhtml.indexof(sprnstr) + 17);
        prnhtml = prnhtml.substring(0, prnhtml.indexof(eprnstr));
        window.document.body.innerhtml =  prnhtml;
        var obj = window.document.body;
        dozoom(obj, 18); //放大打印
        window.print();
    }

    function dozoom(artibody, size) {
        if (!artibody)
        {
          return;
        }
          setchildnodesbycurrentnode(artibody,size);
    }

    function setchildnodesbycurrentnode(node,size) {
        for(var i=0; i < node.childnodes.length; i++) {
            var artibodychild = node.childnodes[i];
            if (artibodychild.nodetype == 1) {
                artibodychild.classname="";
                artibodychild.style.fontsize = size + 'px';
                artibodychild.style.fontfamily = '仿宋_gb2312,宋体,"times new roman",georgia,serif';
                artibodychild.style.fontweight = 900;
                if(artibodychild.childnodes.length>0) {
                    setchildnodesbycurrentnode(artibodychild,size);
                }
            }
         }
      }

-->

 </script>
</asp:content>


观察上面代码可以看出,打印时,window.document.body.innerhtml 被重新赋值成设定需要打印的网页部分的代码(这里是 prnhtml),然后通过递归方式调用setchildnodesbycurrentnode(artibody,size)更改相关子节点的样式,以达到全部更换相关字体大小及样式设置的目的。