Pasting from MS Word and Outlook into the editor
Users copy content into your app from all kinds of places, but Word documents and Outlook emails are the worst offenders: paste a formatted email reply or an invoice drafted in Word and the clipboard brings along a wall of proprietary markup that clashes with your own CSS the moment the page renders. This page shows how the WinForms HTML Editor detects and cleans that markup automatically, and how to layer your own paste rules on top for cases the built-in cleanup does not cover.
The WinForms HTML editor detects when pasted content originates from Microsoft Word or Outlook and strips the markup that typically breaks rendering elsewhere: MsoNormal classes, inline mso-* styles, Office XML namespaces, and conditional comments. This cleanup runs automatically because Options.AutoDetectWordPaste defaults to true. Paste a Word document and read back DocumentHtml to confirm the output is clean of Word-specific markup.
<html xmlns:o="urn:schemas-microsoft-com:office:office"> <!--[if gte mso 9]><xml><o:OfficeDocumentSettings></o:OfficeDocumentSettings></xml><![endif]--> <p class="MsoNormal" style="margin:0in;mso-margin-top-alt:auto;font-size:14.0pt; font-family:Calibri;mso-fareast-language:EN-US">Hello <b>world</b></p> </html>
<p><b>Hello world</b></p>
The xmlns declaration, the conditional comment, the MsoNormal class, and every mso-* inline style are stripped - only the semantic markup and formatting survive.
By default, Options.MsIePasteBehavior is true: the editor delegates paste handling to the underlying browser engine, the AutoDetectWordPaste logic is bypassed entirely, and the Pasting event receives a null PastingHtml because the editor never sees the HTML the browser inserts. To get the automatic Word cleanup described above, set MsIePasteBehavior to false explicitly.
// Set once during composer initialization:
htmlEditor1.Options.AutoDetectWordPaste = true; // already the default
htmlEditor1.Options.MsIePasteBehavior = false; // must be set explicitly - the shipped default is trueTo enforce additional formatting rules on pasted content, for example stripping inline font declarations so the host application's own CSS takes over, subscribe to the Pasting event and rewrite e.PastingHtml before the editor inserts it.
htmlEditor1.Pasting += (sender, e) => {
if (string.IsNullOrEmpty(e.PastingHtml))
return;
// Strip inline font-family and font-size so the template's CSS wins.
e.PastingHtml = Regex.Replace(
e.PastingHtml,
@"(font-family|font-size)\s*:\s*[^;""]+;?",
string.Empty,
RegexOptions.IgnoreCase);
// e.IsPastingFromMsWord indicates the source was Word-shaped,
// so the handler applies Word-specific extra rules on top of the built-in cleanup.
if (e.IsPastingFromMsWord)
{
// The marketing team sometimes embeds clip-art via <v:shape>.
// Strip those - they never render in email clients anyway.
e.PastingHtml = Regex.Replace(e.PastingHtml, @"<v:[^>]*>.*?</v:[^>]*>",
string.Empty, RegexOptions.Singleline);
}
// Reject pastes that try to smuggle in <script> - security policy.
if (e.PastingHtml.IndexOf("<script", StringComparison.OrdinalIgnoreCase) >= 0)
e.Cancel = true;
};AddHandler htmlEditor1.Pasting, Sub(sender, e)
If String.IsNullOrEmpty(e.PastingHtml) Then Return
' Strip inline font-family and font-size so the template's CSS wins.
e.PastingHtml = Regex.Replace(
e.PastingHtml,
"(font-family|font-size)\s*:\s*[^;"""]+;?",
String.Empty,
RegexOptions.IgnoreCase)
' e.IsPastingFromMsWord indicates the source was Word-shaped,
' so the handler applies Word-specific extra rules on top of the built-in cleanup.
If e.IsPastingFromMsWord Then
' The marketing team sometimes embeds clip-art via <v:shape>.
' Strip those - they never render in email clients anyway.
e.PastingHtml = Regex.Replace(e.PastingHtml, "<v:[^>]*>.*?</v:[^>]*>",
String.Empty, RegexOptions.Singleline)
End If
' Reject pastes that try to smuggle in <script> - security policy.
If e.PastingHtml.IndexOf("<script", StringComparison.OrdinalIgnoreCase) >= 0 Then
e.Cancel = True
End If
End SubPastingHtml is read/write - replace it and the editor inserts your version. IsPastingFromMsWord flags Word-detected content, including content arriving via the legacy BtnPasteFromMsWord toolbar button. Cancel aborts the paste entirely. IsModified is a read-only flag indicating whether the handler changed the content.
(Ctrl+V or the Paste button)
MsIePasteBehavior = true
execCommand("Paste") - built-in
Word cleanup bypassed
e.PastingHtml is forced null
document as MSHTML inserted it
(set explicitly)
the clipboard for Word/Outlook
markers (MsoNormal, mso-*, xmlns)
FilterMsWordHtmlHelper
strips the Word cruft
cleaned HTML in e.PastingHtml,
e.IsPastingFromMsWord = true
e.PastingHtml or set e.Cancel
into the document
The toolbar's legacy "Paste from MS Word" button (BtnPasteFromMsWord) is redundant once automatic Word detection is enabled, since the regular paste action already covers the same case. Hide it to simplify the toolbar:
htmlEditor1.BtnPasteFromMsWord.Visible = false;htmlEditor1.BtnPasteFromMsWord.Visible = False