public HttpResponseMessage SaveFile(EditDocumentRequest postedData) { try { string htmlContent = postedData.getContent(); // Initialize with HTML markup of the edited document string guid = postedData.GetGuid(); string password = postedData.getPassword(); string saveFilePath = Path.Combine(globalConfiguration.GetEditorConfiguration().GetFilesDirectory(), guid); string tempFilename = Path.GetFileNameWithoutExtension(saveFilePath) + "_tmp"; string tempPath = Path.Combine(Path.GetDirectoryName(saveFilePath), tempFilename + Path.GetExtension(saveFilePath)); ILoadOptions loadOptions = GetLoadOptions(guid); if (loadOptions != null) { loadOptions.Password = password; } // Instantiate Editor object by loading the input file using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return(loadOptions); })) { EditableDocument htmlContentDoc = EditableDocument.FromMarkup(htmlContent, null); dynamic saveOptions = GetSaveOptions(guid); if (!(saveOptions is TextSaveOptions)) { saveOptions.Password = password; } if (saveOptions is WordProcessingSaveOptions) { saveOptions.EnablePagination = true; } using (FileStream outputStream = File.Create(tempPath)) { editor.Save(htmlContentDoc, outputStream, saveOptions); } } if (File.Exists(saveFilePath)) { File.Delete(saveFilePath); } File.Move(tempPath, saveFilePath); LoadDocumentEntity loadDocumentEntity = LoadDocument(saveFilePath, password); // return document description return(Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity)); } catch (Exception ex) { // set exception message return(Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.getPassword()))); } }
public HttpResponseMessage CreateFile(EditDocumentRequest postedData) { try { string htmlContent = postedData.getContent(); string guid = postedData.GetGuid(); string saveFilePath = Path.Combine(globalConfiguration.GetEditorConfiguration().GetFilesDirectory(), guid); string tempFilename = Path.GetFileNameWithoutExtension(saveFilePath) + "_tmp"; string tempPath = Path.Combine(Path.GetDirectoryName(saveFilePath), tempFilename + Path.GetExtension(saveFilePath)); File.Create(saveFilePath).Dispose(); using (EditableDocument newFile = EditableDocument.FromMarkup(htmlContent, null)) { using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(saveFilePath)) { dynamic saveOptions = this.GetSaveOptions(saveFilePath); if (saveOptions is WordProcessingSaveOptions) { // TODO: saveOptions.EnablePagination = true here leads to exception } using (FileStream outputStream = File.Create(tempPath)) { editor.Save(newFile, outputStream, saveOptions); } } } if (File.Exists(saveFilePath)) { File.Delete(saveFilePath); } File.Move(tempPath, saveFilePath); LoadDocumentEntity loadDocumentEntity = LoadDocument(saveFilePath, ""); // return document description return(Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity)); } catch (Exception ex) { // set exception message return(Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.getPassword()))); } }
private LoadDocumentEntity LoadDocument(string guid, string password) { LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity(); loadDocumentEntity.SetGuid(guid); ILoadOptions loadOptions = GetLoadOptions(guid); if (loadOptions != null) { loadOptions.Password = password; } // Instantiate Editor object by loading the input file using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return(loadOptions); })) { IDocumentInfo documentInfo = editor.GetDocumentInfo(password); dynamic editOptions = GetEditOptions(guid); if (editOptions is WordProcessingEditOptions) { editOptions.EnablePagination = true; // Open input document for edit — obtain an intermediate document, that can be edited EditableDocument beforeEdit = editor.Edit(editOptions); string allEmbeddedInsideString = beforeEdit.GetEmbeddedHtml(); PageDescriptionEntity page = new PageDescriptionEntity(); page.SetData(allEmbeddedInsideString); loadDocumentEntity.SetPages(page); beforeEdit.Dispose(); } else if (editOptions is SpreadsheetEditOptions) { for (var i = 0; i < documentInfo.PageCount; i++) { // Let's create an intermediate EditableDocument from the i tab SpreadsheetEditOptions sheetEditOptions = new SpreadsheetEditOptions(); sheetEditOptions.WorksheetIndex = i; // index is 0-based EditableDocument tabBeforeEdit = editor.Edit(sheetEditOptions); // Get document as a single base64-encoded string, where all resources (images, fonts, etc) // are embedded inside this string along with main textual content string allEmbeddedInsideString = tabBeforeEdit.GetEmbeddedHtml(); PageDescriptionEntity page = new PageDescriptionEntity(); page.SetData(allEmbeddedInsideString); page.number = i + 1; loadDocumentEntity.SetPages(page); tabBeforeEdit.Dispose(); } } else if (editOptions is PresentationEditOptions) { for (var i = 0; i < documentInfo.PageCount; i++) { // Create editing options PresentationEditOptions presentationEditOptions = new PresentationEditOptions(); // Specify slide index from original document. editOptions.SlideNumber = i; // Because index is 0-based, it is 1st slide EditableDocument slideBeforeEdit = editor.Edit(presentationEditOptions); // Get document as a single base64-encoded string, where all resources (images, fonts, etc) // are embedded inside this string along with main textual content string allEmbeddedInsideString = slideBeforeEdit.GetEmbeddedHtml(); PageDescriptionEntity page = new PageDescriptionEntity(); page.SetData(allEmbeddedInsideString); page.number = i + 1; loadDocumentEntity.SetPages(page); slideBeforeEdit.Dispose(); } } } return(loadDocumentEntity); }