Syncfusion is excited to announce yet another value-added control in the form of the document editor in Syncfusion’s Essential JS 2 (WEB) platform. The document editor is lightweight JavaScript Word processor (HTML5-based, WYSIWYG, MS Word compatible) which allows you to create, view, edit, and print Microsoft Word documents (DOCX) and SFDT (Syncfusion Document Text) format documents. This control works without any 3rd party extensions, Microsoft Office, or interop. It is interoperable with other third-party frameworks such as Angular, React, and Vue.js.
In this blog, you will learn about the feature sets available, different modules of the document editor, and how to get started with the document editor.
While creating a document management application with document editing capability, most users want to stay away from ActiveX controls or browser extensions, as your application standards or brand will not be maintained when using those tools. With this control, however, you can achieve the following functionalities in your web application:
This document editor is designed to provide high performance in all aspects, but is especially powerful in the following ways:
You can author a new document or edit an existing document with the following features of the document editor component.
Let’s see how to get started with the document editor component in a web application through the following simple steps:
git clone https://github.com/syncfusion/ej2-quickstart.git quickstart cd quickstart npm install
System.config({ paths: { 'npm:': './node_modules/', 'syncfusion:': 'npm:@syncfusion/' }, map: { app: 'app', //Syncfusion packages mapping "@syncfusion/ej2-base": "syncfusion:ej2-base/dist/ej2-base.umd.min.js", "@syncfusion/ej2-file-utils": "syncfusion:ej2-file-utils/dist/ej2-file-utils.umd.min.js", "@syncfusion/ej2-compression": "syncfusion:ej2-compression/dist/ej2-compression.umd.min.js", "@syncfusion/ej2-buttons": "syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js", "@syncfusion/ej2-popups": "syncfusion:ej2-popups/dist/ej2-popups.umd.min.js", "@syncfusion/ej2-navigations": "syncfusion:ej2-navigations/dist/ej2-navigations.umd.min.js", "@syncfusion/ej2-inputs": "syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js", "@syncfusion/ej2-splitbuttons": "syncfusion:ej2-splitbuttons/dist/ej2-splitbuttons.umd.min.js", "@syncfusion/ej2-dropdowns": "syncfusion:ej2-dropdowns/dist/ej2-dropdowns.umd.min.js", "@syncfusion/ej2-data": "syncfusion:ej2-data/dist/ej2-data.umd.min.js", "@syncfusion/ej2-lists": "syncfusion:ej2-lists/dist/ej2-lists.umd.min.js", "@syncfusion/ej2-documenteditor": "syncfusion:ej2-documenteditor/dist/ej2-documenteditor.umd.min.js" }, packages: { 'app': { main: 'app', defaultExtension: 'js' } } }); System.import('app');
<!--Element which will be rendered as DocumentEditor --> <div id="DocumentEditor" style="height:800px;"></div>
@import '../../node_modules/@syncfusion/ej2/material.css';
import { DocumentEditor } from '@syncfusion/ej2-documenteditor'; let documenteditor: DocumentEditor = new DocumentEditor({ isReadOnly: false }); documenteditor.enableAllModules(); documenteditor.appendTo('#DocumentEditor'); let sfdt: string =`{ "sections": [ { "blocks": [ { "inlines": [ { "characterFormat": { "bold": true, "italic": true }, "text": "Hello World" } ] } ], "headersFooters": { } } ] }`; documenteditor.open(sfdt);
npm start
Your browser window appears like this.
In the document editor, the documents are stored in their own format called Syncfusion Document Text (SFDT). However, you can convert Word documents into SFDT format using our “Syncfusion.EJ2.DocumentEditor” library by implementing a web service. This library helps you to convert Word documents (.docx, .doc), rich text format documents (.rtf), and text documents (.txt) into SFDT format.
Let’s see how to implement a web service using an ASP.NET Core application through the following simple steps using Visual Studio.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Syncfusion.EJ2.DocumentEditor; using Microsoft.AspNetCore.Http; using System.IO; using Microsoft.AspNetCore.Cors; namespace WebApplication1.Controllers { [Route("api/[controller]")] public class DocumentEditorController : Controller { [AcceptVerbs("Post")] [HttpPost] [EnableCors("AllowAllOrigins")] [Route("Import")] public string Import(IFormCollection data) { if (data.Files.Count == 0) return null; Stream stream = new MemoryStream(); IFormFile file = data.Files[0]; int index = file.FileName.LastIndexOf('.'); string type = index > -1 && index < file.FileName.Length - 1 ? file.FileName.Substring(index) : ".docx"; file.CopyTo(stream); stream.Position = 0; WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower())); string sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(document); document.Dispose(); return sfdt; } internal static FormatType GetFormatType(string format) { if (string.IsNullOrEmpty(format)) throw new NotSupportedException("EJ2 DocumentEditor does not support this file format."); switch (format.ToLower()) { case ".dotx": case ".docx": case ".docm": case ".dotm": return FormatType.Docx; case ".dot": case ".doc": return FormatType.Doc; case ".rtf": return FormatType.Rtf; case ".txt": return FormatType.Txt; case ".xml": return FormatType.WordML; default: throw new NotSupportedException("EJ2 DocumentEditor does not support this file format."); } } } }
You can download a sample application from this link. You can launch this from the command line or VS Code using the following code.
dotnet restore dotnet run
Now, let’s see how to use this web service on the client side to implement Word document importing.
<body> <style> body{ overflow: hidden; } #ContainerPanel{ position: relative; } #WaitingPopup { display: none; height: 800px; width: 100%; position: absolute; top: 0px; background-color: #c6c6c6; z-index: 10; } </style> <div id="ToolBar"> <input type="file" id="uploadfileButton" style="position:fixed; left:-100em" /> <input type="button" id="openButton" value="Open" style="margin:10px 2px;" /> </div> <div id="ContainerPanel"> <!--Element which will be rendered as DocumentEditor --> <div id="DocumentEditor" style="height:800px;"></div> <div id="WaitingPopup"></div> </div> </body>
let fileUpload: HTMLInputElement = document.getElementById('uploadfileButton') as HTMLInputElement; fileUpload.addEventListener('change', onFileChange); fileUpload.setAttribute('accept', '.doc,.docx,.rtf,.txt,.sfdt'); // When launched the web service using Visual studio let host: string = 'http://localhost:23412/'; // When launched the web service using VS Code or command line //let host: string = 'http://localhost:23413/'; let controller: string = 'api/documenteditor/'; let baseUrl: string = host + controller; document.getElementById('openButton').addEventListener('click', onOpenClick); function onOpenClick(): void { fileUpload.value = ''; fileUpload.click(); } function onFileChange(args: any): void { if (args.target.files[0]) { let path = args.target.files[0]; if (path.name.substr(path.name.lastIndexOf('.')) === '.sfdt') { let fileReader: FileReader = new FileReader(); fileReader.onload = (e: any) => { let contents: any = e.target.result; documenteditor.open(contents); }; fileReader.readAsText(path); documenteditor.documentName = path.name.substr(0, path.name.lastIndexOf('.')); } else { loadFile(path); } } event.preventDefault(); }; function loadFile(path: any): void { let httpRequest: XMLHttpRequest = new XMLHttpRequest(); httpRequest.open('POST', baseUrl + 'Import', true); let waitingPopUp: HTMLElement = document.getElementById('WaitingPopup'); waitingPopUp.style.display = 'block'; httpRequest.onreadystatechange = () => { if (httpRequest.readyState === 4) { if (httpRequest.status === 200 || httpRequest.status === 304) { documenteditor.open(httpRequest.responseText); waitingPopUp.style.display = 'none'; } else { waitingPopUp.style.display = 'none'; console.error(httpRequest.response); } } } let formData: FormData = new FormData(); formData.append('files', path); httpRequest.send(formData); };
Your browser appears like this.
All of the features we’ve explored are segregated into individual modules based on feature to enable selective referencing, so only the features you need are included in your application. By default, the document editor displays the document in read-only mode. You can choose the required modules based on the features needed for your application. The following are the selectable modules of the document editor which can be included as required.
Overall, the document editor component was designed to be completely modular and provide optimal performance while being highly flexible.
If you would like to try the document editor component, you can download our free trial. You can visit the document editor source in GitHub, and you can check our sample browser and documentation for detailed explanations and the facts you need to proceed further.
If you have any questions or need any clarification, please let us know in the comments section below. You can also contact us through our support forum or Direct-Trac. We are always happy to assist you!
If you like this blog post, we think you’ll also like the following free e-books: