asp.net中的“按需列印”(列印你需要列印的部分) 實現代碼

來源:互聯網
上載者:User

過程很簡單:

首先在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)更改相關子節點的樣式,以達到全部更換相關字型大小及樣式設定的目的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.