Example of Paste from MS Word in ASP.NET MVC Rich Text Editor Control
This example demonstrates the paste cleanup feature of the Rich Text Editor control. Copy your content from MS Word or other website, and paste it within the editor to cleanup.
Properties
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "pasteEditor",
PasteCleanupSettings = new UiRichTextPasteCleanup { Prompt = true },
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "pasteEditor",
PasteCleanupSettings = new UiRichTextPasteCleanup { Prompt = true },
Value = (string)ViewData["RTEValue"]
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row">
<label for="format">Format Option</label>
<select id="format">
<option value="Prompt">Prompt</option>
<option value="PlainText">Plain Text</option>
<option value="KeepFormat">Keep Format</option>
<option value="Cleanup">Clean Format</option>
</select>
</div>
<div class="demo-property-row"><label for="deniedTags">Denied Tags</label><input id="deniedTags" type="text" placeholder="img[!href], a[href, target]" /></div>
<div class="demo-property-row"><label for="deniedAttributes">Denied Attributes</label><input id="deniedAttributes" type="text" placeholder="id, title" /></div>
<div class="demo-property-row"><label for="allowedStyleProperties">Allowed Style Properties</label><input id="allowedStyleProperties" type="text" placeholder="color, margin" /></div>
</div>
<script>
var pasteEditor = function () { return window.Lumen.get('pasteEditor'); };
document.getElementById('format').addEventListener('change', function (e) {
pasteEditor().setSettings({ pasteCleanupSettings: { formatOption: e.target.value } });
});
document.getElementById('deniedTags').addEventListener('change', function (e) {
pasteEditor().setSettings({ pasteCleanupSettings: { deniedTags: e.target.value } });
});
document.getElementById('deniedAttributes').addEventListener('change', function (e) {
pasteEditor().setSettings({ pasteCleanupSettings: { deniedAttrs: e.target.value } });
});
document.getElementById('allowedStyleProperties').addEventListener('change', function (e) {
pasteEditor().setSettings({ pasteCleanupSettings: { allowedStyleProps: e.target.value } });
});
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/PasteFromMsWord.cshtml; it draws what ViewData holds.
public ActionResult PasteFromMsWord()
{
ViewData["RTEValue"] = "<h4>Paste Cleanup in Rich Text Editor<br></h4><p>The Rich Text Editor automatically <strong>cleans up formatted content</strong> when you paste from external sources like Word, Google Docs, or web pages.</p>" +
"<p><b>Paste Cleanup properties:</b></p><ul><li><strong>Prompt </strong>- specifies whether to enable the prompt when pasting in Rich Text Editor</li>" +
"<li><strong>Plain Text </strong>- specifies whether to paste as plain text or not in Rich Text Editor.</li>" +
"<li><strong>Keep Format</strong>- specifies whether to keep or remove the format when pasting in Rich Text Editor.</li>" +
"<li><strong>Denied Tags</strong> - specifies the tags to restrict when pasting in Rich Text Editor.</li>" +
"<li><strong>Denied Attributes</strong> - specifies the attributes to restrict when pasting in Rich Text Editor.</li></ul>" +
"<p><span><strong>Try It Out!</strong></span></p><p>Copy content from a web page or document and paste it here. The editor will display a prompt and allow you to input your options.</p>";
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The Rich Text Editor allows to paste the HTML content from MS Word or other websites. The editor cleans up the pasted HTML content by considering the following items.
- The unformatted HTML element (MS Office XML format) content to standard HTML elements, and its list paragraphs to real lists.
- The MS Office prefixed style properties are removed and what is left is made safe.
- The unwanted tags, CSS styles, and comments are removed from the copied content.
The following settings are available to clean up the content in the PasteCleanupSettings property:
- Select any option in the
Format Optiondrop down list for the pasted content.- Select the
Promptoption to invoke a prompt dialog with paste options on pasting the content in the editor. - Select the
Plain Textoption to paste the content as plain text. - Select the
Keep Formatoption to keep the same format in the copied content. - Select the
Clean Formatoption to remove the style format in the copied content.
- Select the
- Fill the
Denied Tagstext box to ignore the tags when pasting HTML content (their text is kept). For example:a[!href]– paste the content by filtering anchor tags that don’t have the ‘href’ attribute.a[href, target]– paste the content by filtering anchor tags that have the ‘href’ and ‘target’ attributes.
- Fill the
Denied Attributestext box to paste the content by filtering out these attributes from the content. For example:id, title– This will remove the attributes ‘id’ and ‘title’ from all tags.
- Fill the
Allowed Style Propertiestext box to paste the content by accepting these style properties and removing the others. For example:color, font-size– This will allow only the style properties ‘color’ and ‘font-size’ in each pasted element (the editor then keeps those of them its own style rules allow).