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.
@model AspNetCoreDemos.OfficeFileAPI.WordRTFDocumentConversionModel
@using DevExtreme.AspNet.Mvc
@using (Html.BeginForm("WordRTFConversion", "DocumentConversion", 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)
.OnClick("DownloadClick"))
</div>
<div id="error" class="error-message-right">
<span>@ViewBag.ErrorMessage</span>
</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();
}
function DownloadClick() {
document.getElementById("error").hidden = true;
}
</script>
}
using Microsoft.AspNetCore.Hosting;
using System;
namespace AspNetCoreDemos.OfficeFileAPI {
public partial class DocumentConversionController : DocumentProcessingController {
public DocumentConversionController(IWebHostEnvironment hostingEnvironment)
: base(hostingEnvironment) {
}
string LoadDocumentExceptionString => "ERROR: Cannot convert this document.";
protected override string SessionKey => throw new NotImplementedException();
}
}
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(IWebHostEnvironment hostingEnvironment)
: base(hostingEnvironment) {
}
public static string RichEditSessionKey => "RichEditFileKey";
protected override string SessionKey => RichEditSessionKey;
}
public partial class DocumentConversionController {
public IActionResult WordRTFConversion() {
WordRTFDocumentConversionModel model = new WordRTFDocumentConversionModel();
return View(model);
}
[HttpPost]
public IActionResult WordRTFConversion(WordRTFDocumentConversionModel model) {
RichEditDocumentServer documentServer = new RichEditDocumentServer();
byte[] data;
try {
if (HttpContext.Session.TryGetValue(RichEditDocumentConversionController.RichEditSessionKey, 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);
} catch {
ViewBag.ErrorMessage = LoadDocumentExceptionString;
return View(model);
}
}
}
}
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; }
public int IFrameSize { get; set; } = 452;
}
}