使用iWebOffice实现电子签章
摘要:随着信息化的发展电子签章已经越来越多的被用到很多OA系统中,今天就来看一下如何使用iWebOffice来实现电子签章功能。
内容:
1.iWebOffice2003的基本原理
2.使用iWebOffice2003实现电子签章
一、iWebOffice2003的基本原理
在开始今天的主题之前先简单的说一下iWebOffice的原理。iWebOffice控件由两部分组成:一个是用于集成在页面上的iWebOffice2003.ocx文件,另一个是运行在后台服务器上的iMsgServer2000.dll文件。前者就是大家所熟知的ActiveX插件,当然要使用它在客户端必须安装此插件,后者是用于配合前者的请求进行通信,事实上控件工作的原来正是前后台通信和处理的过程。iWebOffice2003.ocx集成在页面上,通过js脚本调用,用户文档的编辑以及传递消息到服务器;iMsgServer2000.dll在服务器端运行,用于解析iWebOffice2003.ocx控件发过来的信息包,以及将服务器上处理的结果反馈给客户端iWebOffice2003.ocx。
首先来看前端的代码,可以说同其他ActiveX几乎完全一样。classid是固定的,是控件的注册id,这也是所有com组件必须拥有的;codebase由两部分组成#version之前的是控件的下载地址,后面是控件版本号,这两者当然务必要写正确,否则会造成控件不能正常下载、显示和使用。
前端页面代码:
<OBJECT id="CWebOffice" width="100%" height="100%" classid="clsid:23739A7E-5741-4D1C-88D5-D50B18F7C347" codebase=" iWebOffice2003.ocx#version=6,0,0,4 " ></OBJECT>如果classid和version没有设置错的话运行会显示控件(前提是客户端安装了iWebOffice插件),当然此时还不能打开文档,因为前端还没有js调用。为了方便调用,这里将js操作部分加以封装(注意封装部分用到了Ext面向对象):
/*Author:KenshinCuiDate:2011.11.16Description:weboffice operate class. */ Ext.useShims=true; Ext.namespace("Cmj.Web.ExtExtend"); Cmj.Web.ExtExtend.WebOffice=Ext.extend(Ext.util.Observable,{constructor:function(config){ // this.addEvents("beforeopen"); // this.addEvents("open"); // this.addEvents("beforeprint"); // this.addEvents("print"); // this.addEvents("beforeopen"); // this.addEvents("open"); // this.addEvents("beforeopen"); // this.addEvents("open");this.listeners=config.listeners;Cmj.Web.ExtExtend.WebOffice.superclass.constructor.call(this,config);this.objectID=config.objectID;//客户端对象idthis.webUrl=config.webUrl;//weboffice 的服務器端操作urlthis.fileName=config.fileName;//文件名称(不包含完整路径并且后缀可有可无,如果没有后缀的话则必须配置fileType属性)if(config.fileType!=void 0){this.fileType=config.fileType;}else{this.fileType="";}this.webOffice=Ext.getDom(this.objectID);},getObject:function(){return this.webOffice;},load:function(){WebOffice=this.webOffice;try{WebOffice.WebUrl=this.webUrl;WebOffice.RecordID="cid";WebOffice.Template="";WebOffice.FileName=this.fileName;WebOffice.FileType=this.fileType;WebOffice.EditType="1";WebOffice.UserName="SYSTEM";WebOffice.WebOpen(); //打开该文档 window.status=WebOffice.Status;}catch(e){Ext.Msg.alert("系统提示","打开文档过程中发生错误,错误信息:"+e.message);}},showMenu:function(bool){try{this.webOffice.ShowMenu=bool?"1":"0";}catch(e){Ext.Msg.alert("系统提示","设置菜单显示状态时发生错误,错误信息:"+e.message);}},addMenus:function(menuArray){try{var menu=Ext.getDom('menuEventScript');for(var i=1;i<=menuArray.length;++i){if(menuArray[i-1]=="save"){this.webOffice.AppendMenu(i,"保存");menu.text+="if(Index=="+i+"){Ext.getDom('CWebOffice').WebSave();}"; }else if(menuArray[i-1]=="saveAs"){this.webOffice.AppendMenu(i,"另存为");menu.text+="if(Index=="+i+"){Ext.getDom('CWebOffice').WebSaveLocal();}"; }else if(menuArray[i-1]=="print"){this.webOffice.AppendMenu(i,"打印");menu.text+="if(Index=="+i+"){Ext.getDom('CWebOffice').WebOpenPrint();}"; }else if(menuArray[i-1]=="signature"){this.webOffice.AppendMenu(i,"电子签章");menu.text+="if(Index=="+i+"){Ext.getDom('CWebOffice').WebOpenSignature();}"; }}}catch(e){Ext.Msg.alert("系统提示","创建菜单过程中发生错误,错误信息:"+e.message);}},save:function(){//保存到服务器try{this.webOffice.WebSave();}catch(e){Ext.Msg.alert("系统提示","文档保存时发生错误,错误信息:"+e.message);}},saveAs:function(){//另存到客户端try{this.webOffice.WebSaveLocal();}catch(e){Ext.Msg.alert("系统提示","文档另存时发生错误,错误信息:"+e.message);}},print:function(){try{this.webOffice.WebOpenPrint();}catch(e){Ext.Msg.alert("系统提示","文档打印时发生错误,错误信息:"+e.message);}},signature:function(type){//打开电子签章操作窗口try{if(arguments.length>0){this.webOffice.WebOpenSignature(type);}else{this.webOffice.WebOpenSignature();}}catch(e){Ext.Msg.alert("系统提示","打开电子签章窗口时发生错误,错误信息:"+e.message);}}});
这里继承了Ext的Observable类方便对于事件管理(这里由于只是简单的演示因此事件部分注释掉了,需要的话可以加上)。首先在构造函数部分取得webOffice对象,getObject()方法用户向外部公开此对象。文档加载在load()方法中,从代码可以看出打开一个文档只需要设置WebUrl(后台http接口)、FileName(文件名)和Filetype(文件类型)属性,然后调用WebOpen()方法。值得一提的是addMenus()方法,这个方法用于创建菜单,但是注意菜单的创建是通过动态构建js动态创建的。正常使用菜单的方法是页面添加如下脚本(这里用到了控件的"OnMenuClick"事件):
<script language="javascript" for=WebOffice event="OnMenuClick(vIndex,vCaption)"> alert('编号:'+vIndex+'\n\r'+'条目:'+vCaption+'\n\r'+'请根据这些信息编写菜单具体功能');if (vIndex==10){ …}; if (vIndex==11){ …};… </script>然后通过调用WebOffice的AppendMenu()方法进行添加:
webform.WebOffice.AppendMenu("10","测试菜单一"); webform.WebOffice.AppendMenu("11","测试菜单二"); webform.WebOffice.AppendMenu("12","-"); webform.WebOffice.AppendMenu("13","测试菜单四"); webform.WebOffice.AppendMenu("14","测试菜单五"); //其中:“-”表示分割线,增加的顺序是在现有按钮的最前面。 webform.WebOffice.AppendMenu("主菜单",""); //添加主菜单: webform.WebOffice.AppendMenu("15","测试菜单七(&C)"); webform.WebOffice.AppendMenu("16","测试菜单八(&D)");在WebOffice类中这部分代码及判断逻辑由js动态构建,实现菜单的可定制化。
其他方法都比较简单,这里不再赘余。有了这个WebOffice.js类之后只需要简单调用即可:
Ext.onReady(function(){var menus=["save","saveAs","print","signature"];var webOffice=new Cmj.Web.ExtExtend.WebOffice({objectID:'CWebOffice',webUrl:'HTTP://<%=host %>/WebOffice/OfficeServer.aspx',fileName:'<%=fileName %>',fileType:'<%= fileType%>'});webOffice.addMenus(menus);webOffice.load();});前端代码完成了,但是服务器端如何配合前端处理呢,下面的服务器端处理类OfficeServer:
using System; using System.Data; using System.Configuration; using System.Collections; 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 DBstep;namespace DigitalSignature.WebOffice {public partial class OfficeServer : System.Web.UI.Page{private iMsgServer2000 msgServer = null;//服务器端weboffice控件private const string _FilePath = @"Document\";private const string _signatureFilePath = @"Signature\";private string mMarkList = "KenshinCui";private byte[] mFileBody = null;//签章信息private string mOption;private string mRecordID;private string mTemplate;private string mFileName;private string mFileType;private string mHtmlName;private string mDirectory;private string mLabelName;private string mImageName;private string mCommand;protected void Page_Load(object sender, EventArgs e){string mFilePath;string TemplateFilePath;msgServer = new iMsgServer2000();string currentPath = ".";msgServer.MsgVariant(Request.BinaryRead(Request.ContentLength));mFilePath = ConfigurationManager.AppSettings["TempFile"]; //取得文档的存放路径TemplateFilePath = ConfigurationManager.AppSettings["Template"]; //取得存放模版文件的路径if (mFilePath + "" == ""){mFilePath = Server.MapPath("/") +_FilePath;}currentPath = mFilePath;string n = msgServer.GetMsgByName("DBSTEP");if (n.Equals("DBSTEP")){mOption = msgServer.GetMsgByName("OPTION");if (mOption.Equals("LOADFILE")) //请求调用文档{mRecordID = msgServer.GetMsgByName("RECORDID"); //取得文档编号mFileName = msgServer.GetMsgByName("FILENAME"); //取得文档名称mFileType = msgServer.GetMsgByName("FILETYPE"); //取得文档类型mFilePath = mFilePath + mFileName; //取得文档完整路径和名称if (!Path.HasExtension(mFilePath)){mFilePath += mFileType;}msgServer.MsgTextClear();if (msgServer.MsgFileLoad(mFilePath)) //调入文档{msgServer.SetMsgByName("STATUS", "打开成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("打开失败!"); //设置错误信息}}else if (mOption.Equals("SAVEFILE")) //请求保存文档{mRecordID = msgServer.GetMsgByName("RECORDID"); //取得文档编号mFileName = msgServer.GetMsgByName("FILENAME"); //取得文档名称mFileType = msgServer.GetMsgByName("FILETYPE"); //取得文档类型mFilePath = mFilePath + mFileName; //取得文档完整路径和名称if (!Path.HasExtension(mFilePath)){mFilePath += mFileType;}msgServer.MsgTextClear();if (msgServer.MsgFileSave(mFilePath)) //保存文档内容{msgServer.SetMsgByName("STATUS", "保存成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("保存失败!" + mFileName); //设置错误信息}msgServer.MsgFileClear();}else if (mOption.Equals("LOADTEMPLATE")) //请求调用模板文档{mTemplate = msgServer.GetMsgByName("TEMPLATE"); //取得文档编号mFileType = msgServer.GetMsgByName("FILETYPE"); //取得文档类型mFilePath = currentPath;mCommand = msgServer.GetMsgByName("COMMAND");if (mCommand.Equals("INSERTFILE")){msgServer.MsgTextClear();if (msgServer.MsgFileLoad(mFilePath + mTemplate)) //调入模板文档{msgServer.SetMsgByName("STATUS", "打开模板成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("打开模板失败!"); //设置错误信息}}else{mFilePath = TemplateFilePath + mTemplate + mFileType; //取得文档完整路径和名称File.SetAttributes(mFilePath, FileAttributes.Normal);msgServer.MsgTextClear();if (msgServer.MsgFileLoad(mFilePath)) //调入模板文档{msgServer.SetMsgByName("STATUS", "打开模板成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("打开模板失败!"); //设置错误信息}File.SetAttributes(mFilePath, FileAttributes.ReadOnly | FileAttributes.Archive | FileAttributes.Compressed);}}else if (mOption.Equals("SAVETEMPLATE")) //请求保存模板文档{mTemplate = msgServer.GetMsgByName("TEMPLATE"); //取得文档编号mFileType = msgServer.GetMsgByName("FILETYPE"); //取得文档类型mFilePath = TemplateFilePath + mTemplate + mFileType; //取得文档完整路径和名称File.SetAttributes(mFilePath, FileAttributes.Normal);msgServer.MsgTextClear();if (msgServer.MsgFileSave(mFilePath)) //调入模板文档{msgServer.SetMsgByName("STATUS", "保存模板成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("保存模板失败!"); //设置错误信息}File.SetAttributes(mFilePath, FileAttributes.ReadOnly | FileAttributes.Archive | FileAttributes.Compressed);}else if (mOption.Equals("UPDATEFILE")) //请求保存文档{mRecordID = msgServer.GetMsgByName("RECORDID"); //取得文档编号mFileName = msgServer.GetMsgByName("FILENAME"); //取得文档名称mFileType = msgServer.GetMsgByName("FILETYPE"); //取得文档类型mFilePath = mFilePath + mFileName; //取得文档完整路径和名称msgServer.MsgTextClear();if (msgServer.MsgFileSave(mFilePath)) //保存文档内容{msgServer.SetMsgByName("STATUS", "保存成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("保存失败!"); //设置错误信息}msgServer.MsgFileClear();}else if (mOption.Equals("INSERTFILE")) //请求调用正文文档{mRecordID = msgServer.GetMsgByName("RECORDID"); //取得文档编号mFileName = msgServer.GetMsgByName("FILENAME"); //取得文档名称mFileType = msgServer.GetMsgByName("FILETYPE"); //取得文档类型mFilePath = mFilePath + mFileName; //取得文档完整路径和名称msgServer.MsgTextClear();if (msgServer.MsgFileLoad(mFilePath)) //调入文档{msgServer.SetMsgByName("STATUS", "插入文件成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("插入文件失败!"); //设置错误信息}}else if (mOption.Equals("INSERTIMAGE")) //请求调用正文文档{mFilePath = currentPath;mRecordID = msgServer.GetMsgByName("RECORDID"); //取得文档编号mLabelName = msgServer.GetMsgByName("LABLENAME"); //标签名mImageName = msgServer.GetMsgByName("IMAGENAME"); //图片名mFilePath = mFilePath + mImageName; //图片在服务器的完整路径mFileType = mImageName.Substring(mImageName.Length - 4, 4).ToLower(); //取得文件的类型msgServer.MsgTextClear();if (msgServer.MsgFileLoad(mFilePath)) //调入图片{msgServer.SetMsgByName("IMAGETYPE", mFileType); //指定图片的类型 msgServer.SetMsgByName("POSITION", mLabelName); //设置插入的位置[书签对象名] msgServer.SetMsgByName("STATUS", "插入图片成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("插入图片失败!"); //设置错误信息}}else if (mOption.Equals("SAVEASHTML")){mFilePath = currentPath;mHtmlName = msgServer.GetMsgByName("HTMLNAME"); //取得标签文档内容mDirectory = msgServer.GetMsgByName("DIRECTORY"); //取得标签文档内容msgServer.MsgTextClear();if (mDirectory.Equals("")){mFilePath = mFilePath + "\\HTML";}else{mFilePath = mFilePath + "\\HTML\\" + mDirectory;}msgServer.MakeDirectory(mFilePath);if (msgServer.MsgFileSave(mFilePath + "\\" + mHtmlName)){string txt = msgServer.MsgTextBody();byte[] biteBody = GetFileBody(mFilePath + "\\" + mHtmlName);Encoding encode = Encoding.GetEncoding("gb2312");string html = encode.GetString(biteBody);msgServer.MsgError(""); //清除错误信息msgServer.SetMsgByName("STATUS", "保存成功"); //设置状态信息}else{msgServer.MsgError("保存失败" + mFilePath + "\\" + mHtmlName); //设置错误信息}msgServer.MsgFileClear();}else if (mOption.Equals("SAVEIMAGE")){mFilePath = currentPath;mHtmlName = msgServer.GetMsgByName("HTMLNAME"); //取得标签文档内容mDirectory = msgServer.GetMsgByName("DIRECTORY"); //取得标签文档内容msgServer.MsgTextClear();if (mDirectory.Equals("")){mFilePath = mFilePath + "\\HTMLIMAGE";}else{mFilePath = mFilePath + "\\HTMLIMAGE\\" + mDirectory;}msgServer.MakeDirectory(mFilePath);if (msgServer.MsgFileSave(mFilePath + "\\" + mHtmlName)){msgServer.MsgError(""); //清除错误信息msgServer.SetMsgByName("STATUS", "保存成功"); //设置状态信息}else{msgServer.MsgError("保存失败"); //设置错误信息}msgServer.MsgFileClear();}else if (mOption.Equals("LOADMARKLIST"))//获取电子印章列表{if (LoadMarkList()){ //获得列表信息mMarkListmsgServer.SetMsgByName("MARKLIST", mMarkList); //显示签章列表(多个之间用换行符\r分割)msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("创建印章列表失败!"); //设置错误信息}}else if (mOption.Equals("LOADMARKIMAGE"))//打开印章文件{string mMarkName = msgServer.GetMsgByName("IMAGENAME"); //取得签名名称string mUserName = msgServer.GetMsgByName("USERNAME"); //取得用户名称string mPassword = msgServer.GetMsgByName("PASSWORD"); //取得用户密码msgServer.MsgTextClear(); //清除文本信息if (LoadMarkImage(mMarkName, mPassword)){//调入签名信息mFileBody//msgServer.SetMsgByName("IMAGETYPE", mFileType); //设置签名类型msgServer.SetMsgByName("IMAGETYPE", ".jpg"); //设置签名类型msgServer.MsgFileBody(mFileBody); //将签名信息打包msgServer.SetMsgByName("POSITION", "Manager"); //设置插入位置msgServer.SetMsgByName("STATUS", "打开成功!"); //设置状态信息msgServer.MsgError(""); //清除错误信息}else{msgServer.MsgError("签名或密码错误!"); //设置错误信息}}else if (mOption.Equals("SAVESIGNATURE"))//保存签章信息{//mRecordID = msgServer.GetMsgByName("RECORDID"); //取得文档编号//mFileName = msgServer.GetMsgByName("FILENAME"); //取得文件名称//mMarkName = msgServer.GetMsgByName("MARKNAME"); //取得签名名称//mUserName = msgServer.GetMsgByName("USERNAME"); //取得用户名称//mDateTime = msgServer.GetMsgByName("DATETIME"); //取得签名时间//mHostName = Request.getRemoteAddr(); //取得用户IP//mMarkGuid = msgServer.GetMsgByName("MARKGUID"); //取得唯一编号//msgServer.MsgTextClear(); //清除文本信息//if (SaveSignature())//{ //保存签章信息进数据库// msgServer.SetMsgByName("STATUS", "保存印章成功!"); //设置状态信息// msgServer.MsgError(""); //清除错误信息//}//else//{// msgServer.MsgError("保存印章失败!"); //设置错误信息//}}}else{msgServer.MsgError("Error:packet message");msgServer.MsgTextClear();msgServer.MsgFileClear();}Response.BinaryWrite(msgServer.MsgVariant());Response.End();}private bool LoadMarkImage(string mMarkName, object mPassword){if (Signature.HasRight(Session["userName"]+"",mPassword.ToString())){string picPath = Server.MapPath("/") + _signatureFilePath + mMarkName + ".jpg";mFileBody = File.ReadAllBytes(picPath);return true;}else{return false;}}private bool LoadMarkList(){string tMarkList = Signature.GetMaskList(); //印章列表if (tMarkList + "" != string.Empty){mMarkList = tMarkList;return true;}else{return false;}}private byte[] GetFileBody(string strFullFileName){byte[] file = new byte[0];FileInfo fInfo = new FileInfo(strFullFileName);if (fInfo.Exists == true){if ((fInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){File.SetAttributes(strFullFileName, FileAttributes.Archive);}}file = new byte[fInfo.Length];FileStream fs = new FileStream(strFullFileName, FileMode.Open);fs.Read(file, 0, (int)fInfo.Length);fs.Close();return file;}} }服务器端主要通过iMsgServer2000对象进行同前端的交互,前端执行某些操作时(注意是某些操作,并非所有操作都同服务器端交互,具体见iWebOffice白皮书)会向后端发送一个消息,服务器端通过iMsgServer2000对象的GetMsgByName()方法可以接收参数并根据不同参数配合前端做出相应的处理。例如加载文档时,当前端js调用WebOpen()方法时就会向服务器端传递一个名为"OPTION"的参数,通过这个参数可以判断要执行的操作类型,例如此时它的值等于"LOADFILE",服务器端就可以知道是要打开一个文档,当然此时也就知道它还会传递一些和打开文档相关的参数(例如FILENAME、FILETYPE)通过这些参数,服务器端构建一个完整的文件路径,然后调用MsgFileLoad()方法加载此文档,这时客户端就会打开此文档。当然,其他还有很多类似的操作,但是原理都是一样的,包括今天要说的电子签章。下面先看一下文档加载的后的截图:
二、使用iWebOffice2003实现电子签章
在iWebOffice中一个电子签章操作包括三个交互过程,分别是:加载签章列表(LOADMARKLIST)、加载签章文件(LOADMARKIMAGE)、保存签章信息(SAVESIGNATURE)。
LOADMARKLIST是加载签章列表的过程,在这个过程中服务器端需要通过iMsgServer2000对象的SetMsgByName()方法给客户端传递一个列表字符串(多个签章中间用换行符分割)。
LOADMARKIMAGE是加载签章文件的过程,有了签章名称客户端就可以选择相应的签章,但是选择了签章名称之后需要加载对应的签章文件(事实上是一个图片),加载签章文件的过程就发生这个阶段。在这个过程中服务器端的任务就是根据签章名称加载对应的文件,然后使用iMsgServer2000对象的MsgFileBody()方法将文件的字节流返回给客户端。注意这个过程中客户端需要输入密码,因此签章的权限验证部分可以在此进行处理。
SAVESIGNATURE是保存签章信息的过程,对于签章操作后的信息可以在此时进行保存(例如文档编号、文件名称、签章名称、签章日期等可以在此时记录到数据库),在本例中由于没有使用数据库因此就不对签章信息进行保存了。
这三个过程处理好之后,客户端使用时只需要调用WebOffice对象的WebOpenSignature()方法即可调出电子签章管理界面,下面是界面窗口:
通过上面的窗口可以看出,在iWebOffice中电子签章的不仅包括印章还包括签名、批注。从签章列表中选择一个签章,然后输入密码,点"盖章",此时就会从服务器端加载对应的签章文件:
点击确定就可以添加到文档中:
在看两张手写签名效果:
当然添加过签章之后需要对文档进行保存,不过注意文档保存到客户端还是服务器端,在本例中的"保存"是将文档在服务器端进行保存,"另存为"即保存到客户端。
总结
以上是生活随笔为你收集整理的使用iWebOffice实现电子签章的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: C语言段错误-core文件
- 下一篇: Node2vec原理剖析,代码实现