/// <summary> /// /// </summary> /// <param name="xmlFilePath"></param> /// <param name="xslFilePath"></param> /// <param name="outputFilePath"></param> /// <param name="mediaType"></param> internal static void GenerateFile(string xmlFilePath, string xslFilePath, string outputFilePath, RenderSettings.MediaType mediaType, string watermark = "") { System.Exception exception = null; // 2. open xml file Document wDoc = GetWDoc(FileName: xmlFilePath, Format: WdOpenFormat.wdOpenFormatXML, XMLTransform: xslFilePath); // 3. generate file try { // 3.1. add water mark if (!string.IsNullOrEmpty(watermark)) { RemoveProtectPassword(wDoc, ProtectLevel.All); AddWatermark(wApp, watermark); } // 3.2. save as new file switch (mediaType) { case RenderSettings.MediaType.Pdf: wDoc.ExportAsFixedFormat(outputFilePath, WdExportFormat.wdExportFormatPDF); break; case RenderSettings.MediaType.Xps: wDoc.ExportAsFixedFormat(outputFilePath, WdExportFormat.wdExportFormatXPS); break; case RenderSettings.MediaType.Docx: wDoc.SaveAs(outputFilePath, FileFormat: WdSaveFormat.wdFormatXMLDocument); break; case RenderSettings.MediaType.Mht: wDoc.SaveAs(outputFilePath, WdSaveFormat.wdFormatWebArchive); break; } wDoc.Saved = true; } catch (System.Exception ex) { exception = ex; SxLogger.LogInfo(LogLevel.Error, ex.Message); } CloseWDoc(wDoc); if (exception != null) { throw exception; } }
/// <summary> /// /// </summary> /// <param name="wordXmlFile"></param> /// <param name="mediaType"></param> /// <param name="outputFile"></param> internal static void GenerateFile(string wordXmlFile, RenderSettings.MediaType mediaType, string watermark, string outputFile) { System.Exception exception = null; // 1. initialize word application Application wApp = new Application(); wApp.Visible = false; // 2. open word document xml file Document wDoc = wApp.Documents.Open(FileName: wordXmlFile, Format: WdOpenFormat.wdOpenFormatXML); // 3. generate to output file try { // 3.1. add water mark if (!string.IsNullOrEmpty(watermark)) { RemoveProtectPassword(wDoc, ProtectLevel.All); AddWatermark(wApp, watermark); } // 3.2. export file switch (mediaType) { case RenderSettings.MediaType.Pdf: wDoc.ExportAsFixedFormat(outputFile, WdExportFormat.wdExportFormatPDF); break; case RenderSettings.MediaType.Xps: wDoc.ExportAsFixedFormat(outputFile, WdExportFormat.wdExportFormatXPS); break; case RenderSettings.MediaType.Docx: break; } wDoc.Saved = true; } catch (System.Exception ex) { exception = ex; SxLogger.LogInfo(LogLevel.Error, ex.Message); } // 4. close Close(wApp, wDoc); if (exception != null) { throw exception; } }
private static void AddAttachment(ref Outlook.MailItem email, Word.Document wDoc, RenderSettings.MediaType mediaType, string attachName, ref List <string> tempFiles) { if (wDoc == null || email == null) { return; } string filePath = string.IsNullOrEmpty(attachName) ? wDoc.FullName : attachName + FileExtension.Xml; // mwxml object missing = System.Type.Missing; switch (mediaType) { case RenderSettings.MediaType.Docx: // msxml filePath = AssetManager.FileAdapter.GetFilePath( string.Format("{0}\\{1}", AssetManager.FileAdapter.TemporaryFolderPath, System.IO.Path.GetFileNameWithoutExtension(filePath)), FileExtension.Docx); // msxml wDoc.SaveAs(filePath, Word.WdSaveFormat.wdFormatXMLDocument); tempFiles.Add(filePath); break; case RenderSettings.MediaType.Pdf: filePath = AssetManager.FileAdapter.GetFilePath( string.Format("{0}\\{1}", AssetManager.FileAdapter.TemporaryFolderPath, System.IO.Path.GetFileNameWithoutExtension(filePath)), FileExtension.Pdf); wDoc.ExportAsFixedFormat(filePath, Word.WdExportFormat.wdExportFormatPDF); tempFiles.Add(filePath); break; case RenderSettings.MediaType.Xps: filePath = AssetManager.FileAdapter.GetFilePath( string.Format("{0}\\{1}", AssetManager.FileAdapter.TemporaryFolderPath, System.IO.Path.GetFileNameWithoutExtension(filePath)), FileExtension.Xps); wDoc.ExportAsFixedFormat(filePath, Word.WdExportFormat.wdExportFormatXPS); tempFiles.Add(filePath); break; default: filePath = string.Empty; break; } if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath)) { email.Attachments.Add(filePath, missing, missing, missing); } }