Copy from MS Word and Paste to Editor

Sven runs the engineering side of a Scandinavian ISV that builds estimating and contract-notes software for mid-sized construction firms. His users are project managers who live in Microsoft Word. They draft scope-of-work paragraphs in Word, pull boilerplate from Outlook emails, and paste it all into Sven's WPF contract-notes tool roughly thirty times a day. Then they hit "Export to PDF" and ship the result to a customer.

The problem he was chasing for two weeks: the PDFs looked broken. Headings collapsed, bullet spacing went wrong, fonts shifted halfway through a paragraph. The culprit turned out to be Word's HTML output. When Word puts content on the clipboard, it includes MsoNormal classes, <o:p> namespace tags, conditional comments aimed at older Internet Explorer, and a forest of mso-* style properties. The PDF renderer choked on it.

Sven swapped to WpfHtmlEditor and the first thing he noticed was that this had been thought through. The control auto-detects Word content on the clipboard and strips the Word-specific noise before insertion. The flag is on by default, so he did not have to do anything to get the basic cleanup.

// Enabled by default; shown here for completeness.

editor.Options.AutoDetectWordPaste = true;
WpfHtmlEditor stripping Word markup automatically on paste

That fixed eighty percent of his PDF problems. The remaining twenty percent came from Sven's house style. His company's contracts had to ship in a single typeface (Calibri 11) and a single text color (black on white). His PMs would paste from emails written in red, blue, and the occasional comic sans, and the styling came along for the ride.

So Sven subscribed to the Pasting event. It fires after the Word cleanup has run, gives him the already-cleaned HTML, and lets him strip whatever he still does not want before the editor inserts it. He also wanted to cap pastes that exceeded two hundred kilobytes, because his PMs had a habit of pasting entire 40-page specs by accident.

public MainWindow()

{

    InitializeComponent();

    editor.Pasting += Editor_Pasting;

}



private void Editor_Pasting(object sender, PastingHtmlEventArgs e)

{

    if (e.IsPastingFromMsWord && !string.IsNullOrEmpty(e.PastingHtml))

    {

        // House style: drop inline colors and font-family choices.

        e.PastingHtml = Regex.Replace(e.PastingHtml, @"color\s*:\s*[^;""]+;?",        "");

        e.PastingHtml = Regex.Replace(e.PastingHtml, @"font-family\s*:\s*[^;""]+;?", "");

    }



    if (e.PastingHtml != null && e.PastingHtml.Length > 200_000)

        e.Cancel = true;

}
Pasting event handler enforcing house style and a size cap

The IsPastingFromMsWord flag on the event args told him which pastes came from Word so he could apply the rule selectively. Pastes from regular text editors, IDEs, or web pages passed through untouched.

One quiet detail Sven appreciated: the toolbar has a dedicated "Paste from Word" button. When a PM hits that explicit button, the editor runs the Word cleanup unconditionally, even if the clipboard does not look Word-flavored to the auto-detector. His training documentation now tells new PMs to use that button whenever they are not sure, and the support tickets about garbled PDFs dropped to zero.

Last updated on May 15, 2026