C# Lodop实现打印

时间:2022-01-21 04:07:21

项目的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("\\", "http://www.mamicode.com/"); pashimg = pash + "\\template\\JY_DYJY_SJZWJYZS.jpg"; pashimg=pashimg.Replace("\\", "http://www.mamicode.com/"); 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="http://www.mamicode.com/$pash/template/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="http://www.mamicode.com/$pash/template/bootstrap.min.js"></script> <script type="text/javascript" src="http://www.mamicode.com/$pash/template/jquery.bootstrap.js"></script> <script type="text/javascript" src="http://www.mamicode.com/$pash/template/jquery.form.js"></script> <script type="text/javascript" src="http://www.mamicode.com/$pash/template/jquery.validate.min.js"></script> <script type="text/javascript" src="http://www.mamicode.com/$pash/template/messages_bs_zh.js"></script> <script type="text/javascript" src="http://www.mamicode.com/$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=http://www.mamicode.com/‘$pashimg‘>");//增加背景图 LODOP.SET_SHOW_MODE("BKIMG_IN_PREVIEW", 1);//打印预览是否显示背景图,1:显示 LODOP.PREVIEW();//预览 }); </script> </HEAD> <body> </body> </HTML>

顺便看下这个: 

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