/// <summary> /// close word document and word application /// </summary> /// <param name="wApp"></param> /// <param name="wDoc"></param> internal static void Close(Application wApp, Document wDoc) { object missing = System.Type.Missing; // 1. close document if (wDoc != null) { if (!wDoc.Saved) { wDoc.Saved = true; } ((_Document)wDoc).Close(missing, missing, missing); } // 2. quit application if (wApp != null) { ((_Application)wApp).Quit(missing, missing, missing); } // 3. release com object try { System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc); System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp); } catch (Exception ex) { SxLogger.LogInfo(LogLevel.Error, ex.Message); } }
/// <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; } }
/// <summary> /// /// </summary> /// <param name="pdwFilePath"></param> /// <param name="isHighlightBookmarks"></param> /// <param name="isAddWaterMark"></param> /// <param name="pdfOutputFile"></param> internal static void GenerateFile(string pdwFilePath, bool isHighlightBookmarks, string watermark, string pdfOutputFile, string datatagColor = "", string documentSpecificColor = "") { System.Exception exception = null; // 1. initialize word application Application wApp = new Application(); wApp.Visible = false; // 2. open pdw file Document wDoc = wApp.Documents.Open(pdwFilePath); // 3. generate file try { // 3.1. add water mark if (!string.IsNullOrEmpty(watermark)) { RemoveProtectPassword(wDoc, ProtectLevel.All); AddWatermark(wApp, watermark); } // 3.2. highlight all bookmark if (isHighlightBookmarks) { HighlightBookmarks(wDoc, datatagColor, documentSpecificColor); } // 3.3. save as new file object pdfFormat = WdSaveFormat.wdFormatPDF; wDoc.SaveAs(pdfOutputFile, pdfFormat); wDoc.Saved = true; } catch (System.Exception ex) { exception = ex; SxLogger.LogInfo(LogLevel.Error, ex.Message); } Close(wApp, wDoc); if (exception != null) { throw exception; } }
private static bool Transform(string xmlPath, string xslPath, string outputPath) { try { System.Xml.Xsl.XslCompiledTransform transformer = new System.Xml.Xsl.XslCompiledTransform(); transformer.Load(xslPath); transformer.Transform(xmlPath, outputPath); return(true); } catch (Exception ex) { SxLogger.LogInfo(LogLevel.Error, ex.Message); } return(false); }
/// <summary> /// /// </summary> /// <param name="pdwFilePath"></param> /// <param name="isHighlightBookmarks"></param> /// <param name="isAddWaterMark"></param> /// <param name="pdfOutputFile"></param> internal static void GenerateFile(string pdwFilePath, bool isHighlightBookmarks, string watermark, string pdfOutputFile, Dictionary <string, string> bookmarks = null, Dictionary <string, string> datatagColors = null, Dictionary <string, string> documentSpecificColors = null) { System.Exception exception = null; // 2. open pdw file Document wDoc = GetWDoc(pdwFilePath); // 3. generate file try { // 3.1. add water mark if (!string.IsNullOrEmpty(watermark)) { RemoveProtectPassword(wDoc, ProtectLevel.All); AddWatermark(wApp, watermark); } // 3.2. highlight all bookmark if (isHighlightBookmarks) { HighlightBookmarks(wDoc, bookmarks, datatagColors, documentSpecificColors); } // 3.3. save as new file object pdfFormat = WdSaveFormat.wdFormatPDF; wDoc.SaveAs(pdfOutputFile, pdfFormat); wDoc.Saved = true; } catch (System.Exception ex) { exception = ex; SxLogger.LogInfo(LogLevel.Error, ex.Message); } CloseWDoc(wDoc); if (exception != null) { throw exception; } }
protected static void CloseWDoc(Document wDoc, bool isRemove = false) { object missing = System.Type.Missing; // 1. close document if (wDoc != null) { if (!wDoc.Saved) { wDoc.Saved = true; } ((_Document)wDoc).Close(missing, missing, missing); } //2. Remove from Document collection try { if (isRemove) { wApp.Documents.Close(RouteDocument: wDoc); } } catch (Exception ex) { SxLogger.LogInfo(LogLevel.Error, ex.Message); } // 3. release com object try { System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc); } catch (Exception ex) { SxLogger.LogInfo(LogLevel.Error, ex.Message); } }
/// <summary> /// remove protect password in document /// </summary> /// <param name="wDoc">Word document</param> /// <param name="level">Level of protected to remove</param> /// <returns>Level of removed protection (0: bookmark, 1: document, other: -1)</returns> internal static ProtectLevel RemoveProtectPassword(Document wDoc, ProtectLevel level) { ProtectLevel removedLevel = ProtectLevel.None; if (wDoc.ProtectionType == WdProtectionType.wdAllowOnlyReading) { object password = ProtectBookmarkPassword; if (level == ProtectLevel.Bookmark || level == ProtectLevel.All) { try { wDoc.Unprotect(ref password); removedLevel = ProtectLevel.Bookmark; } catch (Exception ex) { SxLogger.LogInfo(LogLevel.Error, ex.Message); } } password = ProtectDocumentPassword; if (level == ProtectLevel.Document || level == ProtectLevel.All) { try { wDoc.Unprotect(ref password); removedLevel = ProtectLevel.Document; } catch (Exception ex) { SxLogger.LogInfo(LogLevel.Error, ex.Message); } } } return(removedLevel); }