Word (RTF) Document Conversion

Use the Word Processing Document API to convert a document to various formats. Click Select File and load a document in RTF, DOC, DOCX, TXT, MHT, ODT, XML, EPUB, or HTML format. Select an output format from the list and click Download to save the result. The maximum file size you can upload is 1MB.

Convert file to:
@model AspNetCoreDemos.OfficeFileAPI.WordRTFDocumentConversionModel
@using DevExtreme.AspNet.Mvc

@using (Html.BeginForm("WordRTFConvertTo", "RichEditDocumentConversion", FormMethod.Post)) {
    @Html.HiddenFor(model => model.DocumentUrl)

    <div class="uploader-panel">
        @(Html.DevExtreme().FileUploader()
                           .ID("file-uploader")
                           .Name("myFile")
                           .Multiple(false)
                           .ShowFileList(true)
                           .Accept(".rtf, .doc, .docx, .txt, .mht, .odt, .xml, .epub, .htm, .html")
                           .MaxFileSize(1048576)
                           .UploadMode(FileUploadMode.Instantly)
                           .UploadUrl(Url.Action("DocumentUpload", "RichEditDocumentConversion"))
                           .OnValueChanged("fileUploader_valueChanged")
        )
    </div>

    <div class="document-downloader">
        <div class="document-downloader-item">Convert file to:</div>
        <div class="document-downloader-format-selector">
            @(Html.DevExtreme().SelectBoxFor(m => m.FileFormat)
                               .DataSource(Html.GetEnumSelectList<AspNetCoreDemos.OfficeFileAPI.RichEditFileFormat>()
                               .Select(i => new { Value = int.Parse(i.Value), Text = i.Text }))
                               .ValueExpr("Value")
                               .DisplayExpr("Text")
            )
        </div>
        <div class="document-downloader-item">
            @(Html.DevExtreme().Button()
                               .ID("downloadButton")
                               .Text("Download")
                               .Disabled(true)
                               .Type(ButtonType.Default)
                               .StylingMode(ButtonStylingMode.Contained)
                               .UseSubmitBehavior(true))
        </div>
    </div>

    <script>
        function fileUploader_valueChanged(e) {
            var files = e.value;
            if (files.length > 0) {
                $("#selected-files .selected-item").remove();

                $.each(files, function (i, file) {
                    var $selectedItem = $("<div />").addClass("selected-item");
                    $selectedItem.append(file.name);
                    $selectedItem.appendTo($("#selected-files"));
                });

                $("#selected-files").show();

                $('#DocumentUrl').val(files[0].name);
                var button = $("#downloadButton").dxButton("instance");
                button.option("disabled", false);
            }
            else
                $("#selected-files").hide();
        }
    </script>
}

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;

namespace AspNetCoreDemos.OfficeFileAPI {
    public class DocumentConversionController : DocumentProcessingController {

        public IActionResult WordRTFConversion() {
            WordRTFDocumentConversionModel model = new WordRTFDocumentConversionModel();
            return View(model);
        }

    }
}
using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using DevExpress.XtraRichEdit;
using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreDemos.OfficeFileAPI {
    public class RichEditDocumentConversionController : DocumentConversionController {
        public RichEditDocumentConversionController(IHostingEnvironment hostingEnvironment)
            : base(hostingEnvironment) {
        }

        protected override string SessionKey => "RichEditFileKey";

        public IActionResult WordRTFConvertTo(WordRTFDocumentConversionModel model) {
            RichEditDocumentServer documentServer = new RichEditDocumentServer();
            byte[] data;
            if (HttpContext.Session.TryGetValue(SessionKey, out data))
                documentServer.LoadDocument(data);

            MemoryStream stream = new MemoryStream();
            RichEditFileFormat documentType = model.FileFormat;
            DocumentFormat format = WordRTFUtils.ConvertToFormat(documentType);
            if(format == DocumentFormat.Undefined)
                documentServer.ExportToPdf(stream);
            else
                documentServer.SaveDocument(stream, format);

            string contentType = WordRTFUtils.ConvertToContentType(documentType);
            string fileExtension = WordRTFUtils.ConvertToFileExtension(documentType);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(Uri.UnescapeDataString(model.DocumentUrl));
            return CreateFileStreamResult(stream, contentType, fileExtension, fileNameWithoutExtension);
        }
    }
}
namespace AspNetCoreDemos.OfficeFileAPI {
    public class WordRTFDocumentConversionModel : WordRTFModelBase {
        public WordRTFDocumentConversionModel() {
            FileFormat = RichEditFileFormat.Pdf;
        }

        public string DocumentUrl { get; set; }
    }
}
namespace AspNetCoreDemos.OfficeFileAPI {
    public class WordRTFModelBase {
        public WordRTFModelBase() {
            PreviewModel = new WordRTFPreviewModel();
            PreviewModel.OwnerPropertyName = "PreviewModel";
            FileFormat = RichEditFileFormat.Rtf;
        }

        public RichEditFileFormat FileFormat { get; set; }
        public WordRTFPreviewModel PreviewModel { get; internal set; }
    }

    public class WordRTFPreviewModel {
        public WordRTFPreviewModel() {
        }

        public string OwnerPropertyName { get; set; }
        public string PreviewDocumentAction { get; set; }
        public string ControllerName { get; set; }
    }
}