客户端打印页面内容的2种方法,
方法一:用js 来实现
代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript">
function printSpecial()
{
var html = '<HTML>\n<HEAD>\n';
if (document.getElementsByTagName != null)
{
var headTags = document.getElementsByTagName("head");
if (headTags.length > 0)
html += headTags[0].innerHTML;
}
html += '\n</HEAD>\n<BODY>\n<div style="height:150px;"></div>';
var printReadyElem = document.getElementById("printPart");
if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printPart div");
return;
}
html += '\n</BODY>\n</HTML>';
var printWin = window.open("","printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
printWin.print();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
<input type="button" value="Print Graph" onclick="printSpecial();" />
<div id="printPart" >
<asp:Chart ID="Chart1" runat="server" ImageType="Png" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"
Width="600px" Height="296px" BorderDashStyle="Solid" Palette="BrightPastel" BackGradientStyle="TopBottom"
BorderWidth="2" BackColor="#33DFC1" BorderColor="181, 64, 1">
<Legends>
<asp:Legend Enabled="False" Name="Default" BackColor="Transparent">
</asp:Legend>
</Legends>
<BorderSkin SkinStyle="Emboss"></BorderSkin>
<Series>
<asp:Series BorderWidth="3" XValueType="Double" Name="Default" ChartType="Line" ShadowColor="Transparent"
BorderColor="180, 26, 59, 105" Color="Red" YValueType="Double">
<Points>
<asp:DataPoint XValue="1" YValues="400" />
<asp:DataPoint XValue="2" YValues="200" />
<asp:DataPoint XValue="3" YValues="700" />
<asp:DataPoint XValue="4" YValues="300" />
<asp:DataPoint XValue="5" YValues="450" />
</Points>
</asp:Series>
<asp:Series BorderWidth="3" Name="Series2" Color="Blue" ChartType="Line" BorderColor="Green">
<Points>
<asp:DataPoint XValue="1" YValues="200" />
<asp:DataPoint XValue="2" YValues="300" />
<asp:DataPoint XValue="3" YValues="350" />
<asp:DataPoint XValue="4" YValues="80" />
<asp:DataPoint XValue="5" YValues="400" />
</Points>
</asp:Series>
<asp:Series BorderWidth="3" Name="Series3" Color="Green" ChartType="Line">
<Points>
<asp:DataPoint XValue="1" YValues="500" />
<asp:DataPoint XValue="2" YValues="120" />
<asp:DataPoint XValue="3" YValues="300" />
<asp:DataPoint XValue="4" YValues="50" />
<asp:DataPoint XValue="5" YValues="130" />
</Points>
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BackSecondaryColor="White"
BackColor="OldLace" ShadowColor="Transparent" BackGradientStyle="TopBottom">
<AxisY2>
<MajorGrid Enabled="False" />
</AxisY2>
<AxisX2>
<MajorGrid Enabled="False" />
</AxisX2>
<Area3DStyle PointGapDepth="0" Rotation="5" Perspective="10" Inclination="15" IsRightAngleAxes="False"
WallWidth="0" IsClustered="False"></Area3DStyle>
<AxisY LineColor="6, 64, 64, 64" IsLabelAutoFit="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="255, 0, 0, 0" />
</AxisY>
<AxisX LineColor="64, 64, 64, 64" IsLabelAutoFit="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="255, 0, 0, 0" />
<StripLines>
<asp:StripLine StripWidth="2" Interval="3" IntervalOffset="0.5" BackColor="64, 191, 191, 191">
</asp:StripLine>
</StripLines>
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</div>
</form>
</body>
</html>
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript">
function printSpecial()
{
var html = '<HTML>\n<HEAD>\n';
if (document.getElementsByTagName != null)
{
var headTags = document.getElementsByTagName("head");
if (headTags.length > 0)
html += headTags[0].innerHTML;
}
html += '\n</HEAD>\n<BODY>\n<div style="height:150px;"></div>';
var printReadyElem = document.getElementById("printPart");
if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printPart div");
return;
}
html += '\n</BODY>\n</HTML>';
var printWin = window.open("","printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
printWin.print();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
<input type="button" value="Print Graph" onclick="printSpecial();" />
<div id="printPart" >
<asp:Chart ID="Chart1" runat="server" ImageType="Png" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"
Width="600px" Height="296px" BorderDashStyle="Solid" Palette="BrightPastel" BackGradientStyle="TopBottom"
BorderWidth="2" BackColor="#33DFC1" BorderColor="181, 64, 1">
<Legends>
<asp:Legend Enabled="False" Name="Default" BackColor="Transparent">
</asp:Legend>
</Legends>
<BorderSkin SkinStyle="Emboss"></BorderSkin>
<Series>
<asp:Series BorderWidth="3" XValueType="Double" Name="Default" ChartType="Line" ShadowColor="Transparent"
BorderColor="180, 26, 59, 105" Color="Red" YValueType="Double">
<Points>
<asp:DataPoint XValue="1" YValues="400" />
<asp:DataPoint XValue="2" YValues="200" />
<asp:DataPoint XValue="3" YValues="700" />
<asp:DataPoint XValue="4" YValues="300" />
<asp:DataPoint XValue="5" YValues="450" />
</Points>
</asp:Series>
<asp:Series BorderWidth="3" Name="Series2" Color="Blue" ChartType="Line" BorderColor="Green">
<Points>
<asp:DataPoint XValue="1" YValues="200" />
<asp:DataPoint XValue="2" YValues="300" />
<asp:DataPoint XValue="3" YValues="350" />
<asp:DataPoint XValue="4" YValues="80" />
<asp:DataPoint XValue="5" YValues="400" />
</Points>
</asp:Series>
<asp:Series BorderWidth="3" Name="Series3" Color="Green" ChartType="Line">
<Points>
<asp:DataPoint XValue="1" YValues="500" />
<asp:DataPoint XValue="2" YValues="120" />
<asp:DataPoint XValue="3" YValues="300" />
<asp:DataPoint XValue="4" YValues="50" />
<asp:DataPoint XValue="5" YValues="130" />
</Points>
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BackSecondaryColor="White"
BackColor="OldLace" ShadowColor="Transparent" BackGradientStyle="TopBottom">
<AxisY2>
<MajorGrid Enabled="False" />
</AxisY2>
<AxisX2>
<MajorGrid Enabled="False" />
</AxisX2>
<Area3DStyle PointGapDepth="0" Rotation="5" Perspective="10" Inclination="15" IsRightAngleAxes="False"
WallWidth="0" IsClustered="False"></Area3DStyle>
<AxisY LineColor="6, 64, 64, 64" IsLabelAutoFit="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="255, 0, 0, 0" />
</AxisY>
<AxisX LineColor="64, 64, 64, 64" IsLabelAutoFit="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="255, 0, 0, 0" />
<StripLines>
<asp:StripLine StripWidth="2" Interval="3" IntervalOffset="0.5" BackColor="64, 191, 191, 191">
</asp:StripLine>
</StripLines>
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</div>
</form>
</body>
</html>
方法2:通过后台代码实现(使用MSChart, updatePanel 会有问题), 这种方法其实和方法一一样。
主页面前台代码
代码
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Test Print"></asp:Label>
</asp:Panel>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Test Print"></asp:Label>
</asp:Panel>
主页面后台代码
代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPrint_Click(object sender, EventArgs e)
{
Session["ctrl"] = this.Panel1;
ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=600px,width=800px,scrollbars=1');</script>");
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPrint_Click(object sender, EventArgs e)
{
Session["ctrl"] = this.Panel1;
ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=600px,width=800px,scrollbars=1');</script>");
}
}
打印页面后台代码:
代码
protected void Page_Load(object sender, EventArgs e)
{
PrintHelper.PrintWebControl((Control)Session["ctrl"]);
}
{
PrintHelper.PrintWebControl((Control)Session["ctrl"]);
}
打印方法:
代码
using System.Web.UI.DataVisualization.Charting;
/// <summary>
///PrintHelper 的摘要说明
/// </summary>
public class PrintHelper
{
public PrintHelper()
{
}
public static void PrintWebControl(Control ctrl)
{
PrintWebControl(ctrl, string.Empty);
}
public static void PrintWebControl(Control ctrl, string Script)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ctrl is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
if (Script != string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
}
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ctrl);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
}
/// <summary>
///PrintHelper 的摘要说明
/// </summary>
public class PrintHelper
{
public PrintHelper()
{
}
public static void PrintWebControl(Control ctrl)
{
PrintWebControl(ctrl, string.Empty);
}
public static void PrintWebControl(Control ctrl, string Script)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ctrl is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
if (Script != string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
}
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ctrl);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
}

浙公网安备 33010602011771号