PPMLExport.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using System;
  2. using System.Collections.Generic;
  3. using FastReport.Utils;
  4. using System.IO;
  5. using System.Drawing;
  6. using FastReport.Table;
  7. using System.Collections;
  8. using System.Drawing.Imaging;
  9. using System.Windows.Forms;
  10. namespace FastReport.Export.Ppml
  11. {
  12. /// <summary>
  13. /// Represents the PPML export filter.
  14. /// </summary>
  15. public partial class PPMLExport : ExportBase
  16. {
  17. #region Private Fields
  18. private string path;
  19. private string fileNameWOext;
  20. private string extension;
  21. private string pageFileName;
  22. private int quality;
  23. private int currentPage;
  24. private int img_nbr;
  25. private bool pictures;
  26. private bool textInCurves;
  27. private float pageLeftMargin;
  28. private float pageTopMargin;
  29. private Hashtable hashtable;
  30. private ImageFormat format;
  31. private Dictionary<string, MemoryStream> images;
  32. private PPMLDocument ppml;
  33. #endregion
  34. #region Properties
  35. /// <summary>
  36. /// Enable or disable the pictures in PPML export
  37. /// </summary>
  38. public bool Pictures
  39. {
  40. get { return pictures; }
  41. set { pictures = value; }
  42. }
  43. public bool TextInCurves
  44. {
  45. get { return textInCurves; }
  46. set { textInCurves = value; }
  47. }
  48. public int Quality
  49. {
  50. get { return quality; }
  51. set { quality = value; }
  52. }
  53. #endregion
  54. #region Private Methods
  55. private void ExportObj(Base c)
  56. {
  57. if (c is ReportComponentBase && (c as ReportComponentBase).Exportable)
  58. {
  59. ReportComponentBase obj = c as ReportComponentBase;
  60. if (obj is CellularTextObject)
  61. obj = (obj as CellularTextObject).GetTable();
  62. else if (obj is TableBase)
  63. {
  64. TableBase table = obj as TableBase;
  65. if (table.ColumnCount > 0 && table.RowCount > 0)
  66. {
  67. using (TextObject tableback = new TextObject())
  68. {
  69. tableback.Border = table.Border;
  70. tableback.Fill = table.Fill;
  71. tableback.FillColor = table.FillColor;
  72. tableback.Left = table.AbsLeft;
  73. tableback.Top = table.AbsTop;
  74. float tableWidth = 0;
  75. float tableHeight = 0;
  76. for (int i = 0; i < table.ColumnCount; i++)
  77. tableWidth += table[i, 0].Width;
  78. for (int i = 0; i < table.RowCount; i++)
  79. tableHeight += table.Rows[i].Height;
  80. tableback.Width = (tableWidth < table.Width) ? tableWidth : table.Width;
  81. tableback.Height = tableHeight;
  82. AddTextObject(ppml, tableback as TextObject, false);
  83. // draw cells
  84. AddTable(ppml, table, true);
  85. // draw cells border
  86. AddTable(ppml, table, false);
  87. // draw table border
  88. AddBorder(ppml, tableback.Border, tableback.AbsLeft, tableback.AbsTop, tableback.Width, tableback.Height);
  89. }
  90. }
  91. }
  92. else if (obj is TextObject)
  93. AddTextObject(ppml, obj as TextObject, false);
  94. else if (obj is BandBase)
  95. AddBandObject(ppml, obj as BandBase);
  96. else if (obj is LineObject)
  97. AddLine(ppml, obj as LineObject);
  98. else if (obj is ShapeObject)
  99. AddShape(ppml, obj as ShapeObject);
  100. else
  101. AddPictureObject(ppml, obj as ReportComponentBase);
  102. }
  103. }
  104. private void AddBitmapWatermark(ReportPage page)
  105. {
  106. if (page.Watermark.Image != null)
  107. {
  108. using (PictureObject pictureWatermark = new PictureObject())
  109. {
  110. pictureWatermark.Left = page.LeftMargin;
  111. pictureWatermark.Top = page.TopMargin;
  112. pictureWatermark.Width = ExportUtils.GetPageWidth(page) * Units.Millimeters;
  113. pictureWatermark.Height = ExportUtils.GetPageHeight(page) * Units.Millimeters;
  114. pictureWatermark.SizeMode = PictureBoxSizeMode.Normal;
  115. pictureWatermark.Image = new Bitmap((int)pictureWatermark.Width, (int)pictureWatermark.Height);
  116. using (Graphics g = Graphics.FromImage(pictureWatermark.Image))
  117. {
  118. g.Clear(Color.Transparent);
  119. page.Watermark.DrawImage(new FRPaintEventArgs(g, 1, 1, Report.GraphicCache),
  120. new RectangleF(0, 0, pictureWatermark.Width, pictureWatermark.Height), Report, true);
  121. }
  122. pictureWatermark.Transparency = page.Watermark.ImageTransparency;
  123. pictureWatermark.Fill = new SolidFill(Color.Transparent);
  124. pictureWatermark.FillColor = Color.Transparent;
  125. AddPictureObject(ppml, pictureWatermark);
  126. }
  127. }
  128. }
  129. private void AddTextWatermark(ReportPage page)
  130. {
  131. if (!String.IsNullOrEmpty(page.Watermark.Text))
  132. using (TextObject textWatermark = new TextObject())
  133. {
  134. textWatermark.HorzAlign = HorzAlign.Center;
  135. textWatermark.VertAlign = VertAlign.Center;
  136. textWatermark.Left = page.LeftMargin;
  137. textWatermark.Top = page.TopMargin;
  138. textWatermark.Width = ExportUtils.GetPageWidth(page) * Units.Millimeters;
  139. textWatermark.Height = ExportUtils.GetPageHeight(page) * Units.Millimeters;
  140. textWatermark.Text = page.Watermark.Text;
  141. textWatermark.TextFill = page.Watermark.TextFill;
  142. if (page.Watermark.TextRotation == WatermarkTextRotation.Vertical)
  143. textWatermark.Angle = 270;
  144. else if (page.Watermark.TextRotation == WatermarkTextRotation.ForwardDiagonal)
  145. textWatermark.Angle = 360 - (int)(Math.Atan(textWatermark.Height / textWatermark.Width) * (180 / Math.PI));
  146. else if (page.Watermark.TextRotation == WatermarkTextRotation.BackwardDiagonal)
  147. textWatermark.Angle = (int)(Math.Atan(textWatermark.Height / textWatermark.Width) * (180 / Math.PI));
  148. textWatermark.Font = page.Watermark.Font;
  149. if (page.Watermark.TextFill is SolidFill)
  150. textWatermark.TextColor = (page.Watermark.TextFill as SolidFill).Color;
  151. textWatermark.Fill = new SolidFill(Color.Transparent);
  152. textWatermark.FillColor = Color.Transparent;
  153. AddTextObject(ppml, textWatermark, false);
  154. }
  155. }
  156. private void SaveImgsToFiles()
  157. {
  158. foreach (DictionaryEntry fl_nm_e in hashtable)
  159. {
  160. foreach (KeyValuePair<string, MemoryStream> img_e in images)
  161. {
  162. if (fl_nm_e.Key.ToString() == img_e.Key.ToString())
  163. {
  164. string fullImagePath = Path.Combine(path, fl_nm_e.Value.ToString()) + "." + format.ToString().ToLower();
  165. using (FileStream file = new FileStream(fullImagePath, FileMode.Create))
  166. img_e.Value.WriteTo(file);
  167. }
  168. }
  169. }
  170. }
  171. private void AddTable(PPMLDocument ppml, TableBase table, bool drawCells)
  172. {
  173. float y = 0;
  174. for (int i = 0; i < table.RowCount; i++)
  175. {
  176. float x = 0;
  177. for (int j = 0; j < table.ColumnCount; j++)
  178. {
  179. if (!table.IsInsideSpan(table[j, i]))
  180. {
  181. TableCell textcell = table[j, i];
  182. textcell.Left = x;
  183. textcell.Top = y;
  184. if (drawCells)
  185. {
  186. Border oldBorder = textcell.Border.Clone();
  187. textcell.Border.Lines = BorderLines.None;
  188. if ((textcell as TextObject) is TextObject)
  189. AddTextObject(ppml, textcell as TextObject, false);
  190. else
  191. AddPictureObject(ppml, textcell as ReportComponentBase);
  192. textcell.Border = oldBorder;
  193. }
  194. else
  195. AddBorder(ppml, textcell.Border, textcell.AbsLeft, textcell.AbsTop, textcell.Width, textcell.Height);
  196. }
  197. x += (table.Columns[j]).Width;
  198. }
  199. y += (table.Rows[i]).Height;
  200. }
  201. }
  202. private void AddPictureObject(PPMLDocument ppml, ReportComponentBase obj)
  203. {
  204. if (obj.Height <= 0 || obj.Width <= 0)
  205. return;
  206. if (pictures)
  207. {
  208. MemoryStream imageStream = new MemoryStream();
  209. using (System.Drawing.Image image = new Bitmap((int)obj.Width, (int)obj.Height))
  210. {
  211. using (Graphics g = Graphics.FromImage(image))
  212. {
  213. using (GraphicCache cache = new GraphicCache())
  214. {
  215. g.Clear(Color.White);
  216. float Left = obj.Width >= 0 ? obj.AbsLeft : obj.AbsLeft + obj.Width;
  217. float Top = obj.Height >= 0 ? obj.AbsTop : obj.AbsTop + obj.Height;
  218. g.TranslateTransform(-Left, -Top);
  219. obj.Draw(new FRPaintEventArgs(g, 1, 1, cache));
  220. }
  221. }
  222. ExportUtils.SaveJpeg(image, imageStream, quality);
  223. }
  224. imageStream.Position = 0;
  225. string hash = Crypter.ComputeHash(imageStream);
  226. string imageFileName = fileNameWOext.Replace(" ", "") + hashtable.Count.ToString();
  227. if (!hashtable.ContainsKey(hash))
  228. {
  229. hashtable.Add(hash, imageFileName);
  230. if (path != null && path != "")
  231. GeneratedFiles.Add(imageFileName);
  232. images.Add(hash, imageStream);
  233. }
  234. else
  235. imageFileName = hashtable[hash] as string;
  236. // add image
  237. ppml.AddImage(imageFileName, format.ToString().ToLower(), (obj.AbsLeft + pageLeftMargin) * 0.75f, (obj.AbsTop + pageTopMargin) * 0.75f, obj.Width, obj.Height);
  238. img_nbr++;
  239. }
  240. }
  241. private void AddBorder(PPMLDocument ppml, Border border, float Left, float Top, float Width, float Height)
  242. {
  243. if (border.Lines != BorderLines.None)
  244. {
  245. using (TextObject emptyText = new TextObject())
  246. {
  247. emptyText.Left = Left;
  248. emptyText.Top = Top;
  249. emptyText.Width = Width;
  250. emptyText.Height = Height;
  251. emptyText.Border = border;
  252. emptyText.Text = String.Empty;
  253. emptyText.FillColor = Color.Transparent;
  254. AddTextObject(ppml, emptyText, true);
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// Add TextObject.
  260. /// </summary>
  261. private void AddTextObject(PPMLDocument ppml, TextObject text, bool Band)
  262. {
  263. Font font = text.Font;
  264. float AbsLeft = text.AbsLeft + pageLeftMargin;
  265. float AbsTop = text.AbsTop + pageTopMargin;
  266. float Width = text.Width;
  267. float Height = text.Height;
  268. string HorzAlign = Convert.ToString(text.HorzAlign);
  269. string VertAlign = Convert.ToString(text.VertAlign);
  270. float BorderWidth = text.Border.Width;
  271. string BorderLines = Convert.ToString(text.Border.Lines);
  272. string Text = text.Text;
  273. float FontSize = text.Font.Size;
  274. string FontName = Convert.ToString(text.Font.Name);
  275. bool Bold = text.Font.Bold;
  276. bool Italic = text.Font.Italic;
  277. bool Underline = text.Font.Underline;
  278. float PaddingLeft = text.Padding.Left;
  279. float PaddingTop = text.Padding.Top;
  280. float PaddingRight = text.Padding.Right;
  281. float PaddingBottom = text.Padding.Bottom;
  282. bool WordWrap = text.WordWrap;
  283. string BorderBrush;
  284. string Background;
  285. string Foreground;
  286. float Angle = text.Angle;
  287. float LeftLine = text.Border.LeftLine.Width;
  288. float TopLine = text.Border.TopLine.Width;
  289. float RightLine = text.Border.RightLine.Width;
  290. float BottomLine = text.Border.BottomLine.Width;
  291. //Dash------------------------------------------------
  292. string LeftLineDashStile = Convert.ToString(text.Border.LeftLine.Style);
  293. string TopLineDashStile = Convert.ToString(text.Border.TopLine.Style);
  294. string RightLineDashStile = Convert.ToString(text.Border.RightLine.Style);
  295. string BottomLineDashStile = Convert.ToString(text.Border.BottomLine.Style);
  296. string colorLeftLine = ExportUtils.HTMLColorCode(text.Border.LeftLine.Color);
  297. string colorTopLine = ExportUtils.HTMLColorCode(text.Border.TopLine.Color);
  298. string colorRightLine = ExportUtils.HTMLColorCode(text.Border.RightLine.Color);
  299. string colorBottomLine = ExportUtils.HTMLColorCode(text.Border.BottomLine.Color);
  300. //GlassFill
  301. bool Glass = false;
  302. string colorTop = null;
  303. if (text.Fill is GlassFill)
  304. {
  305. Glass = true;
  306. Color color = GetBlendColor((text.Fill as GlassFill).Color, (text.Fill as GlassFill).Blend);
  307. colorTop = ExportUtils.HTMLColorCode(color);
  308. }
  309. NormalizeBorderBrushColor(text, out BorderBrush);
  310. NormalizeForegroundColor(text, out Foreground);
  311. NormalizeBackgroundColor(text, out Background);
  312. //Shadow----
  313. float ShadowWidth = text.Border.ShadowWidth;
  314. string ShadowColor = ExportUtils.HTMLColorCode(text.Border.ShadowColor);
  315. if (Band)
  316. {
  317. HorzAlign = null;
  318. VertAlign = null;
  319. Text = null;
  320. FontSize = 0;
  321. Foreground = null;
  322. FontName = null;
  323. Bold = false;
  324. Italic = false;
  325. Underline = false;
  326. PaddingLeft = 0;
  327. PaddingTop = 0;
  328. PaddingRight = 0;
  329. PaddingBottom = 0;
  330. WordWrap = false;
  331. }
  332. ppml.AddTextObject(AbsLeft * 0.75f, AbsTop * 0.75f, Width * 0.75f, Height * 0.75f, HorzAlign, VertAlign, BorderBrush,
  333. BorderWidth * 0.75f, LeftLine * 0.75f, TopLine * 0.75f, RightLine * 0.75f, BottomLine * 0.75f, LeftLineDashStile, TopLineDashStile,
  334. RightLineDashStile, BottomLineDashStile, colorLeftLine, colorTopLine, colorRightLine, colorBottomLine,
  335. text.Border.Shadow, ShadowColor, ShadowWidth * 0.75f, Background, BorderLines, Text, Foreground, PaddingLeft * 0.75f, PaddingTop * 0.75f, PaddingRight * 0.75f, PaddingBottom * 0.75f, WordWrap, Angle, Glass, colorTop, font, false);
  336. }
  337. /// <summary>
  338. /// Add BandObject.
  339. /// </summary>
  340. private void AddBandObject(PPMLDocument ppml, BandBase band)
  341. {
  342. using (TextObject newObj = new TextObject())
  343. {
  344. newObj.Left = band.AbsLeft;
  345. newObj.Top = band.AbsTop;
  346. newObj.Width = band.Width;
  347. newObj.Height = band.Height;
  348. newObj.Fill = band.Fill;
  349. newObj.Border = band.Border;
  350. AddTextObject(ppml, newObj, true);
  351. }
  352. }
  353. /// <summary>
  354. /// Add Line.
  355. /// </summary>
  356. private void AddLine(PPMLDocument ppml, LineObject line)
  357. {
  358. float AbsLeft = line.AbsLeft + pageLeftMargin;
  359. float AbsTop = line.AbsTop + pageTopMargin;
  360. float Width = line.Width;
  361. float Height = line.Height;
  362. string Fill = Convert.ToString(ExportUtils.GetColorFromFill(line.Fill));
  363. float Border = line.Border.Width;
  364. string LineStyle = line.Style;
  365. string BorderBrush = ExportUtils.HTMLColorCode(line.Border.Color);
  366. if (line.StartCap.Style == CapStyle.Arrow)
  367. {
  368. float x3, y3, x4, y4;
  369. DrawArrow(line.StartCap, Border, Width + AbsLeft, Height + AbsTop, AbsLeft, AbsTop, out x3, out y3, out x4, out y4);
  370. ppml.AddLine(AbsLeft * 0.75f, AbsTop * 0.75f, x3 * 0.75f, y3 * 0.75f, BorderBrush, Border * 0.75f);
  371. ppml.AddLine(AbsLeft * 0.75f, AbsTop * 0.75f, x4 * 0.75f, y4 * 0.75f, BorderBrush, Border * 0.75f);
  372. }
  373. if (line.EndCap.Style == CapStyle.Arrow)
  374. {
  375. float x3, y3, x4, y4;
  376. DrawArrow(line.EndCap, Border, AbsLeft, AbsTop, Width + AbsLeft, Height + AbsTop, out x3, out y3, out x4, out y4);
  377. ppml.AddLine((Width + AbsLeft) * 0.75f, (AbsTop + Height) * 0.75f, x3 * 0.75f, y3 * 0.75f, BorderBrush, Border * 0.75f);
  378. ppml.AddLine((Width + AbsLeft) * 0.75f, (AbsTop + Height) * 0.75f, x4 * 0.75f, y4 * 0.75f, BorderBrush, Border * 0.75f);
  379. }
  380. ppml.AddLine(AbsLeft * 0.75f, AbsTop * 0.75f, (AbsLeft + Width) * 0.75f, (AbsTop + Height) * 0.75f, BorderBrush, Border * 0.75f);
  381. }
  382. private void DrawArrow(CapSettings Arrow, float lineWidth, float x1, float y1, float x2, float y2, out float x3, out float y3, out float x4, out float y4)
  383. {
  384. float k1, a, b, c, d;
  385. float xp, yp;
  386. float wd = Arrow.Width * lineWidth;
  387. float ld = Arrow.Height * lineWidth;
  388. if (Math.Abs(x2 - x1) > 0)
  389. {
  390. k1 = (y2 - y1) / (x2 - x1);
  391. a = (float)(Math.Pow(k1, 2) + 1);
  392. b = 2 * (k1 * ((x2 * y1 - x1 * y2) / (x2 - x1) - y2) - x2);
  393. c = (float)(Math.Pow(x2, 2) + Math.Pow(y2, 2) - Math.Pow(ld, 2) +
  394. Math.Pow((x2 * y1 - x1 * y2) / (x2 - x1), 2) -
  395. 2 * y2 * (x2 * y1 - x1 * y2) / (x2 - x1));
  396. d = (float)(Math.Pow(b, 2) - 4 * a * c);
  397. xp = (float)((-b + Math.Sqrt(d)) / (2 * a));
  398. if ((xp > x1) && (xp > x2) || (xp < x1) && (xp < x2))
  399. xp = (float)((-b - Math.Sqrt(d)) / (2 * a));
  400. yp = xp * k1 + (x2 * y1 - x1 * y2) / (x2 - x1);
  401. if (y2 != y1)
  402. {
  403. x3 = (float)(xp + wd * Math.Sin(Math.Atan(k1)));
  404. y3 = (float)(yp - wd * Math.Cos(Math.Atan(k1)));
  405. x4 = (float)(xp - wd * Math.Sin(Math.Atan(k1)));
  406. y4 = (float)(yp + wd * Math.Cos(Math.Atan(k1)));
  407. }
  408. else
  409. {
  410. x3 = xp; y3 = yp - wd;
  411. x4 = xp; y4 = yp + wd;
  412. }
  413. }
  414. else
  415. {
  416. xp = x2; yp = y2 - ld;
  417. if ((yp > y1) && (yp > y2) || (yp < y1) && (yp < y2))
  418. yp = y2 + ld;
  419. x3 = xp - wd; y3 = yp;
  420. x4 = xp + wd; y4 = yp;
  421. }
  422. }
  423. /// <summary>
  424. /// Add Shape.
  425. /// </summary>
  426. private void AddShape(PPMLDocument ppml, ShapeObject shape)
  427. {
  428. float AbsLeft = shape.AbsLeft + pageLeftMargin;
  429. float AbsTop = shape.AbsTop + pageTopMargin;
  430. float Width = shape.Width;
  431. float Height = shape.Height;
  432. float BorderWidth = shape.Border.Width;
  433. string BorderLines = Convert.ToString(shape.Border.Lines);
  434. string BorderBrush;
  435. string Background;
  436. NormalizeColor(shape, out BorderBrush, out Background);
  437. if (shape.Shape == ShapeKind.Rectangle)
  438. ppml.AddRectangle(AbsLeft * 0.75f, AbsTop * 0.75f, Width * 0.75f, Height * 0.75f, BorderBrush, BorderWidth * 0.75f, Background, false);
  439. if (shape.Shape == ShapeKind.RoundRectangle)
  440. ppml.AddRectangle(AbsLeft * 0.75f, AbsTop * 0.75f, Width * 0.75f, Height * 0.75f, BorderBrush, BorderWidth * 0.75f, Background, true);
  441. if (shape.Shape == ShapeKind.Ellipse)
  442. ppml.AddEllipse(AbsLeft * 0.75f, AbsTop * 0.75f, Width * 0.75f, Height * 0.75f, BorderBrush, BorderWidth * 0.75f, Background);
  443. if (shape.Shape == ShapeKind.Triangle)
  444. ppml.AddTriangle(AbsLeft * 0.75f, AbsTop * 0.75f, Width * 0.75f, Height * 0.75f, BorderBrush, BorderWidth * 0.75f, Background);
  445. if (shape.Shape == ShapeKind.Diamond)
  446. ppml.AddDiamond(AbsLeft * 0.75f, AbsTop * 0.75f, Width * 0.75f, Height * 0.75f, BorderBrush, BorderWidth * 0.75f, Background);
  447. }
  448. private Color GetBlendColor(Color c, float Blend)
  449. {
  450. return Color.FromArgb(255, (int)Math.Round(c.R + (255 - c.R) * Blend),
  451. (int)Math.Round(c.G + (255 - c.G) * Blend),
  452. (int)Math.Round(c.B + (255 - c.B) * Blend));
  453. }
  454. private void NormalizeBorderBrushColor(TextObject obj, out string BorderBrush)
  455. {
  456. obj.FillColor = ExportUtils.GetColorFromFill(obj.Fill);
  457. BorderBrush = ExportUtils.HTMLColorCode(obj.Border.Color);
  458. }
  459. private void NormalizeBackgroundColor(TextObject obj, out string Background)
  460. {
  461. obj.FillColor = ExportUtils.GetColorFromFill(obj.Fill);
  462. if (obj.FillColor.Name == "Transparent")
  463. Background = "none";
  464. else Background = ExportUtils.HTMLColorCode(obj.FillColor);
  465. }
  466. private void NormalizeForegroundColor(TextObject obj, out string Foreground)
  467. {
  468. obj.FillColor = ExportUtils.GetColorFromFill(obj.Fill);
  469. Foreground = ExportUtils.HTMLColorCode(obj.TextColor);
  470. }
  471. private void NormalizeColor(ShapeObject obj, out string BorderBrush, out string Background)
  472. {
  473. obj.FillColor = ExportUtils.GetColorFromFill(obj.Fill);
  474. BorderBrush = ExportUtils.HTMLColorCode(obj.Border.Color);
  475. if (obj.FillColor.Name == "Transparent")
  476. {
  477. Background = "none";
  478. }
  479. else Background = ExportUtils.HTMLColorCode(obj.FillColor);
  480. }
  481. #endregion
  482. #region Protected Methods
  483. /// <inheritdoc/>
  484. protected override void Start()
  485. {
  486. base.Start();
  487. //init
  488. GeneratedStreams = new List<Stream>();
  489. hashtable = new Hashtable();
  490. img_nbr = 0;
  491. images = new Dictionary<string, MemoryStream>();
  492. currentPage = 0;
  493. if (FileName != "" && FileName != null)
  494. {
  495. path = Path.GetDirectoryName(FileName);
  496. fileNameWOext = Path.GetFileNameWithoutExtension(FileName);
  497. extension = Path.GetExtension(FileName);
  498. }
  499. else
  500. fileNameWOext = "xamlreport";
  501. GeneratedFiles.Clear();
  502. GeneratedStreams.Clear();
  503. hashtable.Clear();
  504. using (ReportPage page = GetPage(0))
  505. {
  506. ppml = new PPMLDocument(fileNameWOext, ExportUtils.GetPageWidth(page) * Units.Millimeters * 0.75f, ExportUtils.GetPageHeight(page) * Units.Millimeters * 0.75f);
  507. ppml.TextInCurves = textInCurves;
  508. }
  509. }
  510. /// <summary>
  511. /// Begin exporting of page
  512. /// </summary>
  513. /// <param name="page"></param>
  514. protected override void ExportPageBegin(ReportPage page)
  515. {
  516. base.ExportPageBegin(page);
  517. pageLeftMargin = page.LeftMargin * Units.Millimeters;
  518. pageTopMargin = page.TopMargin * Units.Millimeters;
  519. ppml.AddPage();
  520. // bitmap watermark on bottom
  521. if (page.Watermark.Enabled && !page.Watermark.ShowImageOnTop)
  522. AddBitmapWatermark(page);
  523. // text watermark on bottom
  524. if (page.Watermark.Enabled && !page.Watermark.ShowTextOnTop)
  525. AddTextWatermark(page);
  526. // page borders
  527. if (page.Border.Lines != BorderLines.None)
  528. {
  529. using (TextObject pageBorder = new TextObject())
  530. {
  531. pageBorder.Border = page.Border;
  532. pageBorder.Left = 0;
  533. pageBorder.Top = 0;
  534. pageBorder.Width = (ExportUtils.GetPageWidth(page) - page.LeftMargin - page.RightMargin);
  535. pageBorder.Height = (ExportUtils.GetPageHeight(page) - page.TopMargin - page.BottomMargin);
  536. AddTextObject(ppml, pageBorder, true);
  537. }
  538. }
  539. if (path != null && path != "")
  540. pageFileName = Path.Combine(path, fileNameWOext + currentPage.ToString() + extension);
  541. else
  542. pageFileName = null;
  543. }
  544. /// <summary>
  545. /// Export of Band
  546. /// </summary>
  547. /// <param name="band"></param>
  548. protected override void ExportBand(BandBase band)
  549. {
  550. base.ExportBand(band);
  551. ExportObj(band);
  552. foreach (Base c in band.ForEachAllConvectedObjects(this))
  553. {
  554. ExportObj(c);
  555. }
  556. }
  557. /// <summary>
  558. /// End exporting
  559. /// </summary>
  560. /// <param name="page"></param>
  561. protected override void ExportPageEnd(ReportPage page)
  562. {
  563. base.ExportPageEnd(page);
  564. // bitmap watermark on top
  565. if (page.Watermark.Enabled && page.Watermark.ShowImageOnTop)
  566. AddBitmapWatermark(page);
  567. // text watermark on top
  568. if (page.Watermark.Enabled && page.Watermark.ShowTextOnTop)
  569. AddTextWatermark(page);
  570. ppml.Finish();
  571. // increment page number
  572. //currentPage++;
  573. }
  574. /// <inheritdoc/>
  575. protected override void Finish()
  576. {
  577. if (HasMultipleFiles)
  578. {
  579. if (Directory.Exists(path) && !string.IsNullOrEmpty(FileName))
  580. {
  581. // desktop mode
  582. if (currentPage == 0)
  583. {
  584. // save first page in parent Stream
  585. ppml.Save(Stream);
  586. Stream.Position = 0;
  587. GeneratedStreams.Add(Stream);
  588. GeneratedFiles.Add(FileName);
  589. }
  590. else
  591. {
  592. // save all page after first in files
  593. ppml.Save(pageFileName);
  594. GeneratedFiles.Add(pageFileName);
  595. }
  596. }
  597. else if (string.IsNullOrEmpty(path))
  598. {
  599. if (currentPage == 0)
  600. {
  601. // save first page in parent Stream
  602. ppml.Save(Stream);
  603. Stream.Position = 0;
  604. GeneratedStreams.Add(Stream);
  605. GeneratedFiles.Add(FileName);
  606. }
  607. else
  608. {
  609. // server mode, save in internal stream collection
  610. MemoryStream pageStream = new MemoryStream();
  611. ppml.Save(pageStream);
  612. pageStream.Position = 0;
  613. GeneratedStreams.Add(pageStream);
  614. GeneratedFiles.Add(pageFileName);
  615. }
  616. }
  617. }
  618. if (Directory.Exists(path) && !string.IsNullOrEmpty(FileName))
  619. SaveImgsToFiles();
  620. }
  621. /// <inheritdoc/>
  622. protected override string GetFileFilter()
  623. {
  624. return new MyRes("FileFilters").Get("PPMLFile");
  625. }
  626. #endregion
  627. /// <inheritdoc/>
  628. public override void Serialize(FRWriter writer)
  629. {
  630. base.Serialize(writer);
  631. writer.WriteBool("TextInCurves", TextInCurves);
  632. writer.WriteInt("Jpeg quality", Quality);
  633. }
  634. /// <summary>
  635. /// Initializes a new instance of the <see cref="PPMLExport"/> class.
  636. /// </summary>
  637. public PPMLExport()
  638. {
  639. HasMultipleFiles = true;
  640. pictures = true;
  641. quality = 90;
  642. format = System.Drawing.Imaging.ImageFormat.Jpeg;
  643. }
  644. }
  645. }