ReportWordDocumentManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. #if __MACOS__
  3. using Syncfusion.Drawing;
  4. #else
  5. using System.Drawing;
  6. #endif
  7. using System.Linq;
  8. using Syncfusion.DocIO.DLS;
  9. using Vinno.IUS.Common.Log;
  10. using Vinno.vCloud.Report.Interfaces;
  11. using Vinno.vCloud.Report.Models;
  12. using PageNumberStyle = Syncfusion.DocIO.DLS.PageNumberStyle;
  13. namespace Vinno.vCloud.ReportGenerator
  14. {
  15. public class ReportWordDocument : WordDocument, IReportWordDocument
  16. {
  17. }
  18. public class ReportWordDocumentManager
  19. {
  20. private static bool _showImageIndex;
  21. private static float _widthZoomRatio;
  22. /// <summary>
  23. /// gets word document from report info
  24. /// </summary>
  25. /// <param name="reportInfo"></param>
  26. /// <returns></returns>
  27. public static IReportWordDocument GetGenerateWordDocument(ReportInfo reportInfo)
  28. {
  29. _widthZoomRatio = 1;
  30. var wordDocument = new ReportWordDocument();
  31. try
  32. {
  33. if (reportInfo == null)
  34. {
  35. return null;
  36. }
  37. _showImageIndex = reportInfo.ElementValues != null && reportInfo.ElementValues.ContainsKey(ElementTag.AIIndexFlag);
  38. reportInfo = reportInfo.Clone();//bcs may del table row or change HeaderDistance/HeaderHeight
  39. var reportTemplate = reportInfo.Template;
  40. var headerVisible = reportTemplate.Header.Any(x => x.IsVisibleIn(reportInfo));
  41. if (!headerVisible)
  42. reportInfo.Template.HeaderDistance = 0;
  43. var footerVisible = reportTemplate.Footer.Any(x => x.IsVisibleIn(reportInfo));
  44. if (!footerVisible)
  45. reportInfo.Template.FooterDistance = 0;
  46. if (reportTemplate.InvertColor)
  47. {
  48. wordDocument.Background.Type = BackgroundType.Color;
  49. wordDocument.Background.Color = Color.Black;
  50. }
  51. wordDocument.FontSettings.SubstituteFont += OnSubstituteFont;
  52. var section = wordDocument.AddSection();
  53. SetPageSetting(reportTemplate.PageSize, reportTemplate.PagePadding, section);
  54. if (reportTemplate.Header.Count > 0)
  55. {
  56. if (reportTemplate.HeaderHeight == null)
  57. {
  58. foreach (var blockElement in reportTemplate.Header)
  59. {
  60. AddBlock(reportInfo, blockElement, section.HeadersFooters.Header, wordDocument);
  61. }
  62. }
  63. else
  64. {
  65. var table = section.HeadersFooters.Header.AddTable();
  66. InitTable(table);
  67. var cell = table.Rows[0].Cells[0];
  68. foreach (var blockElement in reportTemplate.Header)
  69. {
  70. AddBlock(reportInfo, blockElement, cell, wordDocument);
  71. }
  72. //bcs may del table row in AddBlock
  73. table.Rows[0].Height = (float)reportTemplate.HeaderHeight - 5;
  74. }
  75. }
  76. foreach (var blockElement in reportTemplate.Blocks)
  77. {
  78. AddBlock(reportInfo, blockElement, section.Body, wordDocument);
  79. }
  80. if (reportTemplate.Footer.Count > 0)
  81. {
  82. if (reportTemplate.FooterHeight == null)
  83. {
  84. foreach (var blockElement in reportTemplate.Footer)
  85. {
  86. AddBlock(reportInfo, blockElement, section.HeadersFooters.Footer, wordDocument);
  87. }
  88. }
  89. else
  90. {
  91. var table = section.HeadersFooters.Footer.AddTable();
  92. InitTable(table);
  93. table.Rows[0].Height = (float)reportTemplate.FooterHeight - 5;
  94. var cell = table.Rows[0].Cells[0];
  95. foreach (var blockElement in reportTemplate.Footer)
  96. {
  97. AddBlock(reportInfo, blockElement, cell, wordDocument);
  98. }
  99. }
  100. }
  101. var pageNumberElement = reportTemplate.Footer.SelectMany(f => f.Elements)
  102. .FirstOrDefault(e => e is PageNumberElement) as PageNumberElement;
  103. if (pageNumberElement != null)
  104. {
  105. section.PageSetup.RestartPageNumbering = true;
  106. section.PageSetup.PageStartingNumber = 1;
  107. section.PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
  108. }
  109. foreach (WSection wSection in wordDocument.Sections)
  110. {
  111. wSection.PageSetup.HeaderDistance = (float)reportInfo.Template.HeaderDistance;
  112. if (!headerVisible)
  113. {
  114. wSection.HeadersFooters.Header.ChildEntities.Clear();
  115. }
  116. wSection.PageSetup.FooterDistance = (float)reportInfo.Template.FooterDistance;
  117. if (!footerVisible)
  118. {
  119. wSection.HeadersFooters.Footer.ChildEntities.Clear();
  120. }
  121. var endParagraph = wSection.AddParagraph();//hide blank page/paragraph after last table
  122. endParagraph.BreakCharacterFormat.FontSize = 0;
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. Logger.WriteLineError($"Generate word document error {e}");
  128. }
  129. return wordDocument;
  130. }
  131. public static IReportWordDocument GetGenerateWordDocumentAdaptPageSetting(ReportInfo reportInfo, RTPageSize pageSize, RTThickness pagePadding)
  132. {
  133. var wordDocument = new ReportWordDocument();
  134. try
  135. {
  136. if (reportInfo == null)
  137. {
  138. return null;
  139. }
  140. _showImageIndex = reportInfo.ElementValues != null && reportInfo.ElementValues.ContainsKey(ElementTag.AIIndexFlag);
  141. reportInfo = reportInfo.Clone();//bcs may del table row or change HeaderDistance/HeaderHeight
  142. var reportTemplate = reportInfo.Template;
  143. _widthZoomRatio = (float)Math.Round((pageSize.Width-pagePadding.Left-pagePadding.Right) / (reportTemplate.PageSize.Width-reportTemplate.PagePadding.Left-reportTemplate.PagePadding.Right), 3);
  144. var headerVisible = reportTemplate.Header.Any(x => x.IsVisibleIn(reportInfo));
  145. if (!headerVisible)
  146. reportInfo.Template.HeaderDistance = 0;
  147. var footerVisible = reportTemplate.Footer.Any(x => x.IsVisibleIn(reportInfo));
  148. if (!footerVisible)
  149. reportInfo.Template.FooterDistance = 0;
  150. if (reportTemplate.InvertColor)
  151. {
  152. wordDocument.Background.Type = BackgroundType.Color;
  153. wordDocument.Background.Color = Color.Black;
  154. }
  155. wordDocument.FontSettings.SubstituteFont += OnSubstituteFont;
  156. var section = wordDocument.AddSection();
  157. SetPageSetting(pageSize, pagePadding, section);
  158. if (reportTemplate.Header.Count > 0)
  159. {
  160. if (reportTemplate.HeaderHeight == null)
  161. {
  162. foreach (var blockElement in reportTemplate.Header)
  163. {
  164. AddBlock(reportInfo, blockElement, section.HeadersFooters.Header, wordDocument);
  165. }
  166. }
  167. else
  168. {
  169. var table = section.HeadersFooters.Header.AddTable();
  170. InitTable(table);
  171. var cell = table.Rows[0].Cells[0];
  172. foreach (var blockElement in reportTemplate.Header)
  173. {
  174. AddBlock(reportInfo, blockElement, cell, wordDocument);
  175. }
  176. //bcs may del table row in AddBlock
  177. table.Rows[0].Height = (float)reportTemplate.HeaderHeight - 5;
  178. }
  179. }
  180. foreach (var blockElement in reportTemplate.Blocks)
  181. {
  182. AddBlock(reportInfo, blockElement, section.Body, wordDocument);
  183. }
  184. if (reportTemplate.Footer.Count > 0)
  185. {
  186. if (reportTemplate.FooterHeight == null)
  187. {
  188. foreach (var blockElement in reportTemplate.Footer)
  189. {
  190. AddBlock(reportInfo, blockElement, section.HeadersFooters.Footer, wordDocument);
  191. }
  192. }
  193. else
  194. {
  195. var table = section.HeadersFooters.Footer.AddTable();
  196. InitTable(table);
  197. table.Rows[0].Height = (float)reportTemplate.FooterHeight - 5;
  198. var cell = table.Rows[0].Cells[0];
  199. foreach (var blockElement in reportTemplate.Footer)
  200. {
  201. AddBlock(reportInfo, blockElement, cell, wordDocument);
  202. }
  203. }
  204. }
  205. var pageNumberElement = reportTemplate.Footer.SelectMany(f => f.Elements)
  206. .FirstOrDefault(e => e is PageNumberElement) as PageNumberElement;
  207. if (pageNumberElement != null)
  208. {
  209. section.PageSetup.RestartPageNumbering = true;
  210. section.PageSetup.PageStartingNumber = 1;
  211. section.PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
  212. }
  213. foreach (WSection wSection in wordDocument.Sections)
  214. {
  215. wSection.PageSetup.HeaderDistance = (float)reportInfo.Template.HeaderDistance;
  216. if (!headerVisible)
  217. {
  218. wSection.HeadersFooters.Header.ChildEntities.Clear();
  219. }
  220. wSection.PageSetup.FooterDistance = (float)reportInfo.Template.FooterDistance;
  221. if (!footerVisible)
  222. {
  223. wSection.HeadersFooters.Footer.ChildEntities.Clear();
  224. }
  225. var endParagraph = wSection.AddParagraph();//hide blank page/paragraph after last table
  226. endParagraph.BreakCharacterFormat.FontSize = 0;
  227. }
  228. }
  229. catch (Exception e)
  230. {
  231. Logger.WriteLineError($"Generate word document error {e}");
  232. }
  233. return wordDocument;
  234. }
  235. private static void OnSubstituteFont(object sender, SubstituteFontEventArgs args)
  236. {
  237. args.AlternateFontName = "Microsoft YaHei";
  238. }
  239. private static void InitTable(IWTable table)
  240. {
  241. table.ResetCells(1, 1);
  242. table.Rows[0].HeightType = TableRowHeightType.Exactly;
  243. table.TableFormat.Borders.BorderType = BorderStyle.None;
  244. table.TableFormat.Paddings.All = 0;// workaround for syncfusion bug
  245. }
  246. /// <summary>
  247. /// Add Block
  248. /// </summary>
  249. public static void AddBlock(ReportInfo reportInfo, IBlockElement blockElement, WTextBody textBody, ReportWordDocument wordDocument)
  250. {
  251. if (blockElement is Paragraph paragraph)
  252. {
  253. ParagraphGenerater.Generate(reportInfo, paragraph, textBody);
  254. return;
  255. }
  256. if (blockElement is RTTable rtTable)
  257. {
  258. if (rtTable.IsVisibleIn(reportInfo))
  259. {
  260. TableGenerater.Generate(reportInfo, rtTable, textBody, _widthZoomRatio, true);
  261. return;
  262. }
  263. if (reportInfo.Template.HeaderHeight != null && textBody.IsInHeader())
  264. reportInfo.Template.HeaderHeight -= rtTable.RowDefinitions.Sum(x => x.Height);
  265. }
  266. if (blockElement is InputImageList inputImageList)
  267. {
  268. TableGenerater.GenerateInputImageList(reportInfo, inputImageList, textBody, _showImageIndex);
  269. }
  270. if (blockElement is AtlasElement atlasElement)
  271. {
  272. TableGenerater.GenerateAtlasElement(reportInfo, atlasElement, textBody, wordDocument);
  273. }
  274. }
  275. /// <summary>
  276. /// Set pagesetting
  277. /// </summary>
  278. private static void SetPageSetting(RTPageSize pageSize, RTThickness pagePadding, IWSection section)
  279. {
  280. // workaround fix for too large page size crash
  281. var width = (float)pageSize.Width;
  282. if (width > 3000)
  283. width = 3000;
  284. var height = (float)pageSize.Height;
  285. if (height > 3000)
  286. height = 3000;
  287. section.PageSetup.PageSize = new SizeF(width, height);
  288. section.PageSetup.Margins = new MarginsF((float)pagePadding.Left,
  289. (float)pagePadding.Top, (float)pagePadding.Right,
  290. (float)pagePadding.Bottom);
  291. }
  292. }
  293. }