项目的Debug文件夹下有个template文件夹,里面有用到的js、自己建的要打印的网页和用到的背景图
1、打印方法:
class print { public void printzb(string bh){ //要打印的变量 string zwjyzsbh = "123456"; string zsfwdp = ""; string zsfwdc = ""; string zsfwdxian = ""; //获取打印背景图的地址 string pash=""; string pashimg=""; //这里用到了VelocityHelper模板 VelocityHelper vh = new VelocityHelper(); vh.Init(@"template");//初始化,指定模板文件的相对路径,就是工程里Debug文件里的路径 //put后,在网页就可以调用这些变量,就和spring的那一套流程类似,jsp通过jstl调用 vh.Put("title", "员工信息"); vh.Put("zwjyzsbh", zwjyzsbh); vh.Put("zsfwdp", zsfwdp); vh.Put("zsfwdc", zsfwdc); vh.Put("zsfwdxian", zsfwdxian); pash = Directory.GetCurrentDirectory();//工程的Debug所在目录 pash = pash.Replace("\\", "/"); pashimg = pash + "\\template\\JY_DYJY_SJZWJYZS.jpg"; pashimg=pashimg.Replace("\\", "/"); vh.Put("pash", pash); vh.Put("pashimg", pashimg); //新建窗体FrmWebBrowser,拉一个控件webBrowser,在该控件显示所要打印的页面page.htm String htm = vh.Display("page.htm"); FrmWebBrowser frmView = new FrmWebBrowser(); frmView.webBrowser1.DocumentText = htm; } }
2、要显示的页面:page.htm,(自己新建一个)
$title
顺便看下这个: http://blog.csdn.net/lovelylord/article/details/43405927
3、FrmWebBrowser窗体,用来显示要打印的页面
3、VelocityHelper模板类,用来生成html,需要引入NVelocity。不用改
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using NVelocity; using NVelocity.App; using NVelocity.Exception; using NVelocity.Runtime; using System.IO; using Commons.Collections; using NVelocity.Context; namespace gjjy { class NVelocityHelper { /// /// NVelocity模板工具类 VelocityHelper /// public class VelocityHelper { private VelocityEngine velocity = null; private IContext context = null; /// /// 构造函数 /// ///模板文件夹路径 public VelocityHelper(string templatDir) { Init(templatDir); } /// /// 无参数构造函数 /// public VelocityHelper() { ;} /// /// 初始话NVelocity模块 /// ///模板文件夹路径 public void Init(string templatDir) { //创建VelocityEngine实例对象 velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatDir); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); } /// /// 给模板变量赋值 /// ///模板变量 ///模板变量值 public void Put(string key, object value) { if (context == null) context = new VelocityContext(); context.Put(key, value); } /// /// 显示模板 /// ///模板文件名 public String Display(string templatFileName) { //从文件中读取模板 Template template = velocity.GetTemplate(templatFileName); //合并模板 StringWriter writer = new StringWriter(); template.Merge(context, writer); return writer.ToString(); } } } }
4、检查电脑是否已安装Lodop
public bool checkLodop() { Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); foreach (string subKeyName in uninstallNode.GetSubKeyNames()) { Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName); object displayName = subKey.GetValue("DisplayName"); if (displayName != null) { //MessageBox.Show(displayName.ToString()); if (displayName.ToString().Contains("C-Lodop(32-bit)")) { return true; //MessageBox.Show(displayName.ToString()); } } } return false; }
自动弹出安装界面
System.Diagnostics.Process.Start(Application.StartupPath + "\\" + "CLodopPrint_Setup_for_Win32NT_2.047.exe");