none
C#怎么做一个web的打印控件 RRS feed

全部回复

  • 你好,

    打印WEB页面的话,通常是在WEB 页面加入特定的标签,然后取出你定义的标检内的内容,在调用JS的打印方法来打印,下面有一个简单的例子,你可以参考一下。

    using System;
    
    using System.Data;
    
    using System.Configuration;
    
    using System.Web;
    
    using System.Web.Security;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    
    using System.Web.UI.WebControls.WebParts;
    
    using System.Web.UI.HtmlControls;
    
    using System.IO;
    
    using System.Text;
    
    using System.Web.SessionState;
    
     
    
    /// <summary>
    
    /// Summary description for PrintHelper
    
    /// </summary>
    
    public class PrintHelper
    
    {
    
        public PrintHelper()
    
        {
    
            //
    
            // TODO: Add constructor logic here
    
            //
    
        }
    
        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();
    
        }
    
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月17日 5:01
    版主
  • 之前用过activeX做过打印插件,局限在ie上使用。

    现在比较多的方案是直接在web上画出内容后,调用浏览器的页面 打印方法(用js调用)打印。效果也不错。


    family as water

    2018年4月19日 13:18