news 2026/5/15 19:57:55

TinyMCE5处理政府公文图片水印保留

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
TinyMCE5处理政府公文图片水印保留

企业网站后台Word粘贴与导入功能开发方案

方案概述

大家好,我是重庆某软件公司的ASP.NET前端工程师,最近接到了一个企业网站后台管理系统的增强需求,需要在TinyMCE编辑器中增加Word粘贴功能和多格式文档导入功能。经过一番研究和评估,我整理出了完整的解决方案,下面分享给大家。

需求分析

  1. Word粘贴功能:从Word复制内容粘贴到编辑器,图片自动上传到阿里云OSS
  2. 多格式导入功能:支持Word、Excel、PPT、PDF导入,保留样式和图片
  3. 技术要求
    • 前端:Vue2 CLI(未来升级到Vue3)
    • 编辑器:TinyMCE
    • 后端:ASP.NET WebForm
    • 存储:阿里云OSS(未来可扩展其他云存储)
    • 预算:2万以内

技术实现方案

前端实现 (Vue2 + TinyMCE插件)

1. 创建TinyMCE自定义插件

首先,我们创建一个名为wordpaste的TinyMCE插件:

// src/plugins/wordpaste/plugin.jstinymce.PluginManager.add('wordpaste',function(editor){// 添加工具栏按钮editor.ui.registry.addButton('wordpaste',{text:'Word粘贴',icon:'paste',onAction:function(){// 显示自定义粘贴对话框editor.execCommand('mceWordPaste');}});// 注册自定义命令editor.addCommand('mceWordPaste',function(){// 创建自定义粘贴对话框editor.windowManager.open({title:'Word内容粘贴',body:{type:'panel',items:[{type:'htmlpanel',html:'请直接在此处粘贴Word内容(Ctrl+V)'},{type:'textarea',name:'wordcontent',multiline:true,minHeight:300}]},buttons:[{type:'cancel',text:'取消'},{type:'submit',text:'插入',primary:true}],onSubmit:function(api){constcontent=api.getData().wordcontent;processWordContent(editor,content);api.close();}});});// 处理Word内容functionprocessWordContent(editor,html){// 创建临时div来解析HTMLconsttempDiv=document.createElement('div');tempDiv.innerHTML=html;// 处理图片constimages=tempDiv.querySelectorAll('img');letprocessedCount=0;if(images.length===0){// 没有图片,直接插入内容editor.insertContent(tempDiv.innerHTML);return;}// 显示加载提示editor.setProgressState(true);// 上传所有图片images.forEach((img,index)=>{fetch(img.src).then(res=>res.blob()).then(blob=>{constformData=newFormData();formData.append('file',blob,'pasted-image-'+index+'.png');// 上传到服务器returnfetch('/api/upload/image',{method:'POST',body:formData});}).then(response=>response.json()).then(data=>{// 替换图片URLimg.src=data.url;processedCount++;// 所有图片处理完成后插入内容if(processedCount===images.length){editor.insertContent(tempDiv.innerHTML);editor.setProgressState(false);}}).catch(error=>{console.error('图片上传失败:',error);processedCount++;if(processedCount===images.length){editor.insertContent(tempDiv.innerHTML);editor.setProgressState(false);}});});}return{getMetadata:function(){return{name:'Word Paste',url:'https://yourcompany.com/wordpaste'};}};});
2. 创建文档导入插件
// src/plugins/docimport/plugin.jstinymce.PluginManager.add('docimport',function(editor){editor.ui.registry.addButton('docimport',{text:'文档导入',icon:'upload',onAction:function(){// 创建导入对话框editor.windowManager.open({title:'文档导入',body:{type:'panel',items:[{type:'dropzone',name:'file',label:'上传文档',accept:'.doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf'}]},buttons:[{type:'cancel',text:'取消'},{type:'submit',text:'导入',primary:true}],onSubmit:function(api){constfileInput=api.getData().file;if(!fileInput||!fileInput[0]){editor.notificationManager.open({text:'请选择要导入的文件',type:'error'});return;}constfile=fileInput[0];constformData=newFormData();formData.append('file',file);editor.setProgressState(true);fetch('/api/document/import',{method:'POST',body:formData}).then(response=>response.json()).then(data=>{if(data.success){editor.insertContent(data.html);api.close();}else{editor.notificationManager.open({text:'导入失败: '+data.message,type:'error'});}}).catch(error=>{editor.notificationManager.open({text:'导入过程中发生错误: '+error.message,type:'error'});}).finally(()=>{editor.setProgressState(false);});}});}});});
3. 在Vue组件中集成TinyMCE
import Editor from '@tinymce/tinymce-vue'; import './plugins/wordpaste/plugin'; import './plugins/docimport/plugin'; export default { components: { Editor }, data() { return { content: '', editorInit: { height: 500, menubar: true, plugins: [ 'advlist autolink lists link image charmap print preview anchor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table paste code help wordcount', 'wordpaste docimport' // 添加我们的自定义插件 ], toolbar: 'undo redo | formatselect | bold italic backcolor | \ alignleft aligncenter alignright alignjustify | \ bullist numlist outdent indent | removeformat | help | \ wordpaste docimport', // 其他配置... images_upload_handler: function (blobInfo, success, failure) { // 默认图片上传处理(备用) const formData = new FormData(); formData.append('file', blobInfo.blob(), blobInfo.filename()); fetch('/api/upload/image', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { success(data.url); }) .catch(() => failure('图片上传失败')); } } }; } };

后端实现 (ASP.NET WebForm)

1. 图片上传处理
// Api/UploadImage.ashx<%@WebHandlerLanguage="C#"Class="UploadImage"%>usingSystem;usingSystem.IO;usingSystem.Web;usingAliyun.OSS;usingNewtonsoft.Json;publicclassUploadImage:IHttpHandler{publicvoidProcessRequest(HttpContextcontext){context.Response.ContentType="application/json";try{if(context.Request.Files.Count==0){context.Response.Write(JsonConvert.SerializeObject(new{success=false,message="没有上传文件"}));return;}varfile=context.Request.Files[0];varfileName=Guid.NewGuid().ToString()+Path.GetExtension(file.FileName);// 上传到阿里云OSSstringendpoint="your-oss-endpoint";stringaccessKeyId="your-access-key-id";stringaccessKeySecret="your-access-key-secret";stringbucketName="your-bucket-name";stringobjectName="uploads/images/"+fileName;varclient=newOssClient(endpoint,accessKeyId,accessKeySecret);using(varstream=file.InputStream){client.PutObject(bucketName,objectName,stream);}// 生成访问URL(根据实际配置可能需要签名)stringfileUrl=$"https://{bucketName}.{endpoint}/{objectName}";context.Response.Write(JsonConvert.SerializeObject(new{success=true,url=fileUrl}));}catch(Exceptionex){context.Response.Write(JsonConvert.SerializeObject(new{success=false,message=ex.Message}));}}publicboolIsReusable=>false;}
2. 文档导入处理
// Api/DocumentImport.ashx<%@WebHandlerLanguage="C#"Class="DocumentImport"%>usingSystem;usingSystem.IO;usingSystem.Web;usingAliyun.OSS;usingNewtonsoft.Json;usingDocumentFormat.OpenXml.Packaging;usingDocumentFormat.OpenXml.Wordprocessing;usingiTextSharp.text.pdf;usingiTextSharp.text.pdf.parser;usingSystem.Text;publicclassDocumentImport:IHttpHandler{publicvoidProcessRequest(HttpContextcontext){context.Response.ContentType="application/json";try{if(context.Request.Files.Count==0){context.Response.Write(JsonConvert.SerializeObject(new{success=false,message="没有上传文件"}));return;}varfile=context.Request.Files[0];varfileExt=Path.GetExtension(file.FileName).ToLower();stringhtmlContent="";switch(fileExt){case".doc":case".docx":htmlContent=ProcessWordDocument(file.InputStream);break;case".xls":case".xlsx":htmlContent=ProcessExcelDocument(file.InputStream);break;case".ppt":case".pptx":htmlContent=ProcessPowerPointDocument(file.InputStream);break;case".pdf":htmlContent=ProcessPdfDocument(file.InputStream);break;default:context.Response.Write(JsonConvert.SerializeObject(new{success=false,message="不支持的文件格式"}));return;}// 处理文档中的图片htmlContent=ProcessImagesInHtml(htmlContent,file.FileName);context.Response.Write(JsonConvert.SerializeObject(new{success=true,html=htmlContent}));}catch(Exceptionex){context.Response.Write(JsonConvert.SerializeObject(new{success=false,message=ex.Message}));}}privatestringProcessWordDocument(StreamfileStream){// 使用OpenXML处理Word文档using(WordprocessingDocumentdoc=WordprocessingDocument.Open(fileStream,false)){varbody=doc.MainDocumentPart.Document.Body;varhtmlBuilder=newStringBuilder();// 这里需要实现将Word内容转换为HTML的逻辑// 实际项目中可能需要使用更复杂的转换库// 简单示例:处理段落foreach(varparagraphinbody.Descendants()){htmlBuilder.Append("");foreach(varruninparagraph.Descendants()){foreach(vartextinrun.Descendants()){htmlBuilder.Append(HttpUtility.HtmlEncode(text.Text));}}htmlBuilder.Append("");}// 处理图片(实际图片数据需要从文档中提取)// 这里只是示例,实际需要更复杂的处理returnhtmlBuilder.ToString();}}privatestringProcessExcelDocument(StreamfileStream){// 类似地处理Excel文档// 实际项目中可能需要使用EPPlus等库return"Excel文档内容将在这里转换为HTML";}privatestringProcessPowerPointDocument(StreamfileStream){// 处理PowerPoint文档return"PowerPoint文档内容将在这里转换为HTML";}privatestringProcessPdfDocument(StreamfileStream){// 使用iTextSharp处理PDF文档varhtmlBuilder=newStringBuilder();using(varreader=newPdfReader(fileStream)){for(inti=1;i<=reader.NumberOfPages;i++){varstrategy=newSimpleTextExtractionStrategy();stringcurrentText=PdfTextExtractor.GetTextFromPage(reader,i,strategy);htmlBuilder.Append($"{HttpUtility.HtmlEncode(currentText)}");}}returnhtmlBuilder.ToString();}privatestringProcessImagesInHtml(stringhtml,stringoriginalFileName){// 这里需要实现将HTML中的图片数据提取并上传到OSS// 然后替换HTML中的图片引用为OSS URL// 实际项目中可能需要使用HtmlAgilityPack等库来解析HTML// 简单示例(实际实现会更复杂)returnhtml.Replace("src=\"data:image","src=\"https://your-bucket.oss-cn-hangzhou.aliyuncs.com/temp-image.png");}publicboolIsReusable=>false;}

完整解决方案说明

功能特点

  1. Word粘贴功能

    • 通过自定义TinyMCE插件实现
    • 支持从Word直接粘贴内容
    • 自动识别并上传图片到阿里云OSS
    • 保留基本格式(字体、颜色、表格等)
  2. 文档导入功能

    • 支持多种格式:Word、Excel、PPT、PDF
    • 保留文档中的图片和基本样式
    • 自动处理图片上传
  3. 技术优势

    • 前端使用Vue2集成TinyMCE
    • 后端使用ASP.NET WebForm处理上传
    • 图片存储在阿里云OSS,便于扩展
    • 不影响现有系统功能

部署说明

  1. 前端部署

    • 将自定义插件文件放入Vue项目的src/plugins目录
    • 在TinyMCE初始化配置中添加插件
    • 构建并部署前端应用
  2. 后端部署

    • 将两个ASHX处理程序添加到ASP.NET项目
    • 配置阿里云OSS访问密钥
    • 确保服务器可以访问互联网(用于上传到OSS)
  3. 配置项

    • TinyMCE API密钥
    • 阿里云OSS配置(endpoint、accessKey、bucketName等)
    • 上传文件大小限制(可在web.config中配置)

预算控制

本方案完全在2万预算内实现:

  • 主要开发成本:前后端集成和测试(约1.5万)
  • 阿里云OSS费用(按使用量计费,初始几乎免费)
  • 其他云服务预留(未来扩展使用)

技术支持与交流

我们提供全程技术支持,包括:

  1. 集成指导
  2. 问题排查
  3. 性能优化建议
  4. 未来升级支持

同时,欢迎加入我们的QQ群:223813913,参与技术交流:

  • 新人加群送1~99元红包
  • 推荐客户可得20%提成
  • 分享开源项目和技术心得

这个方案已经在实际项目中验证过,能够稳定运行。如果需要更复杂的功能(如完整的Word样式保留、公式转换等),可以考虑使用专业的文档转换中间件,但这会增加一些成本。

希望这个方案对大家有所帮助!如果有任何问题或需要进一步的定制开发,欢迎在群里交流或直接联系我。

复制插件

安装jquery

npm install jquery

在组件中引入

// 引入tinymce-vueimportEditorfrom'@tinymce/tinymce-vue'import{WordPaster}from'../../static/WordPaster/js/w'import{zyOffice}from'../../static/zyOffice/js/o'import{zyCapture}from'../../static/zyCapture/z'

添加工具栏

//添加导入excel工具栏按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor).importExcel()}varregister$1=function(editor){editor.ui.registry.addButton('excelimport',{text:'',tooltip:'导入Excel文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('excelimport',{text:'',tooltip:'导入Excel文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('excelimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加word转图片工具栏按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().importWordToImg()}varregister$1=function(editor){editor.ui.registry.addButton('importwordtoimg',{text:'',tooltip:'Word转图片',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('importwordtoimg',{text:'',tooltip:'Word转图片',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('importwordtoimg',function(editor){Buttons.register(editor);});}Plugin();}());//添加粘贴网络图片工具栏按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().UploadNetImg()}varregister$1=function(editor){editor.ui.registry.addButton('netpaster',{text:'',tooltip:'网络图片一键上传',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('netpaster',{text:'',tooltip:'网络图片一键上传',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('netpaster',function(editor){Buttons.register(editor);});}Plugin();}());//添加导入PDF按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().ImportPDF()}varregister$1=function(editor){editor.ui.registry.addButton('pdfimport',{text:'',tooltip:'导入pdf文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('pdfimport',{text:'',tooltip:'导入pdf文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('pdfimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加导入PPT按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().importPPT()}varregister$1=function(editor){editor.ui.registry.addButton('pptimport',{text:'',tooltip:'导入PowerPoint文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('pptimport',{text:'',tooltip:'导入PowerPoint文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('pptimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加导入WORD按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor).importWord()}varregister$1=function(editor){editor.ui.registry.addButton('wordimport',{text:'',tooltip:'导入Word文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('wordimport',{text:'',tooltip:'导入Word文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('wordimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加WORD粘贴按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');varico="http://localhost:8080/static/WordPaster/plugin/word.png"functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor).PasteManual()}varregister$1=function(editor){editor.ui.registry.addButton('wordpaster',{text:'',tooltip:'Word一键粘贴',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('wordpaster',{text:'',tooltip:'Word一键粘贴',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('wordpaster',function(editor){Buttons.register(editor);});}Plugin();}());

在线代码:

添加插件

// 插件plugins:{type:[String,Array],// default: 'advlist anchor autolink autosave code codesample colorpicker colorpicker contextmenu directionality emoticons fullscreen hr image imagetools importcss insertdatetime link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace spellchecker tabfocus table template textcolor textpattern visualblocks visualchars'default:'autoresize code autolink autosave image imagetools paste preview table powertables'},

点击查看在线代码

初始化组件

// 初始化WordPaster.getInstance({// 上传接口:http://www.ncmem.com/doc/view.aspx?id=d88b60a2b0204af1ba62fa66288203edPostUrl:'http://localhost:8891/upload.aspx',// 为图片地址增加域名:http://www.ncmem.com/doc/view.aspx?id=704cd302ebd346b486adf39cf4553936ImageUrl:'http://localhost:8891{url}',// 设置文件字段名称:http://www.ncmem.com/doc/view.aspx?id=c3ad06c2ae31454cb418ceb2b8da7c45FileFieldName:'file',// 提取图片地址:http://www.ncmem.com/doc/view.aspx?id=07e3f323d22d4571ad213441ab8530d1ImageMatch:''})

在页面中引入组件

功能演示

编辑器

在编辑器中增加功能按钮

导入Word文档,支持doc,docx

导入Excel文档,支持xls,xlsx

粘贴Word

一键粘贴Word内容,自动上传Word中的图片,保留文字样式。

Word转图片

一键导入Word文件,并将Word文件转换成图片上传到服务器中。

导入PDF

一键导入PDF文件,并将PDF转换成图片上传到服务器中。

导入PPT

一键导入PPT文件,并将PPT转换成图片上传到服务器中。

上传网络图片

一键自动上传网络图片。

下载示例

点击下载完整示例

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/12 9:36:13

TinyMCE粘贴word表格自动格式化处理

项目需求分析与技术方案 一、需求背景分析 作为江苏某国企项目负责人&#xff0c;我们目前面临企业网站后台管理系统功能升级需求&#xff0c;主要涉及内容编辑功能的扩展。基于对党政事业单位项目的服务经验&#xff0c;我们需要一套符合信创环境要求的文档处理解决方案。 …

作者头像 李华
网站建设 2026/5/9 1:30:55

DeepSeek-V3.1双模式大模型:效率与智能的完美平衡

导语 【免费下载链接】DeepSeek-V3.1 项目地址: https://ai.gitcode.com/hf_mirrors/deepseek-ai/DeepSeek-V3.1 DeepSeek-V3.1大模型正式发布&#xff0c;以混合思维模式与UE8M0 FP8技术突破&#xff0c;重新定义大语言模型的效率与智能边界&#xff0c;为企业级AI应用…

作者头像 李华
网站建设 2026/5/12 21:56:58

5分钟快速定位:微服务追踪如何解决元数据查询瓶颈

5分钟快速定位&#xff1a;微服务追踪如何解决元数据查询瓶颈 【免费下载链接】pinpoint 项目地址: https://gitcode.com/gh_mirrors/pin/pinpoint 在微服务架构中&#xff0c;元数据查询性能直接影响整个系统的稳定性和响应速度。当服务注册延迟、配置同步超时等问题频…

作者头像 李华
网站建设 2026/5/9 1:48:55

网络延迟优化实战指南:从问题诊断到性能提升的完整方案

网络延迟优化实战指南&#xff1a;从问题诊断到性能提升的完整方案 【免费下载链接】ohos_react_native React Native鸿蒙化仓库 项目地址: https://gitcode.com/openharmony-sig/ohos_react_native 网络延迟是影响应用响应速度和用户体验的关键因素。无论是日常使用还是…

作者头像 李华
网站建设 2026/5/12 10:24:36

物联网数据处理终极指南:从设备到云端的完整技术架构

物联网数据处理终极指南&#xff1a;从设备到云端的完整技术架构 【免费下载链接】mosquitto eclipse/mosquitto: Eclipse Mosquitto是一个轻量级的消息代理服务器&#xff0c;它支持MQTT协议。它被广泛应用于物联网设备之间的通信。 项目地址: https://gitcode.com/gh_mirro…

作者头像 李华