HTMLExportTable.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Drawing;
  6. using FastReport.Utils;
  7. namespace FastReport.Export.Html
  8. {
  9. public partial class HTMLExport : ExportBase
  10. {
  11. private int imagesCount;
  12. private ExportMatrix matrix;
  13. private void ExportPageStyles(FastString styles, ExportMatrix FMatrix, int PageNumber)
  14. {
  15. pageStyleName = "frpage" + currentPage;
  16. PrintPageStyle(styles);
  17. if (FMatrix.StylesCount - prevStyleListIndex > 0)
  18. {
  19. styles.AppendLine(HTMLGetStylesHeader());
  20. for (int i = prevStyleListIndex; i < FMatrix.StylesCount; i++)
  21. {
  22. ExportIEMStyle EStyle = FMatrix.StyleById(i);
  23. styles.Append(HTMLGetStyleHeader(i, PageNumber));
  24. HTMLGetStyle(styles, EStyle.Font, EStyle.TextColor,
  25. EStyle.FillColor, EStyle.HAlign, EStyle.VAlign, EStyle.Border,
  26. EStyle.Padding, EStyle.RTL, EStyle.WordWrap, EStyle.LineHeight, EStyle.ParagraphOffset);
  27. }
  28. styles.AppendLine(HTMLGetStylesFooter());
  29. }
  30. }
  31. private string HTMLSaveImage(ExportIEMObject obj, int PageNumber, int CurrentPage, int ImageNumber, bool isSvg)
  32. {
  33. if (pictures)
  34. return HTMLGetImageTag(HTMLGetImage(PageNumber, CurrentPage, ImageNumber, obj.Hash, obj.Base, obj.Metafile, obj.PictureStream, isSvg));
  35. else
  36. return String.Empty;
  37. }
  38. private void SetUpMatrix(ExportMatrix FMatrix)
  39. {
  40. if (singlePage && prevStyleList != null)
  41. FMatrix.Styles = prevStyleList;
  42. if (wysiwyg)
  43. FMatrix.Inaccuracy = 0.5f;
  44. else
  45. FMatrix.Inaccuracy = 10;
  46. if (webMode)
  47. {
  48. singlePage = false;
  49. navigator = false;
  50. }
  51. FMatrix.Watermarks = true;
  52. FMatrix.HTMLMode = true;
  53. FMatrix.FillAsBitmap = true;
  54. FMatrix.Zoom = Zoom;
  55. FMatrix.RotatedAsImage = true;
  56. FMatrix.PlainRich = true;
  57. FMatrix.CropAreaFill = false;
  58. FMatrix.AreaFill = true;
  59. FMatrix.Report = Report;
  60. FMatrix.FramesOptimization = true;
  61. FMatrix.ShowProgress = false;
  62. FMatrix.FullTrust = false;
  63. FMatrix.PrintOptimized = HighQualitySVG;
  64. }
  65. private void GetColumnSizes(FastString Page, ExportMatrix FMatrix)
  66. {
  67. Page.Append("<tr style=\"height: 1px\">");
  68. for (int x = 0; x < FMatrix.Width - 1; x++)
  69. Page.Append("<td style=\"border-collapse:separate;border:none;padding:0;\" width=\"").
  70. Append(SizeValue(Math.Round(FMatrix.XPosById(x + 1) - FMatrix.XPosById(x)), FMatrix.MaxWidth, widthUnits)).
  71. Append("\"/>");
  72. if (FMatrix.Width < 2)
  73. Page.Append("<td/>");
  74. Page.AppendLine("</tr>");
  75. }
  76. private int GetTableHeader(FastString Page, ExportMatrix FMatrix, int PageNumber, int CurrentPage, int ImagesCount)
  77. {
  78. Page.Append("<table width=\"").
  79. Append(SizeValue(Math.Round(FMatrix.MaxWidth), Math.Round(FMatrix.MaxWidth), widthUnits)).
  80. Append("\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse:separate;border:none;padding:0;margin:0;table-layout:fixed;");
  81. // add watermark
  82. if (FMatrix.Pages[0].WatermarkPictureStream != null)
  83. {
  84. string wName;
  85. if (prevWatermarkSize != FMatrix.Pages[0].WatermarkPictureStream.Length)
  86. {
  87. ExportIEMObject watermark = new ExportIEMObject();
  88. watermark.Width = (FMatrix.Pages[0].Width - FMatrix.Pages[0].LeftMargin - FMatrix.Pages[0].RightMargin) * Units.Millimeters;
  89. watermark.Height = (FMatrix.Pages[0].Height - FMatrix.Pages[0].TopMargin - FMatrix.Pages[0].BottomMargin) * Units.Millimeters;
  90. watermark.PictureStream = FMatrix.Pages[0].WatermarkPictureStream;
  91. prevWatermarkSize = FMatrix.Pages[0].WatermarkPictureStream.Length;
  92. FMatrix.CheckPicsCache(watermark);
  93. prevWatermarkName = HTMLGetImage(PageNumber, CurrentPage, ImagesCount++, watermark.Hash, watermark.Base, watermark.Metafile, watermark.PictureStream, false);
  94. }
  95. wName = prevWatermarkName;
  96. Page.Append(" background: url(").Append(wName).Append(") no-repeat;");
  97. }
  98. if (singlePage && PageNumber > 1 && pageBreaks)
  99. Page.Append("\" class=\"page_break");
  100. Page.AppendLine("\" >");
  101. return ImagesCount;
  102. }
  103. private int GetTable(FastString Page, ExportMatrix FMatrix, int PageNumber, int CurrentPage, int ImagesCount)
  104. {
  105. string pgStyle;
  106. if (singlePage)
  107. pgStyle = String.Empty;
  108. else
  109. pgStyle = String.Concat("-", PageNumber.ToString());
  110. for (int y = 0; y < FMatrix.Height - 1; y++)
  111. {
  112. int drow = (int)Math.Round((FMatrix.YPosById(y + 1) - FMatrix.YPosById(y)));
  113. if (drow == 0)
  114. drow = 1;
  115. Page.Append("<tr style=\"height:").Append(SizeValue(drow, FMatrix.MaxHeight, heightUnits)).Append("\">");
  116. for (int x = 0; x < FMatrix.Width - 1; x++)
  117. {
  118. int i = FMatrix.Cell(x, y);
  119. if (i != -1)
  120. {
  121. ExportIEMObject obj = FMatrix.ObjectById(i);
  122. if (obj.Counter == 0)
  123. {
  124. int fx, fy, dx, dy;
  125. FMatrix.ObjectPos(i, out fx, out fy, out dx, out dy);
  126. obj.Counter = 1;
  127. Page.Append("<td").
  128. Append((dx > 1 ? " colspan=\"" + dx.ToString() + "\"" : String.Empty)).
  129. Append((dy > 1 ? " rowspan=\"" + dy.ToString() + "\"" : String.Empty)).
  130. Append(" class=\"").Append(stylePrefix).Append("s").Append(obj.StyleIndex.ToString()).
  131. Append(pgStyle).
  132. Append("\"");
  133. FastString style = new FastString(256);
  134. if (obj.Text.Length == 0)
  135. style.Append("font-size:1px;");
  136. if (obj.PictureStream != null && obj.IsText)
  137. style.Append("background-image: url(").
  138. Append(HTMLGetImage(PageNumber, CurrentPage, ImagesCount++, obj.Hash, obj.Base, obj.Metafile, obj.PictureStream, false)).
  139. Append(");");
  140. if (style.Length > 0)
  141. Page.Append(" style=\"").Append(style).Append("\"");
  142. if (!obj.Style.WordWrap)
  143. Page.Append(" nowrap ");
  144. Page.Append(">");
  145. // TEXT
  146. if (!String.IsNullOrEmpty(obj.URL))
  147. Page.Append("<a href=\"" + obj.URL + "\">");
  148. if (obj.IsText)
  149. {
  150. if (obj.Text.Length > 0)
  151. {
  152. switch (obj.TextRenderType)
  153. {
  154. case TextRenderType.HtmlParagraph:
  155. Page.Append(GetHtmlParagraph(obj));
  156. break;
  157. default:
  158. string text = obj.Text;
  159. if (obj.Style.Font.FontFamily.Name == "Wingdings" || obj.Style.Font.FontFamily.Name == "Webdings")
  160. {
  161. text = WingdingsToUnicodeConverter.Convert(text);
  162. }
  163. Page.Append(ExportUtils.HtmlString(text, obj.TextRenderType, Px(Math.Round(obj.Style.Font.Size * 96 / 72))));
  164. break;
  165. }
  166. }
  167. else
  168. Page.Append(NBSP);
  169. }
  170. else if (obj.TextRenderType == TextRenderType.HtmlTags)
  171. {
  172. Page.Append(obj.Text);
  173. }
  174. else
  175. Page.Append(HTMLSaveImage(obj, PageNumber, CurrentPage, ImagesCount++, obj.IsSvg));
  176. if (!String.IsNullOrEmpty(obj.URL))
  177. Page.Append("</a>");
  178. Page.Append("</td>");
  179. }
  180. }
  181. else
  182. Page.Append("</td>");
  183. }
  184. Page.AppendLine("</tr>");
  185. }
  186. return ImagesCount;
  187. }
  188. private string GetHtmlParagraph(ExportIEMObject obj)
  189. {
  190. RectangleF textRect = new RectangleF(0, 0, obj.Width - obj.Style.Padding.Horizontal, obj.Height - obj.Style.Padding.Vertical);
  191. Color color = Color.Black; if (obj.Style.TextFill is SolidFill) color = (obj.Style.TextFill as SolidFill).Color;
  192. StringFormatFlags flags = 0;
  193. float scale = 1;
  194. StringAlignment align = StringAlignment.Near;
  195. if (obj.Style.HAlign == HorzAlign.Center) align = StringAlignment.Center;
  196. else if (obj.Style.HAlign == HorzAlign.Right) align = StringAlignment.Far;
  197. StringAlignment lineAlign = StringAlignment.Near;
  198. if (obj.Style.VAlign == VertAlign.Center) lineAlign = StringAlignment.Center;
  199. else if (obj.Style.VAlign == VertAlign.Bottom) lineAlign = StringAlignment.Far;
  200. if (obj.Style.RTL) flags |= StringFormatFlags.DirectionRightToLeft;
  201. if (!obj.Style.WordWrap) flags |= StringFormatFlags.NoWrap;
  202. flags |= StringFormatFlags.NoClip;
  203. StringFormat format = Report.GraphicCache.GetStringFormat(align, lineAlign, StringTrimming.None, flags, obj.Style.FirstTabOffset * scale, obj.TabWidth * scale);
  204. if (obj.TabPositions != null)
  205. format = Report.GraphicCache.GetStringFormat(align, lineAlign, StringTrimming.None, flags, obj.Style.FirstTabOffset * scale, obj.TabPositions);
  206. if (obj.ParagraphFormat == null) obj.ParagraphFormat = new ParagraphFormat();
  207. using (HtmlTextRenderer renderer = new HtmlTextRenderer(obj.Text, Report.MeasureGraphics, obj.Style.Font.FontFamily, obj.Style.Font.Size * DrawUtils.ScreenDpiFX, obj.Style.Font.Style, color,
  208. obj.Style.TextColor, textRect, obj.Style.Underlines,
  209. format, obj.Style.HAlign, obj.Style.VAlign, obj.ParagraphFormat.MultipleScale(1), obj.Style.ForceJustify,
  210. 1, 1, obj.InlineImageCache == null ? new InlineImageCache() : obj.InlineImageCache, IsPrinting, obj.TabPositions != null))
  211. {
  212. return GetHtmlParagraph(renderer).ToString();
  213. }
  214. }
  215. private void GetTableFooter(FastString Page)
  216. {
  217. Page.AppendLine("</table>");
  218. }
  219. private void ExportHTMLPageTabledBegin(HTMLData d)
  220. {
  221. imagesCount = 0;
  222. matrix = new ExportMatrix();
  223. SetUpMatrix(matrix);
  224. matrix.AddPageBegin(d.page);
  225. }
  226. private void ExportBandTable(BandBase band)
  227. {
  228. matrix.AddBand(band, this);
  229. }
  230. private void ExportHTMLPageTabledEnd(HTMLData d)
  231. {
  232. matrix.AddPageEnd(d.page);
  233. matrix.Prepare();
  234. FastString Page = new FastString(4096);
  235. ExportPageStyles(Page, matrix, d.ReportPage);
  236. if (singlePage)
  237. {
  238. prevStyleListIndex = matrix.StylesCount;
  239. prevStyleList = matrix.Styles;
  240. }
  241. ExportHTMLPageStart(Page, d.PageNumber, d.CurrentPage);
  242. // Ancor
  243. Page.Append(HTMLGetAncor(d.PageNumber.ToString()));
  244. // Table header
  245. imagesCount = GetTableHeader(Page, matrix, d.PageNumber, d.CurrentPage, imagesCount);
  246. // Column sizes
  247. GetColumnSizes(Page, matrix);
  248. // Table
  249. imagesCount = GetTable(Page, matrix, d.ReportPage /*d.PageNumber*/, d.CurrentPage, imagesCount);
  250. // Table footer
  251. GetTableFooter(Page);
  252. ExportHTMLPageFinal(null, Page, d, matrix.MaxWidth, matrix.MaxHeight);
  253. }
  254. }
  255. }