項目的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,(自己新建一個)
<!DOCTYPE html> <HTML> <HEAD> <TITLE>$title</TITLE> <META http-equiv=Content-Type content="text/html; charset=UTF-8"> <META content="$title" name=description> <META content="$title" name=keywords> <script type="text/javascript" src="http://127.0.0.1:8000/CLodopfuncs.js"></script> <script type="text/javascript" src="$pash/template/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="$pash/template/bootstrap.min.js"></script> <script type="text/javascript" src="$pash/template/jquery.bootstrap.js"></script> <script type="text/javascript" src="$pash/template/jquery.form.js"></script> <script type="text/javascript" src="$pash/template/jquery.validate.min.js"></script> <script type="text/javascript" src="$pash/template/messages_bs_zh.js"></script> <script type="text/javascript" src="$pash/template/LodopFuncs.js"></script> <script> $(function () { LODOP = getLodop(); LODOP.NewPage(); LODOP.ADD_PRINT_TEXT(200, 150, 375, 22, "$zwjyzsbh");//(要打印的文本,前四個是坐標,最后是獲取前面put的變量) LODOP.ADD_PRINT_TEXT(170, 270, 75, 22, "$zsfwdp"); LODOP.ADD_PRINT_TEXT(170, 400, 75, 22, "$zsfwdc"); LODOP.ADD_PRINT_TEXT(170, 400, 75, 22, "$zsfwdxian"); LODOP.ADD_PRINT_SETUP_BKIMG("<img width='760' height='1122' border='0' src='$pashimg'>");//增加背景圖 LODOP.SET_SHOW_MODE("BKIMG_IN_PREVIEW", 1);//打印預覽是否顯示背景圖,1:顯示 LODOP.PREVIEW();//預覽 }); </script> </HEAD> <body> </body> </HTML>
順便看下這個: 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");
