public string ProcessDocument(string source, MSOfficeOutput msOut,string guid) { string result = null; Application app = new Application(); var pres = app.Presentations.Open(source, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse); if (msOut.Equals(MSOfficeOutput.html)) { try { pres.SaveCopyAs(FileDirectory+@"Temp\"+guid+".html", PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoCTrue); result = File.ReadAllText(FileDirectory + @"Temp\" + guid + ".html",Encoding.UTF8); File.Delete(FileDirectory + @"Temp\" + guid + ".html"); } catch { } } else if (msOut.Equals(MSOfficeOutput.rtf)) { pres.SaveCopyAs(FileDirectory + @"Temp\" + guid + ".rtf", PpSaveAsFileType.ppSaveAsRTF, MsoTriState.msoCTrue); result = File.ReadAllText(FileDirectory + @"Temp\" + guid + ".rtf",Encoding.UTF8); File.Delete(FileDirectory + @"Temp\" + guid + ".rtf"); } else if (msOut.Equals(MSOfficeOutput.txt)) { string text =""; foreach (KeyValuePair<int, string> val in ExtractText(source)) { text = text + val.Value + Environment.NewLine; } result = text; } return result; }
public byte[] TestAPI(string inputFileName, string destFormat, bool hasVideo, string extension = "html", string mime = "text/html") { //setup MSOfficeOutput msOutput = (MSOfficeOutput)Enum.Parse(typeof(MSOfficeOutput), destFormat); byte[] apiFileContent = inputFiles.Where(x => x.Key.EndsWith(inputFileName)).Select(x => x.Value).First(); MSOfficeJob OfficeJob = new MSOfficeJob() { Id = Guid.NewGuid(), FileContent = apiFileContent, UserId = Guid.Parse("d2b97532-e8c5-e411-8270-f0def103cfd0"), FileExtension = extension, FileName = "testOfficeJob", MimeType = mime, Status = JobStatus.Started, SubmitTime = DateTime.Now, DownloadCounter = 0, InputFileHash = RoboBrailleProcessor.GetMD5Hash(apiFileContent), MSOfficeOutput = msOutput }; if (hasVideo) { //TODO maybe mock Amara call OfficeJob.SubtitleLangauge = "en-US"; OfficeJob.SubtitleFormat = "srt"; } var apiTask = Task.Run(() => WebAPICall(OfficeJob)); return(apiTask.Result); }
public static byte[] ConvertWordDocument(string sourceFilePath, MSOfficeOutput outputFormat, int encoding = -1) { byte[] res = null; WdSaveFormat saveFormat = WdSaveFormat.wdFormatUnicodeText; string fileExtension = ".txt"; switch (outputFormat) { case MSOfficeOutput.html: saveFormat = WdSaveFormat.wdFormatFilteredHTML; fileExtension = ".html"; break; case MSOfficeOutput.txt: saveFormat = WdSaveFormat.wdFormatText; fileExtension = ".txt"; break; case MSOfficeOutput.rtf: saveFormat = WdSaveFormat.wdFormatRTF; fileExtension = ".rtf"; break; case MSOfficeOutput.pdf: saveFormat = WdSaveFormat.wdFormatPDF; fileExtension = ".pdf"; break; default: break; } var tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + fileExtension); Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); try { app.DisplayAlerts = WdAlertLevel.wdAlertsNone; app.Documents.Open(sourceFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); if (encoding == -1) { app.ActiveDocument.SaveAs2(tempFile, saveFormat); } else { //more detailed save used for unicode text also for slovenian encoding: 1250 and for polish: 1257 app.ActiveDocument.SaveAs2(tempFile, saveFormat, LockComments: false, Password: "", AddToRecentFiles: true, WritePassword: "", ReadOnlyRecommended: false, EmbedTrueTypeFonts: false, SaveNativePictureFormat: false, SaveFormsData: false, SaveAsAOCELetter: false, Encoding: encoding, InsertLineBreaks: false, AllowSubstitutions: false, LineEnding: WdLineEndingType.wdCRLF); } app.ActiveDocument.Close(); app.Quit(); app = null; res = File.ReadAllBytes(tempFile); } catch (Exception e) { res = null; } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } } app = null; return(res); }