我使用下面的方法打印(发送每个控件,我想打印到它)但是这个方法不打印背景颜色和字体,…例如,如果我发送给它的控件的背景色是灰色这个方法就像当背景色是白色并且不使用我在源代码控件中使用的字体
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.ApplicationInstance.CompleteRequest();
}
背景颜色没有因为你的代码而变成白色。是浏览器删除了背景颜色和背景图像。例如,如果您尝试打印此页,您将注意到代码示例的灰色背景变为白色。
用户可以在浏览器级别控制bg颜色和图像的打印,例如,在IE中,他们将遵循以下步骤
你可以设置一个CSS打印样式表,这将给你更多的控制打印区域