文档控件NTKO OFFICE 详细使用说明之预览word编辑保存回服务器
1、在线预览Word文件
(1) 运行环境
① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox)
② IE工具栏-Internet 选项:将www.ntko.com加入到浏览器的可信站点中,不启用保护模式,并将HTTPS验证取消
③ 如果是开发人员首次使用文档控件,需要将控件手动注册控件,获取控件的版本clisid以及版本信息Version(如果程序已经开发完则可以忽略此步骤)
(2) 创建一个加载控件的JS文件,用户只需修改控件版本号Version,classid,cab包路径,以及添加授权即可。具体可访问http://dev.ntko.com:8000/查看
① JS文件代码如下:(-1表示TRUE,0表示FALSE)
////----在线编辑word并保存 document.write('<object id="TANGER_OCX" classid="clsid:A64E3073-2016-4baf-A89D-FFE1FAA10EC0"'); document.write('codebase="NTKO/OfficeControl.cab#version=5,0,3,0" width="100%" height="100%"> '); document.write('<param name="Toolbars" value="-1"> '); // 工具栏 document.write('<param name="Menubar" value="0"> '); //菜单栏 document.write('<param name="CustomToolBar" value="-1"> '); document.write('<param name="Titlebar" value="-1"> '); //标题栏 document.write('<param name="Caption" value="华测计量技术有限公司专用">');//标题内容 document.write('<param name="IsUseControlAgent" value="-1"> '); //是否是控制请求数据,其他请他不允许 document.write('<param name="IsNoCopy" value="0"> '); //0不允许复制 document.write('<param name="IsDirectConnect" value="-1"> '); document.write('<param name="DefaultOpenDocType" value="1"> ');//1表示只能打开word文档 document.write('<param name="FileOpen" value="0"> '); document.write('<param name="FileNew" value="0"> '); document.write('<param name="FileSave" value="0"> '); document.write('<param name="FileSaveAs" value="0"> '); document.write('<param name="FilePrint" value="0"> '); document.write('<param name="FilePrintPreview" value="0"> '); document.write('<param name="ProductCaption" value="Office Anywhere 3.0"> '); document.write('<param name="ProductKey" value="65F867689D7FF5CD02FBE4AA23764306F715A9E2">'); document.write('<SPAN STYLE="color:red">不能装载NTKO OFFICE文档控件。请在检查浏览器的选项中检查浏览器的安全设置。</SPAN> '); document.write('</object> ');
① View代码如下:
@{ ViewBag.Title = "Word"; } <h2>Index</h2> <html> <head> <title>NTKO Office文档控件预览Word修改保存回服务器演示示例</title> <meta http-equiv="content-type" content="text/html; charset=gb2312"> </head> <body bgcolor="#ffffff" ;> <p> <span>针对Word书签的操作</span> <button onclick="AddPicToWord()">添加图片</button> <button onclick="ReplacePicToWord()">替换图片</button> <button onclick="AddTextToWord()">插入文字</button> <button onclick="ReplaceTextToWord()">替换指定的文字</button> <input type="submit" value="保存文档" id="_upload"> </p> <script src="~/Scripts/ntkoGenObj.js"></script> </body> </html> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script language="javascript"> //ntko控件初始化完成后才进行文档的加载 $(function () { inIt(); }); function inIt() { if (TANGER_OCX == null) { alert("NTKO OFFICE文档控件未能正确装载。请在本页面装载时窗口上方出现的提示条上点击装载ActiveX控件,并在随后出现的安装ActiveX的对话框中选择‘是’。"); return; } OpenFile(); } //以文件流的形式加载文件 function OpenFile() { TANGER_OCX.BeginOpenFromURL('@Url.Action("ReturnFileStream", "WordView")', true); } //保存修改后的文件回服务器 $("#_upload").click(function () { var list = TANGER_OCX.ActiveDocument.BookMarks;//书签列表 alert("书签数:" + list.Count); TANGER_OCX.SaveToURL('@Url.Action("uploadFile", "WordView")', "TANGER_OCX", "", "", "", true); }); var bookmarkname = "审核人"; var bookmarkname2 = "批准人"; //添加图片 function AddPicToWord() { //书签列表 var list = TANGER_OCX.ActiveDocument.BookMarks; alert("书签数为:" + list.Count); //判断书签是否存在 if (TANGER_OCX.ActiveDocument.BookMarks.Exists(bookmarkname) && TANGER_OCX.ActiveDocument.BookMarks.Exists(bookmarkname2)) { alert("书签存在,开始插入审核人签名"); TANGER_OCX.ActiveDocument.Application.Selection.GoTo(-1, 0, 0, bookmarkname);//跳转指定书签位置 TANGER_OCX.AddPicFromURL("@Url.Content("~/NTKO/william.jpg")", true, 0, 0, 1, 100, 1);//添加图片,true是以浮动图片插入,false就相当于嵌入在光标处了 ---插入正确 //AddIdtoPicture(bookmarkname);//为图片添加唯一标识(暂时用不到) alert("书签存在,开始插入批准人签名"); TANGER_OCX.ActiveDocument.Application.Selection.GoTo(-1, 0, 0, bookmarkname2);//跳转指定书签位置 TANGER_OCX.AddPicFromURL("@Url.Content("~/NTKO/jimly.jpg")", true, 0, 0, 1, 100, 1);//添加图片,true是以浮动图片插入,false就相当于嵌入在光标处了 ---插入正确 alert("图片添加成功"); } } //为图片列表中图片添加唯一标识(图片的AlternativeText属性可以设置为书签的名字name) function AddIdtoPicture(name) { alert("开始添加唯一标识" + name); var shapes = TANGER_OCX.ActiveDocument.Shapes; for (var i = 1; i <= shapes.Count; i++) { if (shapes(i).Type == 13) { if (shapes(i).AlternativeText == "") { alert(name); shapes(i).AlternativeText = name; } } } } //判断书签是否存在,然后删除图片,替换图片 function ReplacePicToWord() { var list = TANGER_OCX.ActiveDocument.BookMarks; alert("书签数为:" + list.Count); for (var i = 1; i <= list.Count; i++) { var markname = TANGER_OCX.ActiveDocument.BookMarks(i).Name; alert(markname); } if (TANGER_OCX.ActiveDocument.BookMarks.Exists(bookmarkname)) { alert("书签存在,开始替换审核人签名"); var shapes = TANGER_OCX.ActiveDocument.Shapes; alert("图片数量" + shapes.Count); for (var i = 1; i <= shapes.Count; i++) { alert(shapes(i).Type + shapes(i).AlternativeText); //根据图片的类型以及AlternativeText属性(书签的名字来确定图片) if (shapes(i).Type == 13 && shapes(i).AlternativeText == "william") { shapes(i).Delete(); alert("删除成功"); } } TANGER_OCX.ActiveDocument.Application.Selection.GoTo(-1, 0, 0, bookmarkname);//跳转指定书签位置 TANGER_OCX.AddPicFromURL("@Url.Content("~/NTKO/jimly.jpg")", true, 0, 0, 1, 100, 1);//添加图片,true是以浮动图片插入,false就相当于嵌入在光标处了 ---插入正确 } else { alert("书签不存在,请添加"); } } </script> <script> var bookName = "审核时间"; //在书签位置设置书签值 function AddTextToWord() { if (TANGER_OCX.ActiveDocument.BookMarks.Exists(bookName)) { //判断书签是否存在 TANGER_OCX.SetBookmarkValue(bookName, "2018/03/13"); //添加书签值 TANGER_OCX.ActiveDocument.Bookmarks(bookName).Range.Select();//选中书签的内容 //设置为上标 TANGER_OCX.ActiveDocument.Application.Selection.Font.SuperScript = 9999998; TANGER_OCX.ActiveDocument.Application.Selection.Font.Italic = true;//设置成斜体 TANGER_OCX.ActiveDocument.Application.Selection.Font.Bold = true;//加粗 TANGER_OCX.ActiveDocument.Application.Selection.Font.StrikeThrough = true;//带有删除线 TANGER_OCX.ActiveDocument.Application.Selection.Font.Underline = true;//带有下划线 TANGER_OCX.ActiveDocument.Application.Selection.Font.NameFarEast = "宋体";//字体 TANGER_OCX.ActiveDocument.Application.Selection.Font.Size = 18;//大小 TANGER_OCX.ActiveDocument.Application.Selection.Font.Color = 255;//颜色 //设置为下标 //TANGER_OCX.ActiveDocument.Application.Selection.Font.Subscript = 9999998; } } //判断书签是否存在,然后替换书签值(替换文本) function ReplaceTextToWord() { if (TANGER_OCX.ActiveDocument.BookMarks.Exists(bookName)) { TANGER_OCX.SetBookmarkValue(bookName, "YYYYYYYYYYYY"); } } //替换文当中所有内容和Text值相等的值(暂时用不着) function Searchs() { var sel = null; sel = TANGER_OCX.ActiveDocument.Application.Selection; var result = true; sel.Find.ClearFormatting(); sel.Find.Text = "王永乐"; sel.Find.Forward = true; sel.Find.Wrap = 1; do { result = sel.Find.Execute(); if (result) { sel.Font.Color = 255; sel.Font.Name = "Arial"; sel.Font.Size = 24; } } while (result); } </script>
① Controller代码如下:
/// <summary> /// 打开文件(流的形式返回) /// </summary> /// <returns></returns> public ActionResult ReturnFileStream() { var bytes = Properties.Resources.word; var fileStream = new MemoryStream(bytes); return new FileStreamResult(fileStream,"application/xhtml+xml"); } /// <summary> /// 保存文件 /// </summary> /// <returns></returns> [HttpPost] public ActionResult uploadFile() { var files = Request.Files[0]; var path = Server.MapPath($"~/{DateTime.Now.ToLongDateString()}.doc"); FileStream fs = new FileStream(path, FileMode.OpenOrCreate); byte[] bytes = new byte[files.InputStream.Length]; files.InputStream.Read(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); fs.Close(); return View("Index"); }