Example of Export to Word / PDF in ASP.NET MVC Rich Text Editor Control
This example illustrates how to use the import/export feature of the Rich Text Editor to convert the editor content into a PDF or Word document.
@* The editor's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "exportEditor",
Name = "Export",
Height = "400px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
ExportWord = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportWord", "RichTextEditorDemo"), FileName = "Project brief" },
ExportPdf = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportPdf", "RichTextEditorDemo"), FileName = "Project brief" },
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "exportEditor",
Name = "Export",
Height = "400px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
ExportWord = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportWord", "RichTextEditorDemo"), FileName = "Project brief" },
ExportPdf = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportPdf", "RichTextEditorDemo"), FileName = "Project brief" },
Value = (string)ViewData["RTEValue"]
})
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Newtonsoft.Json;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/ExportToWordPdf.cshtml; it draws what ViewData holds.
public ActionResult ExportToWordPdf()
{
ViewData["RTEValue"] = "<h1>Project brief</h1>" +
"<p>The <b>dashboard</b> replaces the weekly spreadsheet. See the <a href=\"https://example.com/plan\">plan</a> for the dates.</p>" +
"<h2>Goals</h2><ol><li>One place for the reports</li><li>Numbers straight from the warehouse<ul><li>Refreshed every hour</li></ul></li></ol>" +
"<h2>Owners</h2><table><tbody><tr><th>Area</th><th>Owner</th></tr><tr><td>Sales</td><td>Maria Santos</td></tr><tr><td>Finance</td><td>Ken Ito</td></tr></tbody></table>" +
"<p align=\"right\"><i>Draft, October 2026</i></p>";
ViewData["Tools"] = new List<string> { "ExportWord", "ExportPdf", "|", "Bold", "Italic", "Underline", "|", "Formats", "Alignments", "|", "OrderedList", "UnorderedList", "CreateTable", "CreateLink", "|", "Undo", "Redo" };
return View();
}
/// <summary>The Export sample's Word action (ExportWord.ServiceUrl): JSON { html, fileName } in, a .docx out.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ExportWord()
{
ExportRequest request = ReadExport();
if (request == null) { return new HttpStatusCodeResult(400, "No HTML was sent."); }
byte[] file;
try { file = UiRichTextWord.Export(request.Html, request.Stylesheet); }
catch (ArgumentException) { return new HttpStatusCodeResult(400, "The HTML is larger than 5 MB."); }
return File(file, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", FileName(request.FileName) + ".docx");
}
/// <summary>The Export sample's PDF action (ExportPdf.ServiceUrl): JSON { html, fileName } in, a PDF out.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ExportPdf()
{
ExportRequest request = ReadExport();
if (request == null) { return new HttpStatusCodeResult(400, "No HTML was sent."); }
byte[] file;
try { file = UiRichTextPdf.Export(request.Html, request.FileName, request.Stylesheet); }
catch (ArgumentException) { return new HttpStatusCodeResult(400, "The HTML is larger than 5 MB."); }
return File(file, "application/pdf", FileName(request.FileName) + ".pdf");
}
private ExportRequest ReadExport()
{
string body;
Request.InputStream.Position = 0;
using (var reader = new StreamReader(Request.InputStream)) { body = reader.ReadToEnd(); }
var request = JsonConvert.DeserializeObject<ExportRequest>(body);
return request == null || request.Html == null ? null : request;
}
private static string FileName(string name)
{
string safe = new string((name ?? string.Empty).Where(c => char.IsLetterOrDigit(c) || c == ' ' || c == '-' || c == '_').ToArray()).Trim();
return safe.Length == 0 ? "Document" : (safe.Length > 80 ? safe.Substring(0, 80) : safe);
}
private sealed class ExportRequest
{
public string Html { get; set; }
public string FileName { get; set; }
/// <summary>Syncfusion's export stylesheet (Phase 5b), when the editor has one.</summary>
public string Stylesheet { get; set; }
}
}
}
@* 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>
@* Rich Text Editors with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
This example illustrates how to use the import/export feature of the Rich Text Editor to export the editor content into a PDF or Word document.
- Clicking the export to PDF icon will export the content of the Rich Text Editor to a PDF document.
- Clicking the export to Word icon will export the content of the Rich Text Editor to a Word document.