溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

asp.net中一實(shí)現(xiàn)按需打印

發(fā)布時(shí)間:2021-10-13 10:14:48 來(lái)源:億速云 閱讀:89 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)asp.net中一實(shí)現(xiàn)按需打印,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

過(guò)程很簡(jiǎn)單:

首先在asp.net頁(yè)面中設(shè)定開始打印和結(jié)束打印的標(biāo)記,為了確保該標(biāo)記不在網(wǎng)頁(yè)瀏覽時(shí)顯示,我們采用"<!-- HTML注釋 -->"的方式。比如:<!--startprint-->和<!--endprint-->。

接著寫相關(guān)的JavaScript代碼,具體代碼見(jiàn)后。

如果不想打印按鈕也被打印出來(lái),注意將打印按鈕包含在<!--startprint-->和<!--endprint-->之外。

相關(guān)代碼:

復(fù)制代碼 代碼如下:


<%@ 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;
}
h2,h3,h4{
    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"  id="TabBtnPrint">
    <tbody>
        <tr align="center">
            <td class="title"><strong>訴 求 信 息</strong></td>
        </tr>
        <tr>
            <td >
            <table cellspacing="1" cellpadding="2" >
                <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">訴求當(dāng)前狀態(tài):</td>
                        <td colspan="3">
                              <asp:Label ID="LblStatus" runat="server" Text="Label"/>
                        </td>
                    </tr>
                    <tr class="tdbg">
                        <td align="right"  nowrap="nowrap">訴求性質(zhì):</td>
                        <td ><asp:Label ID="lblAppealNature" runat="server" Text="Label"/></td>
                        <td align="right"  nowrap="nowrap">被投訴人姓名:</td>
                        <td ><asp:Label ID="lblBeAppealName" runat="server" Text="Label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right"  nowrap="nowrap">被投訴人地址:</td>
                        <td ><asp:Label ID="lblBeAppealAddress" runat="server" Text="Label"/></td>
                        <td align="right"  nowrap="nowrap">投訴人:</td>
                        <td ><asp:Label ID="lblUserName" runat="server" Text="Label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right"  nowrap="nowrap">訴求信息是否保密:</td>
                        <td ><asp:Label ID="lblIsSecret" runat="server" Text="Label"/></td>
                        <td align="right"  nowrap="nowrap">管理員是否同意公開:</td>
                        <td  align="right"><asp:Label ID="lblAdminIsPublic" runat="server" Text="Label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right"  nowrap="nowrap">瀏覽數(shù):</td>
                        <td ><asp:Label ID="lblHits" runat="server" Text="Label"/></td>
                        <td align="right"  nowrap="nowrap">回復(fù)數(shù):</td>
                        <td ><asp:Label ID="lblReply" runat="server" Text="Label"/></td>
                    </tr> 

                    <tr class="tdbg">
                        <td align="right"  nowrap="nowrap">歸檔:</td>
                        <td ><asp:Label ID="lblCategory" runat="server" Text=""/></td>
                        <td align="right"  nowrap="nowrap">訴求提交時(shí)間:</td>
                        <td ><asp:Label ID="lblCreateTime" runat="server" Text="Label"/></td>
                    </tr>


                    <tr class="tdbg">
                        <td align="right"  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">訴求內(nèi)容:</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" >
        <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>

觀察上面代碼可以看出,打印時(shí),window.document.body.innerHTML 被重新賦值成設(shè)定需要打印的網(wǎng)頁(yè)部分的代碼(這里是 prnhtml),然后通過(guò)遞歸方式調(diào)用setChildNodesByCurrentNode(artibody,size)更改相關(guān)子節(jié)點(diǎn)的樣式,以達(dá)到全部更換相關(guān)字體大小及樣式設(shè)置的目的。

關(guān)于“asp.net中一實(shí)現(xiàn)按需打印”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI