C# C/S程序使用HTML文件作为打印模板

     C#   C/S程序使用HTML文件作为打印模板

      在网上找了一堆的资料,整理到郁闷呀,慢慢试慢慢改,哎,终于成功了,哈,菜鸟伤不起呀

     

 public partial class Print : Form
    {

       // 定义dgSetPage委托进行打印时的选项设置
        public delegate void dgSetPage();   

       //定义dgFileDelete 委托进行打印完成后,删除填充后的模板文件
        public delegate void dgFileDelete();

 

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
 
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
 

        //定义SendMessage方法内使用的鼠标单击 常量

        const int BM_CLICK = 0xF5;
 

 

        private void btnPrint_Click(object sender, EventArgs e)
        {
            btnPrint.Enabled = false;

 

           //因为是使用WebBrowser对象进行打印HTML文件,所以无法控制页面设置,需要使用注册表修改一些内容

           //方法 ChangePageSettingByRegist ,修改注册表,取消页眉、页角项目
            ChangePageSettingByRegist();

 

           //读取数据填充HTML模板
            string sFillDataResult=FillDataToNewFile();


            if (sFillDataResult.IndexOf("NG") >= 0)
            {
                MessageBox.Show(sFillDataResult);
                return;
            }

            pd_PrintPage(); 
            btnPrint.Enabled = true;
        }

        private void pd_PrintPage()
        {
            //创建一个WebBrowser对象,然后用它在后台打开并解释HTML文件
            WebBrowser webBrowserForPrinting = new WebBrowser();

   

           //  lblNewFile.Text  保存是填充后的模板文件名 
            webBrowserForPrinting.Url = new Uri(Application.StartupPath.ToString() + "\\" +  lblNewFile.Text);

            //当加载文件完成后激发手动追加的事件
            webBrowserForPrinting.DocumentCompleted +=   new WebBrowserDocumentCompletedEventHandler(PrintDocument);
            webBrowserForPrinting.Focus();
        }


        private void PrintDocument(object sender,WebBrowserDocumentCompletedEventArgs e)
        { 
            //创建一个新的线程,用于当弹出页面设置对话框时发送设置为横向的指令
            Thread th = new Thread(new ThreadStart(new dgSetPage(SetPage)));
            th.Start(); 

            //弹出页面设置对话框-----即上方定义的新进程需要处理的窗口
            ((WebBrowser)sender).ShowPageSetupDialog();

            //打印
            ((WebBrowser)sender).Print();
           
            //释放资源
            ((WebBrowser)sender).Dispose();

            //删除填充后的模板文件,防止重复打印
            System.IO.File.Delete(lblNewFile.Text);
            lblNewFile.Text = "";
        }

 

        //  此处方式被注释掉了,因为如果需要修改主窗口内控件的内容或者状况里需要使用委托调用修改方法

       //  需要时,把SetPage与SetPage2名称互换即可在变更后的名称为SetPage2内添加修改主窗口内容的代码
        //private void SetPage2()
        //{
        //    //新建委托对象
        //    MethodInvoker In = new MethodInvoker(SetPage2);
        //    this.BeginInvoke(In);
        //}

        private void SetPage()
        {
            int i = 0;

          //      此处需要设置循环查找时间,目前为超过10秒未找到结束查找
          while (true)
            {
                IntPtr WindownHand = FindWindow("#32770", "页面设置");
                if (WindownHand != IntPtr.Zero)
                {

                    //找到窗口内子项目的名柄,然后模拟鼠标左键单击事件
                    IntPtr Wk = FindWindowEx(WindownHand, IntPtr.Zero, null, "横向(&A)");
                    SendMessage(Wk, BM_CLICK, 0, 0);

                    IntPtr Wk1 = FindWindowEx(WindownHand, IntPtr.Zero, null, "确定");
                    SendMessage(Wk1, BM_CLICK, 0, 0);

 
                    break;
                }
                else
                {
                    if (i > 20)
                        break;
                    Thread.Sleep(500);
                    i++;
                }
            }

         

        }

        //修改注册表,把页眉页脚拿掉,然后设置为缩放至一页
        private void ChangePageSettingByRegist()
        {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"Software\Microsoft\Internet Explorer\PageSetup".ToUpper(), true);
            object A = (object)"";
            object B = (object)"0.5";
            object C = (object)"0";
            software.SetValue("header", A);
            software.SetValue("footer", A);
            software.SetValue("margin_bottom", B);
            software.SetValue("margin_left", C);
            software.SetValue("margin_right", C);
            software.SetValue("margin_top", C);
            software.SetValue("Shrink_To_Fit","yes");          

        }

 


HTML模板内容片段

     <%A%>  占位符,用于替换

    <table cellpadding="0" cellspacing="0" border="0px">
        <tr><td colspan="8"  class="EachPartSpace"></td></tr>
        <tr><td class="LayOutFieldTitle" colspan="8">Machine Protocol</td></tr>       
        <tr>
            <td class="FieldNameHorizontal Column1With">Purchase Order No:</td>
            <td class="FieldValueHorizontal Column2With"><%OrderNo%></td>
            <td class="FieldNameHorizontal Column1With">Unit No:</td>
            <td class="FieldValueHorizontal Column2With"><%UnitNo%></td>
            <td class="FieldNameHorizontal Column1With">Region:</td>
            <td class="FieldValueHorizontal Column6With"><%Region%></td>
            <td class="FieldNameHorizontal Column2With">Assembly date:</td>
            <td class="FieldValueHorizontal Column2With"><%AssemblyDate%></td>
        </tr>

 

C# C/S程序使用HTML文件作为打印模板

上一篇:Grizzly 2.3.17 API文档CHM版


下一篇:在线教育 (实时实现)