@@ -355,44 +355,62 @@ public FileStreamResult Export(IFormCollection data)
355355 FileDownloadName = fileName
356356 } ;
357357 }
358+ [ AcceptVerbs ( "Post" ) ]
358359 [ HttpPost ]
360+ [ EnableCors ( "AllowAllOrigins" ) ]
359361 [ Route ( "CompareDocuments" ) ]
360362 public string CompareDocuments ( IFormCollection data )
361363 {
362- if ( data . Files . Count < 2 ) return null ;
363- IFormFile originalDoc = data . Files [ 0 ] ;
364- IFormFile revisedDoc = data . Files [ 1 ] ;
365- string type1 = Path . GetExtension ( originalDoc . FileName ) ? . ToLower ( ) ?? ".docx" ;
366- string type2 = Path . GetExtension ( revisedDoc . FileName ) ? . ToLower ( ) ?? ".docx" ;
364+ if ( data . Files . Count == 0 || data . Files . Count < 2 )
365+ return null ;
367366
368- Stream originalStream = GetDocumentStream ( originalDoc , type1 ) ;
367+ IFormFile originalFile = data . Files [ 0 ] ;
368+ IFormFile revisedFile = data . Files [ 1 ] ;
369369
370- Stream revisedStream = GetDocumentStream ( revisedDoc , type2 ) ;
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 ( ) ;
371380
372- using ( originalStream )
373- using ( WDocument originalDocument = new WDocument ( originalStream , WFormatType . Docx ) )
374- using ( revisedStream )
375- using ( WDocument revisedDocument = new WDocument ( revisedStream , WFormatType . Docx ) )
376- {
377- originalDocument . Compare ( revisedDocument ) ;
378- var wordDoc = Syncfusion . EJ2 . DocumentEditor . WordDocument . Load ( originalDocument ) ;
379- return Newtonsoft . Json . JsonConvert . SerializeObject ( wordDoc ) ;
380- }
381+ string json = Newtonsoft . Json . JsonConvert . SerializeObject ( document ) ;
382+ document . Dispose ( ) ;
383+ return json ;
381384 }
382- private Stream GetDocumentStream ( IFormFile file , string fileType )
385+
386+ private static WDocument GetWordDocument ( IFormFile file )
383387 {
384- if ( fileType == ".docx" )
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" )
385397 {
386- return file . OpenReadStream ( ) ;
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+ }
387408 }
388-
389- using ( var reader = new StreamReader ( file . OpenReadStream ( ) ) )
409+ else
390410 {
391- string sfdtContent = reader . ReadToEnd ( ) ;
392- WDocument document = WordDocument . Save ( sfdtContent ) ;
393- var stream = new MemoryStream ( ) ;
394- document . Save ( stream , Syncfusion . DocIO . FormatType . Docx ) ;
395- return stream ;
411+ document = new WDocument ( stream , GetWFormatType ( type ) ) ;
412+ stream . Dispose ( ) ;
413+ return document ;
396414 }
397415 }
398416 private string GetValue ( IFormCollection data , string key )
0 commit comments