Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (161).NET Core  (29).NET MAUI  (65)Angular  (74)ASP.NET  (50)ASP.NET Core  (79)ASP.NET MVC  (88)Azure  (36)Blazor  (153)DocIO  (23)Essential JS 2  (86)Essential Studio  (174)File Formats  (29)Flutter  (123)JavaScript  (187)Microsoft  (113)PDF  (67)React  (62)Succinctly series  (128)Syncfusion  (612)TypeScript  (32)Uno Platform  (3)UWP  (4)Vue  (40)Webinar  (35)Windows Forms  (59)WinUI  (53)WPF  (138)Xamarin  (156)XlsIO  (25)Other CategoriesBarcode  (5)BI  (29)Bold BI  (5)Build conference  (6)Business intelligence  (54)Button  (4)C#  (120)Chart  (57)Cloud  (12)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (6)DataGrid  (55)Development  (403)Doc  (7)DockingManager  (1)eBook  (96)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (23)Extensions  (18)File Manager  (4)Gantt  (11)Gauge  (11)Git  (4)Grid  (28)HTML  (11)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (314)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (30)Performance  (9)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (7)Road Map  (10)Scheduler  (37)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (23)Solution Services  (3)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (92)UI  (226)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (23)Visual Studio Code  (17)Web  (377)What's new  (213)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Document Editing Made Easy Syncfusion Word Processor + Wisej

Document Editing Made Easy: Syncfusion Word Processor + Wisej

A challenge we see while working on modernization projects for desktop applications involves local file access. Web projects have a completely different approach to working with user files that don’t include unrestricted access to the client’s file system. Until recently, if you wanted to modify a file on the client’s file system, you had to go through traditional upload and download dialogs, ending up with the same document multiple times in the downloads folder.

With the introduction of Wisej’s Syncfusion Essential Studio JS integration and the Client File System API, working with local user files is a breeze. In this blog post, we show several different ways to integrate Word documents into the JS 2 Word Processor control.

The first approach uses the server’s file system. The second approach shows the integration using Wisej’s Client File System extension.

About Wisej

Wisej is a powerful SPA web framework for C# and VB.NET developers. It helps us to migrate existing WinForms, VB6, and Visual Web GUI applications to a modernized, cross-platform solution with minimal code changes. We can easily migrate the existing Syncfusion controls for desktop to their corresponding web-based equivalents without the hassle of entirely rewriting the application. Wisej features a pixel-perfect WYSIWYG designer that helps us to drag and drop controls, assign properties, and attach events with the click of a button. Hot reload and a powerful theming system allows developers to see changes in real-time.

If you’ve never used Wisej before, you can get it here.

Getting started

To use the JS Word Processor in Wisej, we have to add several Syncfusion libraries to the project:

  • Syncfusion.Compression.Base
  • Syncfusion.DocIO.Base
  • Syncfusion.EJ2.DocumentEditor
  • Syncfusion.Licensing
  • Syncfusion.OfficeChart.Base

We can easily retrieve these libraries from the Syncfusion website.

You can download the full demo project here.

Retrieving documents on the server

We can fetch documents from the server’s file system and show them in the client’s browser using a few simple calls in Wisej.

To get started, load a Word document into a Stream.

var stream = new FileStream(Application.MapPath("Some/Location/In/My/App.docx"), FileMode.Open);

Once the file is in a stream, it can be loaded into a WordDocument(Syncfusion.EJ2.DocumentEditor.WordDocument) instance:

using (var stream = new FileStream(Application.MapPath("Some/Location/In/My/App.docx"), FileMode.Open))
{
  var document = WordDocument.Load(stream, FormatType.Docx);
}

We can serialize the WordDocument instance to get the corresponding Syncfusion Document Text (SFDT) string.

using (var stream = new FileStream(Application.MapPath("Some/Location/In/My/App.docx"), FileMode.Open))
{
  var document = WordDocument.Load(stream, FormatType.Docx);
  var sfdt = JsonConvert.SerializeObject(document);
}

Once we have the SFDT, we can add a Syncfusion Word Processor (documentEditor in the code) from the Wisej toolbox and load the text into the widget.

Hint:If the DocumentEditor control is missing from the Toolbox, you can right-click the toolbox, choose items, and select the Wisej.Web.Ext.Syncfusion2.dll.

using (var stream = new FileStream(Application.MapPath("Some/Location/In/My/App.docx"), FileMode.Open))
{
  var document = WordDocument.Load(stream, FormatType.Docx);
  var sfdt = JsonConvert.SerializeObject(document);
  this.documentEditor1.OpenFile(sfdt);
}

Design-Time Experience of JS 2 Word Processor in Wisej
Design-Time Experience of JS Word Processor in Wisej
Resulting JS 2 Word Processor
Resulting JS Word Processor

Retrieving documents from the client

We can fetch the documents from the client’s file system and show them within the Wisej app without a traditional upload dialog.

To get started, add the Wisej.Ext.ClientFileSystem extension to the project.

After adding the extension, the Wisej application can access directories on the local file system through the browser’s File System Access API.

It is important to note that read and write permissions must be granted for each directory (unless a member of the directory’s upper hierarchy has already approved access). File access is revoked upon a page refresh, so it is important to request access again.

// get a directory from the client. 
var directoryHandle = await ClientFileSystem.ShowDirectoryPickerAsync();   

// get the files from that directory. 
var fileHandles = await directoryHandle.GetFilesAsync("*.docx");

After retrieving the files, the contents of a file can be queried.

// get a directory from the client. 
var directoryHandle = await ClientFileSystem.ShowDirectoryPickerAsync();   

// get the files from that directory. 
var fileHandles = await directoryHandle.GetFilesAsync("*.docx"); 
if (fileHandles.Length > 0) 
{ 
   // load the first Word document. 
   var fileHandle = fileHandles[0];
  // save a reference to the file if using it later. 
  this.selectedFileHandle = fileHandle;   

  // query read and write permissions on the file. 
  var state = await fileHandle.QueryPermissionAsync(Permission.ReadWrite); 
  if (state == PermissionState.Granted) 
  { 
     // read and load the file. 
     var bytes = await fileHandle.ReadBytesAsync(); 
  } 
}

The rest is the same as the server-side file loading.

// get a directory from the client. 
var directoryHandle = await ClientFileSystem.ShowDirectoryPickerAsync();   

// get the files from that directory. 
var fileHandles = await directoryHandle.GetFilesAsync("*.docx"); 
if (fileHandles.Length > 0) 
{ 
  // load the first Word document. 
  var fileHandle = fileHandles[0];   

  // query read and write permissions on the file. 
  var state = await fileHandle.QueryPermissionAsync(Permission.ReadWrite); 
  if (state == PermissionState.Granted) 
  { 
    var bytes = await fileHandle.ReadBytesAsync(); 
    using (var stream = new MemoryStream(bytes)) 
    { 
      var document = WordDocument.Load(stream, FormatType.Docx); 
      var sfdt = JsonConvert.SerializeObject(document);   
  
      this.documentEditor1.OpenFile(sfdt); 
    } 
  } 
}

Saving documents

The documents have been loaded into the client-side viewer, we can now save the documents back to the server or to the client.

To retrieve the document stream from the client, simply use the following.

var document = await this.documentEditor1.GetDocumentAsync();

Saving server-side documents

To save a document to a server location, simply use System.IO.File.WriteAllBytes().

System.IO.File.WriteAllBytes(Application.MapPath("path/to/file.docx"), document.ToArray());

Saving client-side documents

To save the document to a client-side location, use the reference from the client-side file handle.

this.selectedFileHandle.WriteBytesAsync(document.ToArray(), 0, WritableType.Write, false);

Opening client-side documents using Word

When using the client’s file system to load documents into the viewer, documents can be opened directly in Word from a Wisej application.

Microsoft Office includes a custom data scheme (ms-word://) that provides functionality to open a file path in Word from the browser. You can learn more about it here.

As an example, we can directly open a client-side file located at F:/Play/Test.docx from a Wisej application using Application.Navigate().

Keep in mind the full path of the directory must be known to the Wisej application and the path cannot contain spaces.

Application.Navigate("ms-word:ofe|u|F:/Play/Test.docx");

Also, you can get the demo project from GitHub here. I hope this blog has been helpful to you. Thank you for reading.

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top