|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using WDocument = Syncfusion.DocIO.DLS.WordDocument; |
| 4 | +using WFormatType = Syncfusion.DocIO.FormatType; |
| 5 | + |
| 6 | +namespace DocumentEditor.Controllers |
| 7 | +{ |
| 8 | + [ApiController] |
| 9 | + [Route("api/[controller]")] |
| 10 | + public class DocumentEditorController : Controller |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Imports a DOCX file and returns its SFDT (Syncfusion Document Text) JSON content. |
| 14 | + /// </summary> |
| 15 | + [Route("Import")] |
| 16 | + public string Import(IFormCollection data) |
| 17 | + { |
| 18 | + if (data.Files.Count == 0) |
| 19 | + { |
| 20 | + return string.Empty; |
| 21 | + } |
| 22 | + |
| 23 | + IFormFile file = data.Files[0]; |
| 24 | + int index = file.FileName.LastIndexOf('.'); |
| 25 | + string extension = index > -1 && index < file.FileName.Length - 1 ? |
| 26 | + file.FileName.Substring(index) : ".docx"; |
| 27 | + |
| 28 | + if (extension == ".sfdt") |
| 29 | + { |
| 30 | + using (var streamReader = new StreamReader(file.OpenReadStream())) |
| 31 | + { |
| 32 | + return streamReader.ReadToEnd(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + Stream stream = new MemoryStream(); |
| 37 | + file.CopyTo(stream); |
| 38 | + stream.Position = 0; |
| 39 | + |
| 40 | + // Convert the DOCX stream to SFDT DOM. |
| 41 | + Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx); |
| 42 | + string json = JsonConvert.SerializeObject(document); |
| 43 | + document.Dispose(); |
| 44 | + return json; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Compares an original and a revised DOCX document and returns the compared document as SFDT content with tracked changes. |
| 49 | + /// </summary> |
| 50 | + [Route("CompareDocuments")] |
| 51 | + public string CompareDocuments(IFormCollection data) |
| 52 | + { |
| 53 | + if (data.Files.Count < 2) |
| 54 | + { |
| 55 | + return string.Empty; |
| 56 | + } |
| 57 | + |
| 58 | + IFormFile? originalFile = data.Files.FirstOrDefault(f => f.Name == "originalFile"); |
| 59 | + IFormFile? revisedFile = data.Files.FirstOrDefault(f => f.Name == "revisedFile"); |
| 60 | + if (originalFile == null || revisedFile == null) |
| 61 | + { |
| 62 | + return string.Empty; |
| 63 | + } |
| 64 | + |
| 65 | + string author = data.TryGetValue("author", out var authorValue) ? authorValue.ToString() : "Author"; |
| 66 | + string dateTime = data.TryGetValue("dateTime", out var dateValue) ? dateValue.ToString() : DateTime.UtcNow.ToString("o"); |
| 67 | + |
| 68 | + var originalStream = new MemoryStream(); |
| 69 | + originalFile.CopyTo(originalStream); |
| 70 | + originalStream.Position = 0; |
| 71 | + |
| 72 | + var revisedStream = new MemoryStream(); |
| 73 | + revisedFile.CopyTo(revisedStream); |
| 74 | + revisedStream.Position = 0; |
| 75 | + |
| 76 | + // Load the original and revised documents using the Syncfusion .NET Word (DocIO) library. |
| 77 | + WDocument originalDocument = new WDocument(); |
| 78 | + originalDocument.Settings.SkipIncrementalSaveValidation = true; |
| 79 | + originalDocument.Open(originalStream, WFormatType.Automatic); |
| 80 | + |
| 81 | + WDocument revisedDocument = new WDocument(); |
| 82 | + revisedDocument.Settings.SkipIncrementalSaveValidation = true; |
| 83 | + revisedDocument.Open(revisedStream, WFormatType.Automatic); |
| 84 | + |
| 85 | + // Compare the revised document against the original document. |
| 86 | + originalDocument.Compare(revisedDocument, author, Convert.ToDateTime(dateTime)); |
| 87 | + |
| 88 | + // Convert the compared Word document to SFDT content. |
| 89 | + Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(originalDocument); |
| 90 | + string json = JsonConvert.SerializeObject(document); |
| 91 | + document.Dispose(); |
| 92 | + originalDocument.Dispose(); |
| 93 | + revisedDocument.Dispose(); |
| 94 | + return json; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments