在线Word编辑的jQuery插件时间:2010-12-29 09:15点击:122 次 【大 中 小】 在做OA或者工作流程的网站中,常常能够看到一些在线Word编辑进行文档处理的功能,这里我开发了

在线Word编辑的jQuery插件

时间:2010-12-29 09:15 点击: 122 次 【

在做OA或者工作流程的网站中,常常能够看到一些在线Word编辑进行文档处理的功能,这里我开发了一个在线Word编辑插件并且以此为例。

1.NTKO Word在线编辑器介绍:

NTKO的官方网站:http://www.ntko.com/
NTKO OFFICE文档控件是拥有完全自主知识产权的ActiveX控件。使用NTKO Office文档控件,能够在浏览器中直接编辑Word,Excel等Office文档,或者WPS,金山电子表等文档并保存到web服务器。实现文档和电子表格的统一管理。NTKO OFFICE文档控件具有在线编辑,痕迹保留,手写签名,模板套红,安全电子印章[企业版提供],全屏批注,保存为HTML,MHT,PDF文件等办公自动化系统必备的功能。可以运行在PHP,ASP,JSP,C#,VB.NET,DOMINO等各种web编程语言和服务器。
 2.代码

/*  @author: Leepy
 *  @date:  2010-12
 *  @descript:  ntko word编辑器JQ插件
 
*/ 
(
    
function($) {
        $.ntko 
= $.ntko || {};

        $.fn.extend(
        {
            
//文件类型
            fileType: {
                Word: 
"Word.Document",
                Excel: 
"Excel.Sheet",
                PowerPoint: 
"PowerPoint.Show"
            },
            
//弹出窗类型
            showDialogType: {
                New: 
0,
                Open: 
1,
                Save: 
2,
                SaveCopy: 
3,
                Print: 
4,
                PageSetup: 
5,
                Properties: 
6
            },
            
//Word信息内容
            wordInfo: {
                Start: 
0,
                End: 
0,
                Text: 
"NDOFramer"
            },
            
//表单设置
            formSettings: {
                UploadFileId: 
"EDITFILE",
                Data: 
"savetype=1&fileType=",
                FileName: 
"demo.doc",
                FormId: 
0
            },
            _doc: 
null,
            _selection: 
null,
            _range: 
null,
            
//初始化插件
            ntko: function(parameters) {
                parameters 
= parameters || {};
                
var ndObj = null;
                
this.each(function(i) {
                    ndObj 
= $.ntko.init(this, parameters);
                });
                
return ndObj;
            },
            
//关闭插件
            close: function() {
                
this[0].close();
            },
            
//打开文档
            open: function(url) {
                
this[0].BeginOpenFromURL(url);   //第二个参数为只读设置
            },
            
//新建Word文档
            newWord: function() {
                
this[0].CreateNew(this.fileType.Word);
            },
            
//新建Excel文档
            newExcel: function() {
                
this[0].CreateNew(this.fileType.Excel);
            },
            
//新建PowerPoint文档
            newPowerPoint: function() {
                
this[0].CreateNew(this.fileType.PowerPoint);
            },
            
//打开本地文档
            openLocalFile: function() {
                
this[0].showdialog(this.showDialogType.Open);
            },
            
//保存到本地
            saveLocalFile: function() {
                
this[0].ActiveDocument.Application.Dialogs(83).Show()
            },
            
//保存文档,返回是否保存成功
            save: function(url, parameters) {

                
if (typeof (parameters.formId) !== "undefined")
                    
this.formSettings.FormId = parameters.formId;
                
if (typeof (parameters.data) !== "undefined")
                    
this.formSettings.Data = parameters.data;

                
var retHTML = this[0].saveToURL(url,    //提交到的url地址
                    this.formSettings.UploadFileId,     //文件域的id,类似<input type=file id=upLoadFile 中的id
                    this.formSettings.Data,             //与控件一起提交的参数,savetype参数为要保存的文件格式office,html,pdf。filetype参数保存文件类型
                    this.formSettings.FileName,         //上传文件的名称,类似<input type=file 的value
                    this.formSettings.FormId            //与控件一起提交的表单id,也可以是form的序列号,这里应该是0.
                );
                
return retHTML;
            },
            
//下载远程文件
            downloadFile: function(url, localPath) {
                
this[0].DownloadFile(url, localPath);
            },
            
//插入本地文档
            insertLocalFile: function() {
                
this[0].ActiveDocument.Application.Dialogs(164).Show();
            },
            
//插入文档
            insertFile: function(url) {
                
this._doc = this[0].ActiveDocument;
                
this._doc.Activate();
                
//                this._doc.Select();
                //                this._doc.Application.Selection.Select();

                
this[0].AddTemplateFromURL(url);
            },
            
//插入本地图片
            insertLocalImage: function() {
                
this[0].ActiveDocument.Application.Dialogs(163).Show();
            },
            
//插入图片
            insertImage: function(url, type) {
                
this[0].InsertFile(url, type + 8);
            },
            
//插入文本
            insertText: function(text) {
                
this._doc = this[0].ActiveDocument;
                
this._doc.Activate();
                
this._doc.Application.Selection.InsertAfter(text);
            },
            
//页面设置
            setPageSetup: function() {
                
this[0].showdialog(this.showDialogType.PageSetup);
            },
            
//文档属性设置
            setProperty: function() {
                
this[0].showdialog(this.showDialogType.Properties);
            },
            
//打印
            print: function() {
                
//                this[0].PrintOut();
                this[0].showdialog(this.showDialogType.Print);
            },
            
//打印预览
            printPreview: function() {
                
this[0].PrintPreview();
            },
            
//关闭打印预览
            printExit: function() {
                
this[0].PrintPreviewExit();
            },
            
//隐藏工具栏
            hideToolbar: function() {
                
this[0].Activate(false);
                
this[0].Toolbars = false;
            },
            
//显示工具栏
            showToolbar: function() {
                
this[0].Activate(false);
                
this[0].Toolbars = true;
            },
            
//隐藏菜单栏
            hideMenubar: function() {
                
this[0].Activate(false);
                
this[0].Menubar = false;
            },
            
//显示菜单栏
            showMenubar: function() {
                
this[0].Activate(false);
                
this[0].Menubar = true;
            },
            
//获取或设置窗口高度
            height: function(arg) {
                
if (typeof (arg) !== "undefined") {
                    
this.css("height",arg);
                }
                
else {
                    
return this.css("height"); ;
                }
            },
            
//获取或设置窗口宽度
            width: function(arg) {
                
if (typeof (arg) !== "undefined") {
                    
this.css("width", arg);
                }
                
else {
                    
return this.css("width"); ;
                }
            },
            
//删除本地路径,如c:\\1.doc
            deleteLocalFile: function(localPath) {
                
this[0].DeleteLocalFile(localPath);
            },
            showView: 
function(showViewType) {
                
this[0].Activate(false);
                
this[0].ActiveDocument.ActiveWindow.View = showViewType;
            },
            
//添加本地附加文件
            //            addLocalFile: function(fileID,locaPath) {
            //                this[0].HttpAddPostFile(fileID, locaPath);
            //            },
            //            hostName: function() {
            //                return this[0].HostName;
            //            },
            //            documentFullName: function() {
            //                return this[0].DocumentFullName;
            //            },
            //            caption: function() {
            //                return this[0].Caption;
            //            },
            //            track: function() {
            //                this[0].SetCurrUserName("liping");
            //                this[0].SetTrackRevisions(1);
            //            },
            //            showTrack: function() {
            //                this[0].ShowRevisions(1);
            //                this[0].ShowView(0);
            //            },
            //是否只读
            isReadOnly: function() {
                
return this[0].IsReadOnly;
            },
            
//文档是否已做修改
            isDirty: function() {
                
return !this[0].ActiveDocument.Saved;
            },
            
//获取文档所有内容,返回该Word文档内容Json
            range: function() {
                
this._doc = this[0].ActiveDocument;
                
this._doc.Activate();
                
this._range = this._doc.Range();

                
this.wordInfo.Start = this._range.Start;
                
this.wordInfo.End = this._range.End;
                
this.wordInfo.Text = this._doc.Range(this._range.Start, this._range.End).Text;

                
return this.wordInfo;
            },
            
//选中文档所有内容,返回该Word文档内容Json
            select: function() {
                
this._doc = this[0].ActiveDocument;
                
this._doc.Activate();
                
this._doc.Select();
                
this._selection = this._doc.Application.Selection;

                
this.wordInfo.Start = this._selection.Start;
                
this.wordInfo.End = this._selection.End;
                
this.wordInfo.Text = this._doc.Range(this._selection.Start, this._selection.End).Text;

                
return this.wordInfo;
            },
            
//取消选中内容
            unselect: function() {
                
if (this._selection != null) {
                    
this._selection.Start = 0;
                    
this._selection.End = 0;
                    
this._selection = null;
                }
            },
            replace: 
function(text, start, end) {

                
//                alert(this._doc.Range(start,end).Text);
                this._doc.Range(start, end).Text = text;
                
//                alert(this._doc.range(this.wordInfo.Starti,this.wordInfo.End).Text);
            },
            
//清除文本
            clear: function() {
                
var wordDoc = this[0].ActiveDocument;
                wordDoc.Activate();
                wordDoc.Select();
                
var wordSelection = wordDoc.Application.Selection;
                wordDoc.Range(wordSelection.Start, wordSelection.End).Text 
= "";
            }
        });

        $.extend($.ntko,
            {
                settings:
                {
                    folder: 
"",
                    width: 
"700px",
                    height: 
"500px",
                    id: 
"OFRAMER_" + new Date().getTime(),
                    isToolbar: 
true,
                    isMenubar: 
true,
                    failed: 
function() { }
                },
                init: 
function(obj, parameters) {
                    
//赋值参数
                    if (typeof (parameters.folder) !== "undefined")
                        
this.settings.folder = parameters.folder;
                    
if (typeof (parameters.width) !== "undefined")
                        
this.settings.width = parameters.width;
                    
if (typeof (parameters.height) !== "undefined")
                        
this.settings.height = parameters.height;
                    
if (typeof (parameters.id) !== "undefined")
                        
this.settings.id = parameters.id;
                    
if (typeof (parameters.isToolbar) !== "undefined")
                        
this.settings.isToolbar = parameters.isToolbar;
                    
if (typeof (parameters.isMenubar) !== "undefined")
                        
this.settings.isMenubar = parameters.isMenubar;
                    
if (typeof (parameters.failed) !== "undefined")
                        
this.settings.failed = parameters.failed || {};

                    
//构造插件Html
                    this._createHTML(obj);

                    
var acxId = this.settings.id;
                    
var acxJQjery = null;
                    
var failed = this.settings.failed;


                    
//判断是否安装了word编辑器插件
                    try {

                        document.getElementById(acxId).Close();
                        acxJQjery 
= $("#" + acxId);
                    }
                    
catch (e) {
                        failed(e);
                    }

                    
return acxJQjery;
                },
                
//构造插件Html
                _createHTML: function(obj) {
                    $(obj).html(
                        
this._stringFormat("<object classid=\"clsid:A39F1330-3322-4a1d-9BF0-0BA2BB90E970\" codebase=\"{0}/officecontrol.cab#version=5,0,0,6\"" +
                        "id=\"{1}\" width=\"{2}\" height=\"{3}\">" +
                        
"<param name=\"wmode\" value=\"transparent\">" +
                        
"<param name=\"_ExtentX\" value=\"16960\">" +
                        
"<param name=\"_ExtentY\" value=\"13600\">" +
                        
"<param name=\"BorderColor\" value=\"-2147483632\">" +
                        
"<param name=\"BackColor\" value=\"-2147483643\">" +
                        
"<param name=\"ForeColor\" value=\"-2147483640\">" +
                        
"<param name=\"TitlebarColor\" value=\"-2147483635\">" +
                        
"<param name=\"TitlebarTextColor\" value=\"-2147483634\">" +
                        
"<param name=\"BorderStyle\" value=\"0\">" +
                        
"<param name=\"Titlebar\" value=\"0\">" +
                        
"<param name=\"Statusbar\" value=\"0\">" +
                        
"<param name=\"Toolbars\" value=\"{4}\">" +
                        
"<param name=\"Menubar\" value=\"{5}\">" +
                        
"<param name=\"_ExtentX\" VALUE=\"18071\">" +
                        
"<param name=\"_ExtentY\" VALUE=\"20981\">" +
                        
"<param name=\"IsShowToolMenu\" VALUE=\"1\">" +
                        
"<param name=\"IsNoCopy\" VALUE=\"0\">" +
                        
"<param name=\"IsHiddenOpenURL\" VALUE=\"0\">" +
                        
"<param name=\"MaxUploadSize\" VALUE=\"0\">" +
                        
"<param name=\"FileNew\" VALUE=\"-1\">" +
                        
"<param name=\"FileOpen\" VALUE=\"-1\">" +
                        
"<param name=\"FileClose\" VALUE=\"-1\">" +
                        
"<param name=\"FileSave\" VALUE=\"-1\">" +
                        
"<param name=\"FileSaveAs\" VALUE=\"-1\">" +
                        
"<param name=\"FilePrint\" VALUE=\"-1\">" +
                        
"<param name=\"FilePrintPreview\" VALUE=\"-1\">" +
                        
"<param name=\"FilePageSetup\" VALUE=\"-1\">" +
                        
"<param name=\"FileProperties\" VALUE=\"-1\">" +
                        
"<param name=\"IsStrictNoCopy\" VALUE=\"0\">" +
                        
"<param name=\"IsUseUTF8URL\" VALUE=\"0\">" +
                        
"<param name=\"MenubarColor\" VALUE=\"-2147483643\">" +
                        
"<param name=\"IsUseControlAgent\" VALUE=\"0\">" +
                        
"<param name=\"IsUseUTF8Data\" VALUE=\"0\">" +
                        
"<param name=\"IsSaveDocExtention\" VALUE=\"0\">" +
                        
"<param name=\"IsDirectConnect\" VALUE=\"0\">" +
                        
"<param name=\"SignCursorType\" VALUE=\"0\">" +
                        
"<param name=\"IsResetToolbarsOnOpen\" VALUE=\"0\">" +
                        
"<param name=\"IsSaveDataIfHasVDS\" VALUE=\"0\">" +
                        
"</object>",
                        
this.settings.folder,
                        
this.settings.id,
                        
this.settings.width,
                        
this.settings.height,
                        
this.settings.isToolbar + "" == "true" ? "1" : "0",
                        
this.settings.isMenubar + "" == "true" ? "1" : "0"
                        ));
                },
                
//string格式化构造器
                _stringFormat: function(str) {
                    
var args = arguments;
                    
return str.replace(/\{(\d+)\}/g,
                        
function(m, i) {
                            
return args[parseInt(i) + 1];
                        });
                }
            }
        );
    }
)(jQuery);

3.这里要注意几个问题,首先在保存文档到远程服务器的时候,可以使用aspx页面或者ashx页面去处理。

具体调用的代码:jqObj.save("process.aspx?action=test&data=demo", { formId: "formeditor" });
可以发现我这里有个formId的参数,这个是因为我必须把Word插件必须放在Form标签中,这样在POST数据,也就是文件流的时候有用。
而在process.aspx页面中,的具体代码实现为:
其中,一定要记得写Response.ContentEncoding = Encoding.GetEncoding("GB2312");这段代码,如果没有写,返回的中文会出现乱码的情况。我们通过HttpFileCollection files = Request.Files;方式可以取得一个POST文件流。

代码
WebOffice是一款由北京点聚信息技术有限公司提供的完全免费(商业用途也免费)且功能强大的在线Word/excel/wps编辑辅助控件,可以实现: 1.在线编辑Word、Excel、PPT、WPS... ... 2.全面支持MS Office的界面定制,包括对于Office2007的全面支持 3.修订留痕 4.限制打印、保存、复制 5.直接保存到服务器,支持标准Http Post协议 6.强大的书签管理 7.套红、文档保护 8.模板管理 9.其他功能扩展 10.提供开发论坛http://www.dianju.cn/forum/,在其提供软件更新及问题回答服务 本控件不同于其它同类软件,不是基于微软的开放源码DsoFramer,也不基于OLE,可以解决DsoFramer及其派生产品的一些稳定性问题 本控件不同于其它同类收费软件,本控件完全免费和界面完全可控制。 点聚会定期更新本控件,并解答论坛上的询问,免费不代表免服务,每一个使用者都能得到及时服务。 安装包内包含: 1.WebOffice安装包(仅包含WebOffice控件) 2.WebOffice接口SDK 3.WebOffice网页接口调用例子 4.DES手写及签章系统(Office签章系统)试用版。 5.演示章及证书 6.点聚产品白皮书 WebOffice组件可以无缝集成点聚公司的电子签章和手写审批类产品。 点聚信息(http://www.dianju.com.cn)是国内专业的安全和文档间件软件制造商。公司注重自主知识产权产品的研发,始终坚持技术为本、服务为先的原则,在电子印章、手写签批、安全版式文档和电子表单等领域处于国内领先水平。 点聚自主研发的版式文件系统独创性的将整个系统缩小为1M大小的组件,并在多项重要技术指标上领先PDF和其它版式格式。 点聚支持全系列手写及签章解决方案,可同时提供Office签章系统(Word/Excel/Wps)、网页签章系统、版式签章系统. 点聚是国内唯一一家同时拥有国密和军密资质的电子印章及安全文档产品提供商(国密、军密、公安部销售许可)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值