Export in ASP.NET Core DOCX Editor
14 Aug 202618 minutes to read
ASP.NET Core DOCX Editor (Document Editor) exports the document into various known file formats on the client side, such as Microsoft Word document (.docx), Word Template (.dotx), text document (.txt), and its own format called Syncfusion Document Text (.sfdt). Rich Text Format (.rtf) export is also supported via a server-side conversion.
We are providing two types of save APIs as mentioned below.
| API name | Purpose | Code Snippet for Document Editor | Code Snippet for Document Editor Container |
|---|---|---|---|
| save(filename,FormatType):void FormatType: Sfdt or Docx or Dotx or Txt |
Creates the document with specified file name and format type. Then, the created file is downloaded in the client browser by default. | documenteditor.save(‘sample’, ‘Docx’) | container.documentEditor.save(‘sample’, ‘Docx’) |
| saveAsBlob(FormatType):Blob | Creates the document in specified format type and returns the created document as Blob. This blob can be uploaded to your required server, database, or file path. |
documenteditor.saveAsBlob(‘Docx’) | container.documentEditor.saveAsBlob(‘Docx’) |
Sfdt export
The following example shows how to export documents in Document Editor as Syncfusion document text (.sfdt).
<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditor isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true id="container"></ejs-documenteditor>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
documenteditor = documenteditorElement.ej2_instances[0];
documenteditor.resize();
document.getElementById('export').addEventListener('click', function () {
documenteditor.save('sample', 'Sfdt');
});
});
</script>public ActionResult Default()
{
return View();
}<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditorcontainer id="container" isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true></ejs-documenteditorcontainer>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
container.resize();
document.getElementById('export').addEventListener('click', function () {
container.documentEditor.save('sample', 'Sfdt');
});
});
</script>public ActionResult Default()
{
return View();
}NOTE
To enable Sfdt export for a Document Editor instance, set
enableSfdtExportto true.
Word export
The following example shows how to export the document as Word document (.docx).
<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditor isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true enableWordExprot=true id="container"></ejs-documenteditor>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
documenteditor = documenteditorElement.ej2_instances[0];
documenteditor.resize();
document.getElementById('export').addEventListener('click', function () {
documenteditor.save('sample', 'Docx');
});
});
</script>public ActionResult Default()
{
return View();
}<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditorcontainer id="container" isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true></ejs-documenteditorcontainer>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
container.resize();
document.getElementById('export').addEventListener('click', function () {
container.documentEditor.save('sample', 'Docx');
});
});
</script>public ActionResult Default()
{
return View();
}NOTE
To enable Word export for a Document Editor instance, set
enableWordExportto true.
Word Template Export
The following example shows how to export the document as Word Template (.dotx).
Note: The Syncfusion® Document Editor component’s document pagination (page-by-page display) can’t be guaranteed for all the Word documents to match the pagination of Microsoft Word application. For more information about why the document pagination (page-by-page display) differs from Microsoft Word.
<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditor isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true enableWordExprot=true id="container"></ejs-documenteditor>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
documenteditor = documenteditorElement.ej2_instances[0];
documenteditor.resize();
document.getElementById('export').addEventListener('click', function () {
documenteditor.save('sample', 'Dotx');
});
});
</script>public ActionResult Default()
{
return View();
}<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditorcontainer id="container" isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true></ejs-documenteditorcontainer>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
container.resize();
document.getElementById('export').addEventListener('click', function () {
container.documentEditor.save('sample', 'Dotx');
});
});
</script>public ActionResult Default()
{
return View();
}Text export
The following example shows how to export document as text document (.txt).
<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditor isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true enableTextExport=true id="container"></ejs-documenteditor>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
documenteditor = documenteditorElement.ej2_instances[0];
documenteditor.resize();
document.getElementById('export').addEventListener('click', function () {
documenteditor.save('sample', 'Txt');
});
});
</script>public ActionResult Default()
{
return View();
}<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditorcontainer id="container" isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true></ejs-documenteditorcontainer>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
container.resize();
document.getElementById('export').addEventListener('click', function () {
container.documentEditor.save('sample', 'Txt');
});
});
</script>public ActionResult Default()
{
return View();
}NOTE
To enable text export for a Document Editor instance, set
enableTextExportto true.
Export as blob
Document Editor also supports API to store the document into a blob.
<ejs-button id="export">Export</ejs-button>
<div id="documenteditor">
<ejs-documenteditor enableSfdtExport=true enableWordExport=true exnableTextExport=true id="container"></ejs-documenteditor>
</div>
<script>
var documenteditor;
documenteditor = documenteditorElement.ej2_instances[0];
documenteditor.resize();
documenteditor.open(sfdt);
document.getElementById('export').addEventListener('click', function () {
documenteditor.saveAsBlob('Docx').then(function (exportedDocument) {
// The blob can be processed further
});
});
</script>public ActionResult Default()
{
return View();
}<ejs-button id="export">Export</ejs-button>
<div id="documenteditor" style="width:100%;height:100%">
<ejs-documenteditorcontainer id="container" isReadOnly=false enableEditor=true enableSelection=true enableSfdtExport=true></ejs-documenteditorcontainer>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
container = documenteditorElement.ej2_instances[0];
container.resize();
document.getElementById('export').addEventListener('click', function () {
container.documentEditor.saveAsBlob('Docx');
});
});
</script>public ActionResult Default()
{
return View();
}For instance, to export the document as a Rich Text Format file, implement an ASP.NET Core web API controller that uses the Syncfusion.DocIO package (install it on the server project from NuGet) and converts the DOCX blob to RTF.
//API controller for the conversion.
[HttpPost]
public HttpResponseMessage ExportAsRtf()
{
System.Web.HttpPostedFile data = HttpContext.Current.Request.Files[0];
//Opens document stream
WordDocument wordDocument = new WordDocument(data.InputStream);
MemoryStream stream = new MemoryStream();
//Converts document stream as RTF
wordDocument.Save(stream, FormatType.Rtf);
wordDocument.Close();
stream.Position = 0;
return new HttpResponseMessage() { Content = new StreamContent(stream) };
}On the client side, you can consume this web service and save the document as a Rich Text Format (.rtf) file.
document.getElementById('export').addEventListener('click', () => {
documenteditor.saveAsBlob('Docx').then(function (exportedDocument) {
// The blob can be processed further
var formData = new FormData();
formData.append('fileName', 'sample.docx');
formData.append('data', exportedDocument);
saveAsRtf(formData);
});
});
function saveAsRtf(formData) {
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', '/api/DocumentEditor/ExportAsRtf', true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200 || httpRequest.status === 304) {
if (!(!navigator.msSaveBlob)) {
navigator.msSaveBlob(httpRequest.response, 'sample.rtf');
} else {
var downloadLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
download('sample.rtf', 'rtf', httpRequest.response, downloadLink, 'download' in downloadLink);
}
} else {
console.error(httpRequest.response);
}
}
}
httpRequest.responseType = 'blob';
httpRequest.send(formData);
}
function download(fileName, extension, buffer, downloadLink, hasDownloadAttribute) {
if (hasDownloadAttribute) {
downloadLink.download = fileName;
var dataUrl = window.URL.createObjectURL(buffer);
downloadLink.href = dataUrl;
var event = document.createEvent('MouseEvent');
event.initEvent('click', true, true);
downloadLink.dispatchEvent(event);
setTimeout(function () {
window.URL.revokeObjectURL(dataUrl);
dataUrl = undefined;
});
} else {
if (extension !== 'docx' && extension !== 'xlsx') {
var url = window.URL.createObjectURL(buffer);
var isPopupBlocked = window.open(url, '_blank');
if (!isPopupBlocked) {
window.location.href = url;
}
}
}
}public ActionResult Default()
{
return View();
}Online Demo
Explore how to export Word documents in various formats using the ASP.NET Core Document Editor in this live demo here.