@@ -99,7 +99,7 @@ public string SpellCheck([FromBody] SpellCheckJsonData spellChecker)
9999 return "{\" SpellCollection\" :[],\" HasSpellingError\" :false,\" Suggestions\" :null}" ;
100100 }
101101 }
102- // GET api/values
102+ // GET api/values
103103 [ HttpGet ]
104104 public IEnumerable < string > Get ( )
105105 {
@@ -144,7 +144,7 @@ public class CustomParameter
144144 [ HttpPost ]
145145 [ EnableCors ( "AllowAllOrigins" ) ]
146146 [ Route ( "SystemClipboard" ) ]
147- public string SystemClipboard ( [ FromBody ] CustomParameter param )
147+ public string SystemClipboard ( [ FromBody ] CustomParameter param )
148148 {
149149 if ( param . content != null && param . content != "" )
150150 {
@@ -181,7 +181,7 @@ public class UploadDocument
181181 [ HttpPost ]
182182 [ EnableCors ( "AllowAllOrigins" ) ]
183183 [ Route ( "RestrictEditing" ) ]
184- public string [ ] RestrictEditing ( [ FromBody ] CustomRestrictParameter param )
184+ public string [ ] RestrictEditing ( [ FromBody ] CustomRestrictParameter param )
185185 {
186186 if ( param . passwordBase64 == "" && param . passwordBase64 == null )
187187 return null ;
@@ -208,7 +208,7 @@ public string LoadDefault()
208208 [ Route ( "LoadDocument" ) ]
209209 public string LoadDocument ( [ FromForm ] UploadDocument uploadDocument )
210210 {
211- string documentPath = Path . Combine ( path , uploadDocument . DocumentName ) ;
211+ string documentPath = Path . Combine ( path , uploadDocument . DocumentName ) ;
212212 Stream stream = null ;
213213 if ( System . IO . File . Exists ( documentPath ) )
214214 {
@@ -355,6 +355,64 @@ public FileStreamResult Export(IFormCollection data)
355355 FileDownloadName = fileName
356356 } ;
357357 }
358+ [ AcceptVerbs ( "Post" ) ]
359+ [ HttpPost ]
360+ [ EnableCors ( "AllowAllOrigins" ) ]
361+ [ Route ( "CompareDocuments" ) ]
362+ public string CompareDocuments ( IFormCollection data )
363+ {
364+ if ( data . Files . Count == 0 || data . Files . Count < 2 )
365+ return null ;
366+
367+ IFormFile originalFile = data . Files [ 0 ] ;
368+ IFormFile revisedFile = data . Files [ 1 ] ;
369+
370+ WDocument originalDocument = GetWordDocument ( originalFile ) ;
371+ WDocument revisedDocument = GetWordDocument ( revisedFile ) ;
372+ originalDocument . Compare ( revisedDocument ) ;
373+ //Hooks MetafileImageParsed event.
374+ WordDocument . MetafileImageParsed += OnMetafileImageParsed ;
375+ WordDocument document = WordDocument . Load ( originalDocument ) ;
376+ //Unhooks MetafileImageParsed event.
377+ WordDocument . MetafileImageParsed -= OnMetafileImageParsed ;
378+ originalDocument . Close ( ) ;
379+ revisedDocument . Close ( ) ;
380+
381+ string json = Newtonsoft . Json . JsonConvert . SerializeObject ( document ) ;
382+ document . Dispose ( ) ;
383+ return json ;
384+ }
385+
386+ private static WDocument GetWordDocument ( IFormFile file )
387+ {
388+ Stream stream = new MemoryStream ( ) ;
389+ int index = file . FileName . LastIndexOf ( '.' ) ;
390+ string type = index > - 1 && index < file . FileName . Length - 1 ?
391+ file . FileName . Substring ( index ) : ".docx" ;
392+ file . CopyTo ( stream ) ;
393+ stream . Position = 0 ;
394+
395+ WDocument document ;
396+ if ( type == ".sfdt" )
397+ {
398+ using ( var reader = new StreamReader ( stream ) )
399+ {
400+ string sfdtContent = reader . ReadToEnd ( ) ;
401+ document = WordDocument . Save ( sfdtContent ) ;
402+ var outStream = new MemoryStream ( ) ;
403+ document . Save ( outStream , WFormatType . Docx ) ;
404+ document . Close ( ) ;
405+ WDocument wordDocument = new WDocument ( outStream , WFormatType . Docx ) ;
406+ return wordDocument ;
407+ }
408+ }
409+ else
410+ {
411+ document = new WDocument ( stream , GetWFormatType ( type ) ) ;
412+ stream . Dispose ( ) ;
413+ return document ;
414+ }
415+ }
358416 private string GetValue ( IFormCollection data , string key )
359417 {
360418 if ( data . ContainsKey ( key ) )
0 commit comments