//ExEnd:SaveToWordsDocument //ExStart:SaveToWordsDocumentWithOptions /// <summary> /// Save to Words document with options. /// </summary> public static void SaveToWordsDocumentWithOptions() { // Obtain document stream Stream sourceStream = File.Open(Path.Combine(Common.sourcePath, Common.sourceFile), FileMode.Open, FileAccess.Read); using (InputHtmlDocument htmlDoc = EditorHandler.ToHtml(sourceStream)) { // Obtain HTML document content string htmlContent = htmlDoc.GetContent(); using (OutputHtmlDocument editedHtmlDoc = OutputHtmlDocument.FromMarkup(htmlContent, Path.Combine(Common.sourcePath, Common.resultResourcesFolder))) { using (System.IO.FileStream outputStream = System.IO.File.Create(Path.Combine(Common.resultPath, Common.resultFile))) { WordsSaveOptions saveOptions = new WordsSaveOptions(WordFormats.Docx, Common.sourceFilePassword); saveOptions.Locale = CultureInfo.GetCultureInfo(1); saveOptions.LocaleBi = CultureInfo.GetCultureInfo(1); saveOptions.LocaleFarEast = CultureInfo.GetCultureInfo(2); EditorHandler.ToDocument(editedHtmlDoc, outputStream, saveOptions); } } } // close stream object to release file for other methods. sourceStream.Close(); }
public ActionResult Download(string id, string format) { string LicenseFilePath = ConfigurationManager.AppSettings["LicenseFilePath"]; if (System.IO.File.Exists(LicenseFilePath)) { GroupDocs.Editor.License license = new GroupDocs.Editor.License(); // Instantiate GroupDocs.Editor license license.SetLicense(LicenseFilePath); // Apply GroupDocs.Editor license using license path } Document myDocument = Utils.GetDocument(id); WordFormats saveFormat = Utils.GetSaveFormat(format); string password = string.Empty; WordsSaveOptions saveOptions = new WordsSaveOptions(saveFormat, password); string resourcesDirectory = Server.MapPath("/App_Data/Samples"); string resultFileName = myDocument.Name + "." + format; string resultFilePath = Server.MapPath("/App_Data/Downloads") + "\\" + resultFileName; using (OutputHtmlDocument htmlDoc = OutputHtmlDocument.FromMarkup(myDocument.HTML, resourcesDirectory)) { using (System.IO.FileStream outputFile = System.IO.File.Create(resultFilePath)) { EditorHandler.ToDocument(htmlDoc, outputFile, saveOptions); return(File(resultFilePath, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", resultFileName)); } } }
// Advanced example public static void ToWordsAdvance() { // Setup Conversion configuration var conversionConfig = new ConversionConfig { StoragePath = storagePath, CachePath = cachePath }; conversionConfig.SetUseCache(true); //instantiating the conversion handler var conversionHandler = new ConversionHandler(conversionConfig); //Set password to unprotect protected document during loading LoadOptions loadOptions = new LoadOptions { Password = "******" }; // convert file to Doc, starting from page 2 and convert 2 pages, SaveOptions saveOptions = new WordsSaveOptions { ConvertFileType = WordsSaveOptions.WordsFileType.Doc, PageNumber = 2, NumPagesToConvert = 2, }; var convertedDocumentStream = conversionHandler.Convert <Stream>(inputGUIDFile, loadOptions, saveOptions); }
public string GenerateWordsDoc(string itemFolderPath) { string ext = itemFolderPath.Split('_').LastOrDefault(); string targetFilePath = System.IO.Path.Combine(itemFolderPath, string.Format("output.{0}", ext)); string editedFolderPath = System.IO.Path.Combine(itemFolderPath, Constants.EditedFolderName); Repository.CreateFolderIfNotExists(editedFolderPath); string editedResourceFolderPath = System.IO.Path.Combine(itemFolderPath, Constants.EditedFolderName, Constants.HtmlResourceFolderName); string newHtmlFilePath = System.IO.Path.Combine(itemFolderPath, Constants.EditedFolderName, Constants.HtmlFilename); if (!this._isNewArticle) { string originalResourceFolderPath = System.IO.Path.Combine(itemFolderPath, Constants.OriginalFolderName, Constants.HtmlResourceFolderName); if (System.IO.Directory.Exists(originalResourceFolderPath)) { Repository.Copy(originalResourceFolderPath, editedResourceFolderPath); } } else //if (this._isNewArticle) { Repository.CreateFolderIfNotExists(editedResourceFolderPath); } using (OutputHtmlDocument editedHtmlDoc = OutputHtmlDocument.FromMarkup(this._editedDocumentHtmlContent, editedResourceFolderPath)) { // the commented source code below is not necessary for this particular scenario, but shows a way to establish an association // between links and resources in more complex scenarios, when link doesn't contain resource name at all /* * editedHtmlDoc.LinkToResourceMapper = delegate(string linkToResource, List<IHtmlResource> resources) * { * foreach (IHtmlResource htmlResource in resources) * { * if (linkToResource.Contains(htmlResource.FilenameWithExtension)) * { * return htmlResource; * } * } * return null; * }; */ System.IO.File.WriteAllText(newHtmlFilePath, editedHtmlDoc.HtmlMarkup); WordFormats saveFormat = GetSaveFormat(ext); WordsSaveOptions saveOptions = new WordsSaveOptions(saveFormat, this._closingPassword); saveOptions.Locale = this._locale; saveOptions.LocaleBi = this._localeRtl; saveOptions.LocaleFarEast = this._localeEa; using (System.IO.FileStream outputStream = System.IO.File.Create(targetFilePath)) { EditorHandler.ToDocument(editedHtmlDoc, outputStream, saveOptions); } } return(targetFilePath); }