|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Microsoft.AspNetCore.Cors; |
| 3 | +using Microsoft.AspNetCore.Hosting; |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | +using Syncfusion.DocIO; |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using EJ2DocumentEditor = Syncfusion.EJ2.DocumentEditor; |
| 9 | +namespace DocumentEditor.Controllers; |
| 10 | + |
| 11 | +[ApiController] |
| 12 | +[Route("api/[controller]")] |
| 13 | +public class DocumenteditorController : ControllerBase |
| 14 | +{ |
| 15 | + private IHostEnvironment hostEnvironment; |
| 16 | + public DocumenteditorController(IHostEnvironment environment) |
| 17 | + { |
| 18 | + this.hostEnvironment = environment; |
| 19 | + } |
| 20 | + //Import file from client side. |
| 21 | + [Route("Import")] |
| 22 | + public string Import(IFormCollection data) |
| 23 | + { |
| 24 | + if (data.Files.Count == 0) |
| 25 | + return null; |
| 26 | + Stream stream = new MemoryStream(); |
| 27 | + IFormFile file = data.Files[0]; |
| 28 | + int index = file.FileName.LastIndexOf('.'); |
| 29 | + string type = index > -1 && index < file.FileName.Length - 1 ? |
| 30 | + file.FileName.Substring(index) : ".docx"; |
| 31 | + file.CopyTo(stream); |
| 32 | + stream.Position = 0; |
| 33 | + |
| 34 | + EJ2DocumentEditor.WordDocument document = EJ2DocumentEditor.WordDocument.Load(stream, GetFormatType(type.ToLower())); |
| 35 | + string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); |
| 36 | + document.Dispose(); |
| 37 | + return json; |
| 38 | + } |
| 39 | + //Import documents from web server. |
| 40 | + [Route("ImportFile")] |
| 41 | + public string ImportFile([FromBody]CustomParams param) |
| 42 | + { |
| 43 | + string path = this.hostEnvironment.ContentRootPath + "\\Files\\" + param.fileName; |
| 44 | + try |
| 45 | + { |
| 46 | + Stream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.ReadWrite); |
| 47 | + Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, GetFormatType(path)); |
| 48 | + string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); |
| 49 | + document.Dispose(); |
| 50 | + stream.Dispose(); |
| 51 | + return json; |
| 52 | + } |
| 53 | + catch |
| 54 | + { |
| 55 | + return "Failure"; |
| 56 | + } |
| 57 | + } |
| 58 | + public class CustomParams |
| 59 | + { |
| 60 | + public string fileName |
| 61 | + { |
| 62 | + get; |
| 63 | + set; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [Route("LoadDefault")] |
| 68 | + public string LoadDefault() |
| 69 | + { |
| 70 | + string path = this.hostEnvironment.ContentRootPath + "\\Files\\" + "sample.docx"; |
| 71 | + Stream stream = System.IO.File.OpenRead(path); |
| 72 | + stream.Position = 0; |
| 73 | + |
| 74 | + Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, EJ2DocumentEditor.FormatType.Docx); |
| 75 | + string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); |
| 76 | + document.Dispose(); |
| 77 | + return json; |
| 78 | + } |
| 79 | + |
| 80 | + //Save document in web server. |
| 81 | + [Route("Save")] |
| 82 | + public string Save([FromBody]CustomParameter param) |
| 83 | + { |
| 84 | + string path = this.hostEnvironment.ContentRootPath + "\\Files\\" + param.fileName; |
| 85 | + Byte[] byteArray = Convert.FromBase64String(param.documentData); |
| 86 | + Stream stream = new MemoryStream(byteArray); |
| 87 | + EJ2DocumentEditor.FormatType type = GetFormatType(path); |
| 88 | + try |
| 89 | + { |
| 90 | + FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); |
| 91 | + |
| 92 | + if (type != EJ2DocumentEditor.FormatType.Docx) |
| 93 | + { |
| 94 | + Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx); |
| 95 | + document.Save(fileStream, GetDocIOFomatType(type)); |
| 96 | + document.Close(); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + stream.Position = 0; |
| 101 | + stream.CopyTo(fileStream); |
| 102 | + } |
| 103 | + stream.Dispose(); |
| 104 | + fileStream.Dispose(); |
| 105 | + return "Sucess"; |
| 106 | + } |
| 107 | + catch |
| 108 | + { |
| 109 | + Console.WriteLine("err"); |
| 110 | + return "Failure"; |
| 111 | + } |
| 112 | + } |
| 113 | + public class CustomParameter |
| 114 | + { |
| 115 | + public string fileName |
| 116 | + { |
| 117 | + get; |
| 118 | + set; |
| 119 | + } |
| 120 | + public string documentData |
| 121 | + { |
| 122 | + get; |
| 123 | + set; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + internal static EJ2DocumentEditor.FormatType GetFormatType(string fileName) |
| 128 | + { |
| 129 | + int index = fileName.LastIndexOf('.'); |
| 130 | + string format = index > -1 && index < fileName.Length - 1 ? fileName.Substring(index + 1) : ""; |
| 131 | + |
| 132 | + if (string.IsNullOrEmpty(format)) |
| 133 | + throw new NotSupportedException("EJ2 Document editor does not support this file format."); |
| 134 | + switch (format.ToLower()) |
| 135 | + { |
| 136 | + case "dotx": |
| 137 | + case "docx": |
| 138 | + case "docm": |
| 139 | + case "dotm": |
| 140 | + return EJ2DocumentEditor.FormatType.Docx; |
| 141 | + case "dot": |
| 142 | + case "doc": |
| 143 | + return EJ2DocumentEditor.FormatType.Doc; |
| 144 | + case "rtf": |
| 145 | + return EJ2DocumentEditor.FormatType.Rtf; |
| 146 | + case "txt": |
| 147 | + return EJ2DocumentEditor.FormatType.Txt; |
| 148 | + case "xml": |
| 149 | + return EJ2DocumentEditor.FormatType.WordML; |
| 150 | + default: |
| 151 | + throw new NotSupportedException("EJ2 Document editor does not support this file format."); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + internal static Syncfusion.DocIO.FormatType GetDocIOFomatType(EJ2DocumentEditor.FormatType type) |
| 156 | + { |
| 157 | + switch (type) |
| 158 | + { |
| 159 | + case EJ2DocumentEditor.FormatType.Docx: |
| 160 | + return FormatType.Docx; |
| 161 | + case EJ2DocumentEditor.FormatType.Doc: |
| 162 | + return FormatType.Doc; |
| 163 | + case EJ2DocumentEditor.FormatType.Rtf: |
| 164 | + return FormatType.Rtf; |
| 165 | + case EJ2DocumentEditor.FormatType.Txt: |
| 166 | + return FormatType.Txt; |
| 167 | + case EJ2DocumentEditor.FormatType.WordML: |
| 168 | + return FormatType.WordML; |
| 169 | + default: |
| 170 | + throw new NotSupportedException("DocIO does not support this file format."); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments