Skip to main content

Paste from Word in an ASP.NET Core rich text editor

The editor with a formatted document

People write in Word and then paste it into your site. Unfiltered, that brings pages of hidden markup, fonts you do not use and sizes that break your layout. RichTextBox cleans it as it arrives and keeps what matters.

The setting

Pasting from Word is handled by default. To say so explicitly, set the mode:

<richtextbox name="Body"
             toolbar="default"
             height="400px"
             paste-mode="Word"></richtextbox>
paste-modeWhat it does
AutoDecides from what was pasted
WordCleans Word's markup, keeps the structure
TextKeeps only the text
DisabledTurns pasting off

What survives

What goes

Word's own XML and conditional comments, the long mso- style rules, empty paragraphs and the like. You get back HTML a person could read.

Pasted pictures obey your rules

A picture pasted into the editor is uploaded like any other, so your size limit, allowed types and metadata removal all apply. If people's pictures fail to paste, the 4 MB default is the first thing to check.

Still clean it on the server

Paste cleaning happens in the browser for people using your page. It says nothing about a request sent some other way, so keep [RichTextHtml] on the property that receives the content.

Whole documents

For a full document, importing a .docx file is a better path than copy and paste. Add the importdocument button to your toolbar, and the server also has POST /richtextbox/import/docx. For the other direction, see export to Word.

Read next