1

我使用下面的打印方法(发送我想打印的每个控件)但是这个方法不打印背景颜色和字体,...例如,如果我发送给它的控件的背景颜色是灰色的,这个方法可以就像背景颜色是白色并且不使用我在源代码管理中使用的字体一样

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();
    }
4

1 回答 1

1

由于您的代码,背景颜色不会变为白色。它是删除背景颜色和背景图像的浏览器。例如,如果您尝试打印此页面,您会注意到代码示例的灰色背景变为白色。

用户可以在浏览器级别控制背景颜色和图像的打印,例如在 IE 中,他们将按照以下步骤操作

您可以设置 CSS 打印样式表,这将使您可以更好地控制打印区域。

于 2013-01-10T07:51:05.053 回答