public ReporterMSWord(ReportData inputData , string templatePath, string outputFilePath, Margins margins) { // absolute output file path string absOutputFilePath = string.Empty; if (Path.IsPathRooted(outputFilePath)) absOutputFilePath = outputFilePath; else absOutputFilePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), outputFilePath)); // absolute template path string absTemplatePath = string.Empty; if (Path.IsPathRooted(templatePath)) absTemplatePath = templatePath; else absTemplatePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), templatePath)); // does output directory exists string outDir = Path.GetDirectoryName(absOutputFilePath); if (!Directory.Exists(outDir)) { try { Directory.CreateDirectory(outDir); } catch (System.UnauthorizedAccessException /*ex*/) { throw new UnauthorizedAccessException(string.Format("User not allowed to write under {0}", Directory.GetParent(outDir).FullName)); } catch (Exception ex) { throw new Exception(string.Format("Directory {0} does not exist, and could not be created.", outDir), ex); } } // html file path string htmlFilePath = Path.ChangeExtension(absOutputFilePath, "html"); BuildAnalysisReport(inputData, absTemplatePath, htmlFilePath); // opens word Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = true; Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(htmlFilePath, false, true, NoEncodingDialog: true); // embed pictures (unlinking images) for (int i = 1; i <= wordDoc.InlineShapes.Count; ++i) { if (null != wordDoc.InlineShapes[i].LinkFormat && !wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument) wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true; } // set margins (unit?) wordDoc.PageSetup.TopMargin = wordApp.CentimetersToPoints(margins.Top); wordDoc.PageSetup.BottomMargin = wordApp.CentimetersToPoints(margins.Bottom); wordDoc.PageSetup.RightMargin = wordApp.CentimetersToPoints(margins.Right); wordDoc.PageSetup.LeftMargin = wordApp.CentimetersToPoints(margins.Left); // set print view wordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView; wordDoc.SaveAs(absOutputFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault); _log.Info(string.Format("Saved doc report to {0}", outputFilePath)); // delete image directory DeleteImageDirectory(); // delete html report File.Delete(htmlFilePath); }
public ReporterMSWord(ReportData inputData , string templatePath, string outputFilePath, Margins margins) { // absolute output file path string absOutputFilePath = string.Empty; if (Path.IsPathRooted(outputFilePath)) { absOutputFilePath = outputFilePath; } else { absOutputFilePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), outputFilePath)); } // absolute template path string absTemplatePath = string.Empty; if (Path.IsPathRooted(templatePath)) { absTemplatePath = templatePath; } else { absTemplatePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), templatePath)); } // does output directory exists string outDir = Path.GetDirectoryName(absOutputFilePath); if (!Directory.Exists(outDir)) { try { Directory.CreateDirectory(outDir); } catch (System.UnauthorizedAccessException /*ex*/) { throw new UnauthorizedAccessException(string.Format("User not allowed to write under {0}", Directory.GetParent(outDir).FullName)); } catch (Exception ex) { throw new Exception(string.Format("Directory {0} does not exist, and could not be created.", outDir), ex); } } // html file path string htmlFilePath = Path.ChangeExtension(absOutputFilePath, "html"); BuildAnalysisReport(inputData, absTemplatePath, htmlFilePath); // opens word Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = true; Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(htmlFilePath, false, true, NoEncodingDialog: true); // embed pictures (unlinking images) for (int i = 1; i <= wordDoc.InlineShapes.Count; ++i) { if (null != wordDoc.InlineShapes[i].LinkFormat && !wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument) { wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true; } } // set margins (unit?) wordDoc.PageSetup.TopMargin = wordApp.CentimetersToPoints(margins.Top); wordDoc.PageSetup.BottomMargin = wordApp.CentimetersToPoints(margins.Bottom); wordDoc.PageSetup.RightMargin = wordApp.CentimetersToPoints(margins.Right); wordDoc.PageSetup.LeftMargin = wordApp.CentimetersToPoints(margins.Left); // set print view wordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView; wordDoc.SaveAs(absOutputFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault); _log.Info(string.Format("Saved doc report to {0}", outputFilePath)); // delete image directory DeleteImageDirectory(); // delete html report File.Delete(htmlFilePath); }