55using GroupDocs . Editor . MVC . Products . Editor . Config ;
66using System ;
77using System . Collections . Generic ;
8+ using System . Globalization ;
89using System . IO ;
910using System . Net ;
1011using System . Net . Http ;
1314using System . Web . Http ;
1415using System . Web . Http . Cors ;
1516using GroupDocs . Editor . MVC . Products . Editor . Entity . Web . Request ;
16- using GroupDocs . Editor . Formats ;
17- using System . Globalization ;
1817
1918namespace GroupDocs . Editor . MVC . Products . Editor . Controllers
2019{
@@ -130,15 +129,10 @@ public HttpResponseMessage LoadDocumentDescription(PostedDataEntity postedData)
130129 // return document description
131130 return Request . CreateResponse ( HttpStatusCode . OK , loadDocumentEntity ) ;
132131 }
133- catch ( PasswordRequiredException ex )
134- {
135- // set exception message
136- return Request . CreateResponse ( HttpStatusCode . Forbidden , new Resources ( ) . GenerateException ( ex , postedData . password ) ) ;
137- }
138132 catch ( System . Exception ex )
139133 {
140134 // set exception message
141- return Request . CreateResponse ( HttpStatusCode . InternalServerError , new Resources ( ) . GenerateException ( ex , postedData . password ) ) ;
135+ return Request . CreateResponse ( HttpStatusCode . Forbidden , new Resources ( ) . GenerateException ( ex , postedData . password ) ) ;
142136 }
143137 }
144138
@@ -249,37 +243,34 @@ public HttpResponseMessage SaveFile(EditDocumentRequest postedData)
249243 try
250244 {
251245 string htmlContent = postedData . getContent ( ) ; // Initialize with HTML markup of the edited document
252- string saveFilePath = Path . Combine ( globalConfiguration . GetEditorConfiguration ( ) . GetFilesDirectory ( ) , postedData . GetGuid ( ) ) ;
253-
254- string tempFilename = Path . GetFileNameWithoutExtension ( saveFilePath ) + ".tmp" ;
255- string tempPath = Path . Combine ( Path . GetDirectoryName ( saveFilePath ) , tempFilename ) ;
256-
257- using ( GroupDocs . Editor . Editor editor = new GroupDocs . Editor . Editor ( postedData . GetGuid ( ) ) )
258- {
259- ISaveOptions saveOptions = GetSaveOptions ( saveFilePath ) ;
260- EditableDocument htmlContentDoc = EditableDocument . FromMarkup ( htmlContent , null ) ;
261-
262- using ( FileStream outputStream = File . Create ( tempPath ) )
263- {
264- editor . Save ( htmlContentDoc , outputStream , saveOptions ) ;
265- }
266- }
267246
247+ string saveFilePath = Path . Combine ( globalConfiguration . GetEditorConfiguration ( ) . GetFilesDirectory ( ) , postedData . GetGuid ( ) ) ;
268248 if ( File . Exists ( saveFilePath ) )
269249 {
270250 File . Delete ( saveFilePath ) ;
271251 }
272-
273- File . Move ( tempPath , saveFilePath ) ;
274-
252+ using ( OutputHtmlDocument editedHtmlDoc = new OutputHtmlDocument ( htmlContent , null ) )
253+ {
254+ dynamic options = GetSaveOptions ( saveFilePath ) ;
255+ if ( options . GetType ( ) . Equals ( typeof ( WordProcessingSaveOptions ) ) )
256+ {
257+ options . EnablePagination = true ;
258+ }
259+ options . Password = postedData . getPassword ( ) ;
260+ options . OutputFormat = GetSaveFormat ( saveFilePath ) ;
261+ using ( System . IO . FileStream outputStream = System . IO . File . Create ( saveFilePath ) )
262+ {
263+ EditorHandler . ToDocument ( editedHtmlDoc , outputStream , options ) ;
264+ }
265+ }
275266 LoadDocumentEntity loadDocumentEntity = LoadDocument ( saveFilePath , postedData . getPassword ( ) ) ;
276267 // return document description
277268 return Request . CreateResponse ( HttpStatusCode . OK , loadDocumentEntity ) ;
278269 }
279- catch ( Exception ex )
270+ catch ( System . Exception ex )
280271 {
281272 // set exception message
282- return Request . CreateResponse ( HttpStatusCode . InternalServerError , new Resources ( ) . GenerateException ( ex , postedData . getPassword ( ) ) ) ;
273+ return Request . CreateResponse ( HttpStatusCode . Forbidden , new Resources ( ) . GenerateException ( ex , postedData . getPassword ( ) ) ) ;
283274 }
284275 }
285276
@@ -318,15 +309,30 @@ private dynamic GetSaveFormat(string saveFilePath)
318309 case "Ott" :
319310 format = WordProcessingFormats . Ott ;
320311 break ;
312+ case "txt" :
313+ format = WordProcessingFormats . Text ;
314+ break ;
315+ case "Html" :
316+ format = WordProcessingFormats . Html ;
317+ break ;
318+ case "Mhtml" :
319+ format = WordProcessingFormats . Mhtml ;
320+ break ;
321321 case "WordML" :
322322 format = WordProcessingFormats . WordML ;
323323 break ;
324+ case "Csv" :
325+ format = SpreadsheetFormats . Csv ;
326+ break ;
324327 case "Ods" :
325328 format = SpreadsheetFormats . Ods ;
326329 break ;
327330 case "SpreadsheetML" :
328331 format = SpreadsheetFormats . SpreadsheetML ;
329332 break ;
333+ case "TabDelimited" :
334+ format = SpreadsheetFormats . TabDelimited ;
335+ break ;
330336 case "Xls" :
331337 format = SpreadsheetFormats . Xls ;
332338 break ;
@@ -348,139 +354,114 @@ private dynamic GetSaveFormat(string saveFilePath)
348354 default :
349355 format = WordProcessingFormats . Docx ;
350356 break ;
351- }
352357
358+ }
353359 return format ;
354360 }
355361
356- private ISaveOptions GetSaveOptions ( string saveFilePath )
362+ private dynamic GetSaveOptions ( string saveFilePath )
357363 {
358364 string extension = Path . GetExtension ( saveFilePath ) . Replace ( "." , "" ) ;
359365 extension = CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( extension ) ;
360-
361366 if ( extension . Equals ( "Txt" ) )
362367 {
363368 extension = "Text" ;
364369 }
365-
366- ISaveOptions options = null ;
367-
368- foreach ( var item in typeof ( WordProcessingFormats ) . GetFields ( ) )
370+ dynamic options = null ;
371+ foreach ( var item in Enum . GetNames ( typeof ( WordProcessingFormats ) ) )
369372 {
370- if ( item . Name . Equals ( "Auto" ) )
373+ if ( item . Equals ( "Auto" ) )
371374 {
372375 continue ;
373376 }
374-
375- if ( item . Name . Equals ( extension ) )
377+ if ( item . Equals ( extension ) )
376378 {
377- options = new WordProcessingSaveOptions ( WordProcessingFormats . Docm ) ;
379+ options = new WordProcessingSaveOptions ( ) ;
378380 break ;
379381 }
380382 }
381-
382383 if ( options == null )
383384 {
384- options = new SpreadsheetSaveOptions ( SpreadsheetFormats . Xlsb ) ;
385+ options = new SpreadsheetSaveOptions ( ) ;
385386 }
386-
387- return options ;
388- }
389-
390- private ILoadOptions GetLoadOptions ( string guid )
391- {
392- string extension = Path . GetExtension ( guid ) . Replace ( "." , "" ) ;
393- extension = CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( extension ) ;
394-
395- if ( extension . Equals ( "Txt" ) )
396- {
397- extension = "Text" ;
398- }
399-
400- ILoadOptions options = null ;
401-
402- foreach ( var item in typeof ( WordProcessingFormats ) . GetFields ( ) )
403- {
404- if ( item . Name . Equals ( "Auto" ) )
405- {
406- continue ;
407- }
408-
409- if ( item . Name . Equals ( extension ) )
410- {
411- options = new WordProcessingLoadOptions ( ) ;
412- break ;
413- }
414- }
415-
416- if ( options == null )
417- {
418- options = new SpreadsheetLoadOptions ( ) ;
419- }
420-
421387 return options ;
422388 }
423389
424390 private static List < string > PrepareFormats ( )
425391 {
426392 List < string > outputListItems = new List < string > ( ) ;
427393
428- foreach ( var item in typeof ( WordProcessingFormats ) . GetFields ( ) )
394+ foreach ( var item in Enum . GetNames ( typeof ( WordProcessingFormats ) ) )
429395 {
430- if ( item . Name . Equals ( "Auto" ) )
396+ if ( item . Equals ( "Auto" ) )
431397 {
432398 continue ;
433399 }
434-
435- if ( item . Name . Equals ( "Text" ) )
400+ if ( item . Equals ( "Text" ) )
436401 {
437402 outputListItems . Add ( "Txt" ) ;
438403 }
439-
440404 else
441405 {
442- outputListItems . Add ( item . Name ) ;
406+ outputListItems . Add ( item ) ;
443407 }
408+
444409 }
445410
446- foreach ( var item in typeof ( SpreadsheetFormats ) . GetFields ( ) )
411+ foreach ( var item in Enum . GetNames ( typeof ( SpreadsheetFormats ) ) )
447412 {
448- if ( item . Name . Equals ( "Auto" ) )
413+ if ( item . Equals ( "Auto" ) )
449414 {
450415 continue ;
451416 }
452-
453- outputListItems . Add ( item . Name ) ;
417+ outputListItems . Add ( item ) ;
454418 }
455419
456420 return outputListItems ;
457421 }
458422
459423 private LoadDocumentEntity LoadDocument ( string guid , string password )
460424 {
461- LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity ( ) ;
462- ILoadOptions loadOptions = GetLoadOptions ( guid ) ;
463- loadOptions . Password = password ;
464-
465- // Instantiate Editor object by loading the input file
466- using ( GroupDocs . Editor . Editor editor = new GroupDocs . Editor . Editor ( guid , delegate { return loadOptions ; } ) )
425+ try
467426 {
468- // Open input document for edit — obtain an intermediate document, that can be edited
469- EditableDocument beforeEdit = editor . Edit ( ) ;
427+ dynamic options = null ;
428+ //GroupDocs.Editor cannot detect text-based Cells documents formats (like CSV) automatically
429+ if ( guid . EndsWith ( "csv" , StringComparison . OrdinalIgnoreCase ) )
430+ {
431+ options = new SpreadsheetToHtmlOptions ( ) ;
432+ }
433+ else
434+ {
435+ options = EditorHandler . DetectOptionsFromExtension ( guid ) ;
436+ }
470437
471- // Get document as a single base64-encoded string, where all resources (images, fonts, etc)
472- // are embedded inside this string along with main textual content
473- string allEmbeddedInsideString = beforeEdit . GetEmbeddedHtml ( ) ;
438+ if ( options is SpreadsheetToHtmlOptions )
439+ {
440+ options . TextOptions = options . TextLoadOptions ( "," ) ;
441+ }
442+ else
443+ {
444+ options . Password = password ;
445+ }
446+ string bodyContent ;
474447
475- loadDocumentEntity . SetGuid ( guid ) ;
448+ using ( System . IO . FileStream inputDoc = System . IO . File . OpenRead ( guid ) )
449+
450+ using ( InputHtmlDocument htmlDoc = EditorHandler . ToHtml ( inputDoc , options ) )
451+ {
452+ bodyContent = htmlDoc . GetEmbeddedHtml ( ) ;
453+ }
454+ LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity ( ) ;
455+ loadDocumentEntity . SetGuid ( System . IO . Path . GetFileName ( guid ) ) ;
476456 PageDescriptionEntity page = new PageDescriptionEntity ( ) ;
477- page . SetData ( allEmbeddedInsideString ) ;
457+ page . SetData ( bodyContent ) ;
478458 loadDocumentEntity . SetPages ( page ) ;
479-
480- beforeEdit . Dispose ( ) ;
459+ return loadDocumentEntity ;
460+ }
461+ catch
462+ {
463+ throw ;
481464 }
482-
483- return loadDocumentEntity ;
484465 }
485466 }
486467}
0 commit comments