HTMLExportLayers.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using FastReport.Table;
  5. using FastReport.Utils;
  6. using System.Windows.Forms;
  7. using FastReport.Export;
  8. using System.ComponentModel;
  9. using System.Drawing.Text;
  10. namespace FastReport.Export.Html
  11. {
  12. public partial class HTMLExport : ExportBase
  13. {
  14. private bool doPageBreak;
  15. private void ExportPageStylesLayers(FastString styles, int PageNumber)
  16. {
  17. PrintPageStyle(styles);
  18. if (prevStyleListIndex < cssStyles.Count)
  19. {
  20. styles.AppendLine(HTMLGetStylesHeader());
  21. for (int i = prevStyleListIndex; i < cssStyles.Count; i++)
  22. styles.Append(HTMLGetStyleHeader(i, PageNumber)).Append(cssStyles[i]).AppendLine("}");
  23. styles.AppendLine(HTMLGetStylesFooter());
  24. }
  25. }
  26. private void Layer(FastString Page, ReportComponentBase obj,
  27. float Left, float Top, float Width, float Height, FastString Text, string classTag, FastString addstyletag)
  28. {
  29. if (Page != null && obj != null)
  30. {
  31. string onclick = null;
  32. if (!String.IsNullOrEmpty(ReportID))
  33. {
  34. if (!String.IsNullOrEmpty(obj.ClickEvent) || obj.HasClickListeners())
  35. {
  36. onclick = "click";
  37. }
  38. CheckBoxObject checkBoxObject = obj as CheckBoxObject;
  39. if (checkBoxObject != null && checkBoxObject.Editable)
  40. {
  41. onclick = "checkbox_click";
  42. }
  43. TextObject textObject = obj as TextObject;
  44. if (textObject != null && textObject.Editable)
  45. {
  46. onclick = "text_edit";
  47. }
  48. }
  49. // we need to adjust left, top, width and height values because borders take up space in html elements
  50. float borderLeft;
  51. float borderRight;
  52. float borderTop;
  53. float borderBottom;
  54. HTMLBorderWidthValues(obj, out borderLeft, out borderTop, out borderRight, out borderBottom);
  55. string href = GetHref(obj);
  56. if (!string.IsNullOrEmpty(href))
  57. {
  58. Page.Append(href);
  59. }
  60. Page.Append("<div ").Append(classTag).Append(" style=\"").
  61. Append(onclick != null || !string.IsNullOrEmpty(href) ? "cursor:pointer;" : "").
  62. Append("left:").Append(Px((leftMargin + Left) * Zoom - borderLeft / 2f)).
  63. Append("top:").Append(Px((topMargin + Top) * Zoom - borderTop / 2f)).
  64. Append("width:").Append(Px(Width * Zoom - borderRight / 2f - borderLeft / 2f)).
  65. Append("height:").Append(Px(Height * Zoom - borderBottom / 2f - borderTop / 2f));
  66. if (addstyletag != null)
  67. Page.Append(addstyletag);
  68. Page.Append("\"");
  69. if (onclick != null)
  70. {
  71. string eventParam = String.Format("{0},{1},{2},{3}",
  72. obj.Name,
  73. CurPage,
  74. obj.AbsLeft.ToString("#0"),
  75. obj.AbsTop.ToString("#0"));
  76. Page.Append(" onclick=\"")
  77. .AppendFormat(OnClickTemplate, ReportID, onclick, eventParam)
  78. .Append("\"");
  79. }
  80. Page.Append(">");
  81. if (Text == null)
  82. Page.Append(NBSP);
  83. else
  84. Page.Append(Text);
  85. Page.AppendLine("</div>");
  86. if (!string.IsNullOrEmpty(href))
  87. {
  88. Page.Append("</a>");
  89. }
  90. }
  91. }
  92. private string EncodeURL(string value)
  93. {
  94. #if CROSSPLATFORM || COREWIN
  95. return System.Net.WebUtility.UrlEncode(value);
  96. #else
  97. return ExportUtils.HtmlURL(value);
  98. #endif
  99. }
  100. private string GetHref(ReportComponentBase obj)
  101. {
  102. string href = String.Empty;
  103. href = GetHrefAdvMatrixButton(obj, href);
  104. if (!String.IsNullOrEmpty(obj.Hyperlink.Value))
  105. {
  106. string hrefStyle = String.Empty;
  107. if (obj is TextObject)
  108. {
  109. TextObject textObject = obj as TextObject;
  110. hrefStyle = String.Format("style=\"color:{0}{1}\"",
  111. ExportUtils.HTMLColor(textObject.TextColor),
  112. !textObject.Font.Underline ? ";text-decoration:none" : String.Empty
  113. );
  114. }
  115. string url = EncodeURL(obj.Hyperlink.Value);
  116. if (obj.Hyperlink.Kind == HyperlinkKind.URL)
  117. href = String.Format("<a {0} href=\"{1}\"" + (obj.Hyperlink.OpenLinkInNewTab ? "target=\"_blank\"" : "") + ">", hrefStyle, obj.Hyperlink.Value);
  118. else if (obj.Hyperlink.Kind == HyperlinkKind.DetailReport)
  119. {
  120. url = String.Format("{0},{1},{2}",
  121. EncodeURL(obj.Name), // object name for security reasons
  122. EncodeURL(obj.Hyperlink.ReportParameter),
  123. EncodeURL(obj.Hyperlink.Value));
  124. string onClick = String.Format(OnClickTemplate, ReportID, "detailed_report", url);
  125. href = String.Format("<a {0} onclick=\"{1}\">", hrefStyle, onClick);
  126. }
  127. else if (obj.Hyperlink.Kind == HyperlinkKind.DetailPage)
  128. {
  129. url = String.Format("{0},{1},{2}",
  130. EncodeURL(obj.Name),
  131. EncodeURL(obj.Hyperlink.ReportParameter),
  132. EncodeURL(obj.Hyperlink.Value));
  133. string onClick = String.Format(OnClickTemplate, ReportID, "detailed_page", url);
  134. href = String.Format("<a {0} onclick=\"{1}\">", hrefStyle, onClick);
  135. }
  136. else if (SinglePage)
  137. {
  138. if (obj.Hyperlink.Kind == HyperlinkKind.Bookmark)
  139. href = String.Format("<a {0} href=\"#{1}\">", hrefStyle, url);
  140. else if (obj.Hyperlink.Kind == HyperlinkKind.PageNumber)
  141. href = String.Format("<a {0} href=\"#PageN{1}\">", hrefStyle, url);
  142. }
  143. else
  144. {
  145. string onClick = String.Empty;
  146. if (obj.Hyperlink.Kind == HyperlinkKind.Bookmark)
  147. onClick = String.Format(OnClickTemplate, ReportID, "bookmark", url);
  148. else if (obj.Hyperlink.Kind == HyperlinkKind.PageNumber)
  149. onClick = String.Format(OnClickTemplate, ReportID, "goto", url);
  150. if (onClick != String.Empty)
  151. href = String.Format("<a {0} onclick=\"{1}\">", hrefStyle, onClick);
  152. }
  153. }
  154. return href;
  155. }
  156. private FastString GetSpanText(TextObjectBase obj, FastString text,
  157. float top, float width,
  158. float ParagraphOffset)
  159. {
  160. FastString style = new FastString();
  161. style.Append("display:block;border:0;white-space: pre-wrap;width:").Append(Px(width * Zoom));
  162. if (ParagraphOffset != 0)
  163. style.Append("text-indent:").Append(Px(ParagraphOffset * Zoom));
  164. if (obj.Padding.Left != 0)
  165. style.Append("padding-left:").Append(Px(obj.Padding.Left * Zoom));
  166. if (obj.Padding.Right != 0)
  167. style.Append("padding-right:").Append(Px(obj.Padding.Right * Zoom));
  168. if (obj.Padding.Top != 0)
  169. style.Append("padding-top:").Append(Px(obj.Padding.Top * Zoom));
  170. // if the vertical alignment is not equal to "at the top edge"
  171. if (top != 0)
  172. style.Append("margin-top:").Append(Px((top - obj.Padding.Top) * Zoom));
  173. if (obj is TextObject textObject && !textObject.WordWrap)
  174. style.Append("overflow: hidden; text-wrap: nowrap;");
  175. // we need to apply border width in order to position our div perfectly
  176. float borderLeft;
  177. float borderRight;
  178. float borderTop;
  179. float borderBottom;
  180. if (HTMLBorderWidthValues(obj, out borderLeft, out borderTop, out borderRight, out borderBottom))
  181. {
  182. style.Append("position:absolute;")
  183. .Append("left:").Append(Px(-1 * borderLeft / 2f))
  184. .Append("top:").Append(Px(-1 * borderTop / 2f));
  185. }
  186. FastString result = new FastString(128);
  187. result.Append("<div ").
  188. Append(GetStyle(style.ToString())).Append(">").
  189. Append(text).Append("</div>");
  190. return result;
  191. }
  192. private void LayerText(FastString Page, TextObject obj)
  193. {
  194. float top = 0;
  195. if (obj.Font.FontFamily.Name == "Wingdings" || obj.Font.FontFamily.Name == "Webdings")
  196. {
  197. obj.Text = WingdingsToUnicodeConverter.Convert(obj.Text);
  198. }
  199. switch (obj.TextRenderType)
  200. {
  201. case TextRenderType.HtmlParagraph:
  202. using (HtmlTextRenderer htmlTextRenderer = obj.GetHtmlTextRenderer(Zoom, Zoom))
  203. {
  204. if (obj.VertAlign == VertAlign.Center)
  205. {
  206. top = (obj.Height - htmlTextRenderer.CalcHeight()) / 2;
  207. }
  208. else if (obj.VertAlign == VertAlign.Bottom)
  209. {
  210. top = obj.Height - htmlTextRenderer.CalcHeight();
  211. }
  212. FastString sb = GetHtmlParagraph(htmlTextRenderer);
  213. LayerBack(Page, obj,
  214. GetSpanText(obj, sb,
  215. top + obj.Padding.Top,
  216. obj.Width - obj.Padding.Horizontal,
  217. obj.ParagraphOffset));
  218. }
  219. break;
  220. default:
  221. if (obj.VertAlign != VertAlign.Top)
  222. {
  223. IGraphics g = htmlMeasureGraphics;
  224. using (Font f = new Font(obj.Font.FontFamily, obj.Font.Size * DrawUtils.ScreenDpiFX, obj.Font.Style))
  225. {
  226. RectangleF textRect = new RectangleF(obj.AbsLeft + obj.Padding.Left, obj.AbsTop + obj.Padding.Top,
  227. obj.Width - obj.Padding.Left - obj.Padding.Right,
  228. obj.Height - obj.Padding.Top - obj.Padding.Bottom);
  229. StringFormat format = obj.GetStringFormat(Report.GraphicCache, 0);
  230. Brush textBrush = Report.GraphicCache.GetBrush(obj.TextColor);
  231. AdvancedTextRenderer renderer = new AdvancedTextRenderer(obj.Text, g, f, textBrush, null,
  232. textRect, format, obj.HorzAlign, obj.VertAlign, obj.LineHeight, obj.Angle, obj.FontWidthRatio,
  233. obj.ForceJustify, obj.Wysiwyg, obj.HasHtmlTags, false, Zoom, Zoom, obj.InlineImageCache);
  234. if (renderer.Paragraphs.Count > 0)
  235. {
  236. if (renderer.Paragraphs[0].Lines.Count > 0)
  237. {
  238. float height = renderer.Paragraphs[0].Lines[0].CalcHeight();
  239. if (height > obj.Height)
  240. top = -(height - obj.Height) / 2;
  241. else
  242. {
  243. top = renderer.Paragraphs[0].Lines[0].Top - obj.AbsTop;
  244. height = renderer.CalcHeight();
  245. if (obj.VertAlign == VertAlign.Center)
  246. {
  247. top = (obj.Height - height - obj.Padding.Bottom + obj.Padding.Top) / 2;
  248. if (top < 0)
  249. {
  250. if (obj.Height > height)
  251. top = (obj.Height - height - obj.Padding.Bottom + obj.Padding.Top) / 2;
  252. else
  253. top = (height - obj.Height - obj.Padding.Bottom + obj.Padding.Top) / 2;
  254. }
  255. }
  256. else if (obj.VertAlign == VertAlign.Bottom)
  257. {
  258. // (float)(Math.Round(obj.Font.Size * 96 / 72) / 2
  259. // necessary to compensate for paragraph offset error in GetSpanText method below
  260. top = obj.Height - height - obj.Padding.Bottom - (float)(Math.Round(obj.Font.Size * 96 / 72) / 2);
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. LayerBack(Page, obj,
  268. GetSpanText(obj, ExportUtils.HtmlString(obj.Text, obj.TextRenderType, Px(Math.Round(obj.Font.Size * 96 / 72))),
  269. top,
  270. obj.Width - obj.Padding.Horizontal,
  271. obj.ParagraphOffset));
  272. break;
  273. }
  274. }
  275. private FastString GetHtmlParagraph(HtmlTextRenderer renderer)
  276. {
  277. FastString sb = new FastString();
  278. bool isFirstLine;
  279. foreach (HtmlTextRenderer.Paragraph paragraph in renderer.Paragraphs)
  280. {
  281. isFirstLine = true;
  282. foreach (HtmlTextRenderer.Line line in paragraph.Lines)
  283. {
  284. if (sb == null) sb = new FastString();
  285. sb.Append("<span style=\"");
  286. sb.Append("display:block;");
  287. if (isFirstLine)
  288. sb.Append($"text-indent:{Math.Floor(renderer.ParagraphFormat.FirstLineIndent)}px;");
  289. if (line.Top + line.Height > renderer.DisplayRect.Bottom)
  290. sb.Append("height:").Append(Math.Max(renderer.DisplayRect.Bottom - line.Top, 0).ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
  291. else
  292. {
  293. //sb.Append("height:").Append(line.Height.ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
  294. if (line.LineSpacing > 0)
  295. {
  296. sb.Append("margin-bottom:").Append(line.LineSpacing.ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
  297. }
  298. }
  299. if (!isFirstLine)
  300. sb.Append("overflow:hidden;");
  301. sb.Append("line-height:").Append(line.Height.ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
  302. if (line.HorzAlign == HorzAlign.Justify)
  303. sb.Append("text-align-last:justify;");
  304. else sb.Append("white-space:pre;");
  305. sb.Append("\">");
  306. HtmlTextRenderer.StyleDescriptor styleDesc = null;
  307. float prevWidth = 0;
  308. foreach (HtmlTextRenderer.Word word in line.Words)
  309. {
  310. foreach (HtmlTextRenderer.Run run in word.Runs)
  311. {
  312. if (!run.Style.FullEquals(styleDesc))
  313. {
  314. if (styleDesc != null)
  315. styleDesc.ToHtml(sb, true);
  316. styleDesc = run.Style;
  317. styleDesc.ToHtml(sb, false);
  318. }
  319. if (run is HtmlTextRenderer.RunText)
  320. {
  321. HtmlTextRenderer.RunText runText = run as HtmlTextRenderer.RunText;
  322. foreach (char ch in runText.Text)
  323. {
  324. switch (ch)
  325. {
  326. case '"':
  327. sb.Append("&quot;");
  328. break;
  329. case '&':
  330. sb.Append("&amp;");
  331. break;
  332. case '<':
  333. sb.Append("&lt;");
  334. break;
  335. case '>':
  336. sb.Append("&gt;");
  337. break;
  338. case '\t':
  339. if (word.Type == HtmlTextRenderer.WordType.Tab)
  340. {
  341. if (layers)
  342. sb.Append($"<span style=\"tab-size: {Math.Round(prevWidth + run.Width)}px;\">&Tab;</span>");
  343. else
  344. sb.Append($"<span style=\"tab-size: {Math.Round(run.Left + run.Width)}px;\">&Tab;</span>");
  345. }
  346. else
  347. sb.Append("&Tab;");
  348. break;
  349. default:
  350. sb.Append(ch);
  351. break;
  352. }
  353. }
  354. }
  355. else if (run is HtmlTextRenderer.RunImage)
  356. {
  357. HtmlTextRenderer.RunImage runImage = run as HtmlTextRenderer.RunImage;
  358. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  359. {
  360. try
  361. {
  362. float w, h;
  363. using (Bitmap bmp = runImage.GetBitmap(out w, out h))
  364. {
  365. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  366. }
  367. ms.Flush();
  368. sb.Append("<img src=\"data:image/png;base64,").Append(Convert.ToBase64String(ms.ToArray()))
  369. .Append("\" width=\"").Append(w.ToString(HtmlTextRenderer.CultureInfo)).Append("\" height=\"").Append(h.ToString(HtmlTextRenderer.CultureInfo)).Append("\"/>");
  370. }
  371. catch (Exception /*e*/)
  372. {
  373. }
  374. }
  375. }
  376. prevWidth += run.Width;
  377. //run.ToHtml(sb, true);
  378. }
  379. }
  380. if (styleDesc != null)
  381. styleDesc.ToHtml(sb, true);
  382. else sb.Append("<br/>");
  383. sb.Append("</span>");
  384. isFirstLine = false;
  385. }
  386. }
  387. return sb;
  388. }
  389. private void LayerHtml(FastString page, HtmlObject obj)
  390. {
  391. LayerBack(page, obj,
  392. GetSpanText(obj, new FastString(obj.Text),
  393. obj.Padding.Top,
  394. obj.Width - obj.Padding.Horizontal,
  395. 0));
  396. }
  397. private string GetLayerPicture(ReportComponentBase obj, out float Width, out float Height)
  398. {
  399. string result = String.Empty;
  400. Width = 0;
  401. Height = 0;
  402. if (obj != null)
  403. {
  404. if (pictures)
  405. {
  406. MemoryStream PictureStream = new MemoryStream();
  407. System.Drawing.Imaging.ImageFormat FPictureFormat = System.Drawing.Imaging.ImageFormat.Bmp;
  408. if (imageFormat == ImageFormat.Png)
  409. FPictureFormat = System.Drawing.Imaging.ImageFormat.Png;
  410. else if (imageFormat == ImageFormat.Jpeg)
  411. FPictureFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
  412. else if (imageFormat == ImageFormat.Gif)
  413. FPictureFormat = System.Drawing.Imaging.ImageFormat.Gif;
  414. Width = obj.Width == 0 ? obj.Border.LeftLine.Width : obj.Width;
  415. Height = obj.Height == 0 ? obj.Border.TopLine.Width : obj.Height;
  416. if (Math.Abs(Width) * Zoom < 1 && Zoom > 0)
  417. Width = 1 / Zoom;
  418. if (Math.Abs(Height) * Zoom < 1 && Zoom > 0)
  419. Height = 1 / Zoom;
  420. int zoom = highQualitySVG ? 3 : 1;
  421. using (System.Drawing.Image image =
  422. new Bitmap(
  423. (int)(Math.Abs(Math.Round(Width * Zoom * zoom))),
  424. (int)(Math.Abs(Math.Round(Height * Zoom * zoom)))
  425. ))
  426. {
  427. using (Graphics g = Graphics.FromImage(image))
  428. {
  429. var needClear = obj is TextObjectBase
  430. #if MSCHART
  431. || obj is MSChart.MSChartObject
  432. #endif
  433. || obj is Gauge.GaugeObject;
  434. if (needClear)
  435. {
  436. g.Clear(imageFormat == ImageFormat.Bmp ? Color.White : Color.Transparent);
  437. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  438. }
  439. float Left = Width > 0 ? obj.AbsLeft : obj.AbsLeft + Width;
  440. float Top = Height > 0 ? obj.AbsTop : obj.AbsTop + Height;
  441. float dx = 0;
  442. float dy = 0;
  443. g.TranslateTransform((-Left - dx) * Zoom * zoom, (-Top - dy) * Zoom * zoom);
  444. BorderLines oldLines = obj.Border.Lines;
  445. obj.Border.Lines = BorderLines.None;
  446. obj.Draw(new FRPaintEventArgs(g, Zoom * zoom, Zoom * zoom, Report.GraphicCache));
  447. obj.Border.Lines = oldLines;
  448. }
  449. using (Bitmap b = new Bitmap(
  450. (int)(Math.Abs(Math.Round(Width * Zoom))),
  451. (int)(Math.Abs(Math.Round(Height * Zoom)))
  452. ))
  453. {
  454. using (Graphics gr = Graphics.FromImage(b))
  455. {
  456. gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  457. gr.DrawImage(image, 0, 0, (int)Math.Abs(Width) * Zoom, (int)Math.Abs(Height) * Zoom);
  458. }
  459. if (FPictureFormat == System.Drawing.Imaging.ImageFormat.Jpeg)
  460. ExportUtils.SaveJpeg(b, PictureStream, 95);
  461. else
  462. b.Save(PictureStream, FPictureFormat);
  463. }
  464. }
  465. PictureStream.Position = 0;
  466. string hash = String.Empty;
  467. if (obj is PictureObject)
  468. {
  469. PictureObject pic = (obj as PictureObject);
  470. if (pic.Image != null)
  471. {
  472. #if MONO
  473. using (MemoryStream picStr = new MemoryStream())
  474. {
  475. ImageHelper.Save(pic.Image, picStr);
  476. using(StreamWriter picWriter = new StreamWriter(picStr))
  477. {
  478. picWriter.Write(pic.Width);
  479. picWriter.Write(pic.Height);
  480. picWriter.Write(pic.Angle);
  481. picWriter.Write(pic.Transparency);
  482. picWriter.Write(pic.TransparentColor.ToArgb());
  483. picWriter.Write(pic.CanShrink);
  484. picWriter.Write(pic.CanGrow);
  485. hash = Crypter.ComputeHash(picStr);
  486. }
  487. }
  488. #else
  489. hash = Crypter.ComputeHash(PictureStream);
  490. PictureStream.Position = 0;
  491. #endif
  492. }
  493. }
  494. else
  495. hash = Crypter.ComputeHash(PictureStream);
  496. result = HTMLGetImage(0, 0, 0, hash, true, null, PictureStream, false);
  497. }
  498. }
  499. return result;
  500. }
  501. private void LayerPicture(FastString Page, ReportComponentBase obj, FastString text)
  502. {
  503. if (pictures)
  504. {
  505. string old_text = String.Empty;
  506. if (IsMemo(obj))
  507. {
  508. old_text = (obj as TextObject).Text;
  509. (obj as TextObject).Text = String.Empty;
  510. }
  511. float Width, Height;
  512. string pic = GetLayerPicture(obj, out Width, out Height);
  513. if (IsMemo(obj))
  514. (obj as TextObject).Text = old_text;
  515. FastString picStyleBuilder = new FastString("background: url('")
  516. .Append(pic).Append("') no-repeat !important;-webkit-print-color-adjust:exact;");
  517. string style = GetStyle(obj, picStyleBuilder.ToString());
  518. //FastString addstyle = new FastString(128);
  519. //addstyle.Append(" background: url('").Append(pic).Append("') no-repeat !important;-webkit-print-color-adjust:exact;");
  520. //if (String.IsNullOrEmpty(text))
  521. // text = NBSP;
  522. float x = Width > 0 ? obj.AbsLeft : (obj.AbsLeft + Width);
  523. float y = Height > 0 ? hPos + obj.AbsTop : (hPos + obj.AbsTop + Height);
  524. Layer(Page, obj, x, y, Width, Height, text, style, null);
  525. }
  526. }
  527. private void LayerShape(FastString Page, ShapeObject obj, FastString text)
  528. {
  529. float Width, Height;
  530. FastString addstyle = new FastString(64);
  531. addstyle.Append("position:absolute;");
  532. addstyle.Append("background: url('" + GetLayerPicture(obj, out Width, out Height) + "');no-repeat !important;-webkit-print-color-adjust:exact;");
  533. float x = obj.Width > 0 ? obj.AbsLeft : (obj.AbsLeft + obj.Width);
  534. float y = obj.Height > 0 ? hPos + obj.AbsTop : (hPos + obj.AbsTop + obj.Height);
  535. Layer(Page, obj, x, y, obj.Width, obj.Height, text, null, addstyle);
  536. }
  537. private void LayerBack(FastString Page, ReportComponentBase obj, FastString text)
  538. {
  539. if (obj.Border.Shadow)
  540. {
  541. using (TextObject shadow = new TextObject())
  542. {
  543. shadow.Left = obj.AbsLeft + obj.Border.ShadowWidth + obj.Border.LeftLine.Width;
  544. shadow.Top = obj.AbsTop + obj.Height + obj.Border.BottomLine.Width;
  545. shadow.Width = obj.Width + obj.Border.RightLine.Width;
  546. shadow.Height = obj.Border.ShadowWidth + obj.Border.BottomLine.Width;
  547. shadow.FillColor = obj.Border.ShadowColor;
  548. shadow.Border.Lines = BorderLines.None;
  549. LayerBack(Page, shadow, null);
  550. shadow.Left = obj.AbsLeft + obj.Width + obj.Border.RightLine.Width;
  551. shadow.Top = obj.AbsTop + obj.Border.ShadowWidth + obj.Border.TopLine.Width;
  552. shadow.Width = obj.Border.ShadowWidth + obj.Border.RightLine.Width;
  553. shadow.Height = obj.Height;
  554. LayerBack(Page, shadow, null);
  555. }
  556. }
  557. if (!(obj is PolyLineObject))
  558. {
  559. if (obj.Fill is SolidFill)
  560. Layer(Page, obj, obj.AbsLeft, hPos + obj.AbsTop, obj.Width, obj.Height, text, GetStyle(obj), null);
  561. else
  562. LayerPicture(Page, obj, text);
  563. }
  564. }
  565. private void LayerTable(FastString Page, FastString CSS, TableBase table)
  566. {
  567. float y = 0;
  568. for (int i = 0; i < table.RowCount; i++)
  569. {
  570. float x = 0;
  571. for (int j = 0; j < table.ColumnCount; j++)
  572. {
  573. if (!table.IsInsideSpan(table[j, i]))
  574. {
  575. TableCell textcell = table[j, i];
  576. textcell.Left = x;
  577. textcell.Top = y;
  578. // custom draw
  579. CustomDrawEventArgs e = new CustomDrawEventArgs();
  580. e.report = Report;
  581. e.reportObject = textcell;
  582. e.layers = Layers;
  583. e.zoom = Zoom;
  584. e.left = textcell.AbsLeft;
  585. e.top = hPos + textcell.AbsTop;
  586. e.width = textcell.Width;
  587. e.height = textcell.Height;
  588. OnCustomDraw(e);
  589. if (e.done)
  590. {
  591. CSS.Append(e.css);
  592. Page.Append(e.html);
  593. }
  594. else
  595. {
  596. if (textcell is TextObject && !(textcell as TextObject).TextOutline.Enabled && IsMemo(textcell))
  597. LayerText(Page, textcell as TextObject);
  598. else
  599. {
  600. LayerBack(Page, textcell as ReportComponentBase, null);
  601. LayerPicture(Page, textcell as ReportComponentBase, null);
  602. }
  603. }
  604. }
  605. x += (table.Columns[j]).Width;
  606. }
  607. y += (table.Rows[i]).Height;
  608. }
  609. }
  610. private bool IsMemo(ReportComponentBase Obj)
  611. {
  612. if (Obj is TextObject)
  613. {
  614. TextObject aObj = Obj as TextObject;
  615. return (aObj.Angle == 0) && (aObj.FontWidthRatio == 1) && (!aObj.TextOutline.Enabled) && (!aObj.Underlines);
  616. }
  617. return false;
  618. }
  619. private void Watermark(FastString Page, ReportPage page, bool drawText)
  620. {
  621. using (PictureObject pictureWatermark = new PictureObject())
  622. {
  623. pictureWatermark.Left = 0;
  624. pictureWatermark.Top = 0;
  625. pictureWatermark.Width = (ExportUtils.GetPageWidth(page) - page.LeftMargin - page.RightMargin) * Units.Millimeters;
  626. pictureWatermark.Height = (ExportUtils.GetPageHeight(page) - page.TopMargin - page.BottomMargin) * Units.Millimeters;
  627. pictureWatermark.SizeMode = PictureBoxSizeMode.Normal;
  628. pictureWatermark.Image = new Bitmap((int)pictureWatermark.Width, (int)pictureWatermark.Height);
  629. using (Graphics g = Graphics.FromImage(pictureWatermark.Image))
  630. {
  631. g.Clear(Color.Transparent);
  632. if (drawText)
  633. page.Watermark.DrawText(new FRPaintEventArgs(g, 1f, 1f, Report.GraphicCache),
  634. new RectangleF(0, 0, pictureWatermark.Width, pictureWatermark.Height), Report, true);
  635. else
  636. page.Watermark.DrawImage(new FRPaintEventArgs(g, 1f, 1f, Report.GraphicCache),
  637. new RectangleF(0, 0, pictureWatermark.Width, pictureWatermark.Height), Report, true);
  638. pictureWatermark.Transparency = page.Watermark.ImageTransparency;
  639. LayerBack(Page, pictureWatermark, null);
  640. LayerPicture(Page, pictureWatermark, null);
  641. }
  642. }
  643. }
  644. private void ExportHTMLPageLayeredBegin(HTMLData d)
  645. {
  646. if (!singlePage && !WebMode)
  647. cssStyles.Clear();
  648. css.Clear();
  649. htmlPage.Clear();
  650. ReportPage reportPage = d.page;
  651. if (reportPage != null)
  652. {
  653. maxWidth = ExportUtils.GetPageWidth(reportPage) * Units.Millimeters;
  654. maxHeight = ExportUtils.GetPageHeight(reportPage) * Units.Millimeters;
  655. if (enableMargins)
  656. {
  657. leftMargin = reportPage.LeftMargin * Units.Millimeters;
  658. topMargin = reportPage.TopMargin * Units.Millimeters;
  659. }
  660. else
  661. {
  662. maxWidth -= (reportPage.LeftMargin + reportPage.RightMargin) * Units.Millimeters;
  663. maxHeight -= (reportPage.TopMargin + reportPage.BottomMargin) * Units.Millimeters;
  664. leftMargin = 0;
  665. topMargin = 0;
  666. }
  667. currentPage = d.PageNumber - 1;
  668. ExportHTMLPageStart(htmlPage, d.PageNumber, d.CurrentPage);
  669. doPageBreak = singlePage && pageBreaks;
  670. htmlPage.Append(HTMLGetAncor(d.PageNumber.ToString()));
  671. if (doPageBreak && d.PageNumber > 1)
  672. htmlPage.Append("<div style=\"break-after:page\"></div>");
  673. pageStyleName = "frpage" + currentPage;
  674. htmlPage.Append("<div ").Append(doPageBreak ? "class=\"" + pageStyleName + "\"" : String.Empty)
  675. .Append(" style=\"position:relative;")
  676. .Append(" width:").Append(Px(maxWidth * Zoom + 3))
  677. .Append(" height:").Append(Px(maxHeight * Zoom));
  678. if (reportPage.Fill is SolidFill)
  679. {
  680. SolidFill fill = reportPage.Fill as SolidFill;
  681. htmlPage.Append(" background-color:").
  682. Append(fill.IsTransparent ? "transparent" : ExportUtils.HTMLColor(fill.Color));
  683. if (exportMode == ExportType.WebPrint)
  684. htmlPage.Append("color-adjust: exact !important; print-color-adjust: exact !important; -webkit-print-color-adjust: exact !important;");
  685. }
  686. else
  687. {
  688. // to-do for picture background
  689. }
  690. htmlPage.Append("\">");
  691. if (reportPage.Watermark.Enabled && !reportPage.Watermark.ShowImageOnTop)
  692. Watermark(htmlPage, reportPage, false);
  693. if (reportPage.Watermark.Enabled && !reportPage.Watermark.ShowTextOnTop)
  694. Watermark(htmlPage, reportPage, true);
  695. }
  696. }
  697. private void ExportHTMLPageLayeredEnd(HTMLData d)
  698. {
  699. // to do
  700. if (d.page != null && d.page.Watermark.Enabled)
  701. {
  702. if (d.page.Watermark.ShowImageOnTop)
  703. Watermark(htmlPage, d.page, false);
  704. if (d.page.Watermark.ShowTextOnTop)
  705. Watermark(htmlPage, d.page, true);
  706. }
  707. ExportPageStylesLayers(css, d.PageNumber);
  708. if (singlePage)
  709. {
  710. hPos = 0;
  711. prevStyleListIndex = cssStyles.Count;
  712. }
  713. htmlPage.Append("</div>");
  714. ExportHTMLPageFinal(css, htmlPage, d, maxWidth, maxHeight);
  715. }
  716. /// <summary>
  717. /// For developers only
  718. /// </summary>
  719. [EditorBrowsable(EditorBrowsableState.Never)]
  720. public void ExportReportObject(ReportComponentBase obj)
  721. {
  722. //Init();
  723. var band = new ReportTitleBand();
  724. obj.SetParent(band);
  725. if (obj is ITranslatable translatableObject)
  726. {
  727. translatableObject.ConvertToReportObjects();
  728. }
  729. Init_WebMode();
  730. //InlineStyles = true;
  731. //SaveStreams = true;
  732. EmbedPictures = true;
  733. //Start();
  734. count = 1;
  735. StartWeb();
  736. ExportBandLayers(band);
  737. ProcessPageEnd(0, 0);
  738. }
  739. private void ExportObject(ReportComponentBase obj)
  740. {
  741. if (!String.IsNullOrEmpty(obj.Bookmark))
  742. htmlPage.Append("<a name=\"").Append(obj.Bookmark).Append("\"></a>");
  743. if (obj is CellularTextObject)
  744. obj = (obj as CellularTextObject).GetTable();
  745. if (obj is TableCell)
  746. return;
  747. else if (obj is TableBase)
  748. {
  749. TableBase table = obj as TableBase;
  750. if (table.ColumnCount > 0 && table.RowCount > 0)
  751. {
  752. using (TextObject tableback = new TextObject())
  753. {
  754. tableback.Border = table.Border;
  755. tableback.Fill = table.Fill;
  756. tableback.FillColor = table.FillColor;
  757. tableback.Left = table.AbsLeft;
  758. tableback.Top = table.AbsTop;
  759. float tableWidth = 0;
  760. float tableHeight = 0;
  761. for (int i = 0; i < table.ColumnCount; i++)
  762. tableWidth += table[i, 0].Width;
  763. for (int i = 0; i < table.RowCount; i++)
  764. tableHeight += table.Rows[i].Height;
  765. tableback.Width = (tableWidth < table.Width) ? tableWidth : table.Width;
  766. tableback.Height = tableHeight;
  767. LayerText(htmlPage, tableback);
  768. }
  769. LayerTable(htmlPage, css, table);
  770. }
  771. }
  772. else if (IsMemo(obj))
  773. {
  774. LayerText(htmlPage, obj as TextObject);
  775. }
  776. else if (obj is HtmlObject)
  777. {
  778. LayerHtml(htmlPage, obj as HtmlObject);
  779. }
  780. else if (obj is BandBase)
  781. {
  782. LayerBack(htmlPage, obj, null);
  783. }
  784. else if (obj is LineObject)
  785. {
  786. LayerPicture(htmlPage, obj, null);
  787. }
  788. else if (obj is ShapeObject)
  789. {
  790. LayerShape(htmlPage, obj as ShapeObject, null);
  791. }
  792. else if (HasExtendedExport(obj))
  793. {
  794. ExtendExport(htmlPage, obj, null);
  795. }
  796. else
  797. {
  798. LayerBack(htmlPage, obj, null);
  799. LayerPicture(htmlPage, obj, null);
  800. }
  801. }
  802. private void ExportBandLayers(BandBase band)
  803. {
  804. LayerBack(htmlPage, band, null);
  805. foreach (Base c in band.ForEachAllConvectedObjects(this))
  806. {
  807. if (ExportMode == ExportType.WebPreview)
  808. SetExportableAdvMatrix(c);
  809. if (c is ReportComponentBase obj && obj.Exportable)
  810. {
  811. // custom draw
  812. CustomDrawEventArgs e = new CustomDrawEventArgs();
  813. e.report = Report;
  814. e.reportObject = obj;
  815. e.layers = Layers;
  816. e.zoom = Zoom;
  817. e.left = obj.AbsLeft;
  818. e.top = hPos + obj.AbsTop;
  819. e.width = obj.Width;
  820. e.height = obj.Height;
  821. OnCustomDraw(e);
  822. if (e.done)
  823. {
  824. css.Append(e.css);
  825. htmlPage.Append(e.html);
  826. }
  827. else
  828. {
  829. ExportObject(obj);
  830. }
  831. }
  832. }
  833. }
  834. }
  835. }