XAMLDocument.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. namespace FastReport.Export.XAML
  5. {
  6. /// <summary>
  7. /// Contains Dashes enum
  8. /// </summary>
  9. internal enum Dashes
  10. {
  11. /// <summary>
  12. /// Specifies the Dash.
  13. /// </summary>
  14. Dash,
  15. /// <summary>
  16. /// Specifies the Dot.
  17. /// </summary>
  18. Dot,
  19. /// <summary>
  20. /// Specifies the DashDot.
  21. /// </summary>
  22. DashDot,
  23. /// <summary>
  24. /// Specifies the DashDotDot.
  25. /// </summary>
  26. DashDotDot,
  27. /// <summary>
  28. /// Specifies the Double line.
  29. /// </summary>
  30. Double
  31. }
  32. /// <summary>
  33. /// XAML generator
  34. /// </summary>
  35. internal class XAMLDocument
  36. {
  37. #region Private fields
  38. private XmlAttribute nsAttr;
  39. private XmlElement root;
  40. private XmlElement grid;
  41. private XmlElement resources;
  42. private XmlElement canvas;
  43. private XmlElement stackPanel;
  44. private XmlElement scrollViewer;
  45. private XmlDocument doc = new XmlDocument();
  46. private float width;
  47. private float height;
  48. private int pages_count;
  49. private string fill;
  50. private bool textObjectStyle;
  51. private bool textObjectAngleStyle;
  52. private bool lineStyle;
  53. private bool rectangleStyle;
  54. private bool ellipseStyle;
  55. private bool polygonStyle;
  56. private bool isScrolled = false; //{ get; set; }
  57. #endregion
  58. #region Private Methods
  59. private void AppndAttr(XmlElement element, string attrName, string attrVal)
  60. {
  61. nsAttr = doc.CreateAttribute(attrName);
  62. nsAttr.Value = attrVal;
  63. element.Attributes.Append(nsAttr);
  64. }
  65. private void AppndAttr(XmlElement element, string prefix, string localName, string namespaceURI, string attrVal)
  66. {
  67. nsAttr = doc.CreateAttribute(prefix, localName, namespaceURI);
  68. nsAttr.Value = attrVal;
  69. element.Attributes.Append(nsAttr);
  70. }
  71. private void AddMarginAttrs(float marginLeft, float marginTop, XmlElement element)
  72. {
  73. if (marginLeft != 0 || marginTop != 0)
  74. AppndAttr(element, "Margin", ExportUtils.FloatToString(marginLeft) + "," + ExportUtils.FloatToString(marginTop));
  75. }
  76. private void AddSizeAttrs(float width, float height, XmlElement element)
  77. {
  78. if (width != 0)
  79. AppndAttr(element, "Width", ExportUtils.FloatToString(width));
  80. if (height != 0)
  81. AppndAttr(element, "Height", ExportUtils.FloatToString(height));
  82. }
  83. private void AddStrokeAttrs(string stroke, float strokeThickness, XmlElement element)
  84. {
  85. if (stroke != null && stroke != "Black" && stroke != "#ff000000")
  86. AppndAttr(element, "Stroke", stroke);
  87. if (strokeThickness != 0 && strokeThickness != 1)
  88. AppndAttr(element, "StrokeThickness", ExportUtils.FloatToString(strokeThickness));
  89. }
  90. private void AddXY12Attrs(float x1, float y1, float x2, float y2, XmlElement element)
  91. {
  92. if (x1 != 0)
  93. AppndAttr(element, "X1", ExportUtils.FloatToString(x1));
  94. if (y1 != 0)
  95. AppndAttr(element, "Y1", ExportUtils.FloatToString(y1));
  96. if (x2 != 0)
  97. AppndAttr(element, "X2", ExportUtils.FloatToString(x2));
  98. if (y2 != 0)
  99. AppndAttr(element, "Y2", ExportUtils.FloatToString(y2));
  100. }
  101. /// <summary>
  102. /// Create Window.
  103. /// </summary>
  104. private void CreateWindow(string name)
  105. {
  106. root = doc.CreateElement("Page");
  107. AppndAttr(root, "xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
  108. AppndAttr(root, "xmlns", "x", "http://www.w3.org/2000/xmlns/", "http://schemas.microsoft.com/winfx/2006/xaml");
  109. AppndAttr(root, "x", "Name", "http://schemas.microsoft.com/winfx/2006/xaml", "Window");
  110. AppndAttr(root, "Title", "MainWindow");
  111. AppndAttr(root, "Background", fill);
  112. if (!isScrolled)
  113. {
  114. AppndAttr(root, "Width", ExportUtils.FloatToString(width));
  115. AppndAttr(root, "Height", ExportUtils.FloatToString(height * pages_count));
  116. }
  117. doc.AppendChild(root);
  118. }
  119. ///<summary>
  120. ///Create Grid.
  121. /// </summary>
  122. private void AddGrid()
  123. {
  124. grid = doc.CreateElement("Grid");
  125. AppndAttr(grid, "x", "Name", "http://schemas.microsoft.com/winfx/2006/xaml", "LayoutRoot");
  126. root.AppendChild(grid);
  127. }
  128. ///<summary>
  129. ///Create Canvas.
  130. /// </summary>
  131. private void AddCanvas(int PageNum)
  132. {
  133. canvas = doc.CreateElement("Canvas");
  134. if (PageNum != 0)
  135. AppndAttr(canvas, "Margin", "0, " + ExportUtils.FloatToString(height) + ", 0, 0");
  136. stackPanel.AppendChild(canvas);
  137. }
  138. /// <summary>
  139. /// Create StackPanel
  140. /// </summary>
  141. private void AddStackPanel(XmlElement _root)
  142. {
  143. stackPanel = doc.CreateElement("StackPanel");
  144. if (isScrolled)
  145. {
  146. AppndAttr(stackPanel, "Width", ExportUtils.FloatToString(width));
  147. AppndAttr(stackPanel, "Height", ExportUtils.FloatToString(height * pages_count));
  148. }
  149. _root.AppendChild(stackPanel);
  150. }
  151. /// <summary>
  152. /// Create StackPanel
  153. /// </summary>
  154. private void AddScrollViewer()
  155. {
  156. scrollViewer = doc.CreateElement("ScrollViewer");
  157. AppndAttr(scrollViewer, "HorizontalScrollBarVisibility", "auto");
  158. AppndAttr(scrollViewer, "VerticalScrollBarVisibility", "auto");
  159. root.AppendChild(scrollViewer);
  160. }
  161. #region add resources
  162. ///<summary>
  163. ///Create Resources tag
  164. ///</summary>
  165. private void AddResources()
  166. {
  167. resources = doc.CreateElement("Page.Resources");
  168. root.AppendChild(resources);
  169. }
  170. ///<summary>
  171. ///Add resource for TextObject
  172. ///</summary>
  173. private void AddResourceTextObject()
  174. {
  175. XmlElement Style = doc.CreateElement("Style");
  176. AppndAttr(Style, "TargetType", "TextBox");
  177. AppndAttr(Style, "x", "Key", "http://schemas.microsoft.com/winfx/2006/xaml", "rTxt");
  178. resources.AppendChild(Style);
  179. XmlElement Setter = doc.CreateElement("Setter");
  180. AppndAttr(Setter, "Property", "Background");
  181. AppndAttr(Setter, "Value", "{x:Null}");
  182. Style.AppendChild(Setter);
  183. Setter = doc.CreateElement("Setter");
  184. AppndAttr(Setter, "Property", "BorderBrush");
  185. AppndAttr(Setter, "Value", "Black");
  186. Style.AppendChild(Setter);
  187. Setter = doc.CreateElement("Setter");
  188. AppndAttr(Setter, "Property", "BorderThickness");
  189. AppndAttr(Setter, "Value", "0");
  190. Style.AppendChild(Setter);
  191. Setter = doc.CreateElement("Setter");
  192. AppndAttr(Setter, "Property", "FontSize");
  193. AppndAttr(Setter, "Value", "10pt");
  194. Style.AppendChild(Setter);
  195. Setter = doc.CreateElement("Setter");
  196. AppndAttr(Setter, "Property", "FontFamily");
  197. AppndAttr(Setter, "Value", "Arial");
  198. Style.AppendChild(Setter);
  199. Setter = doc.CreateElement("Setter");
  200. AppndAttr(Setter, "Property", "Padding");
  201. AppndAttr(Setter, "Value", "2,0,2,0");
  202. Style.AppendChild(Setter);
  203. Setter = doc.CreateElement("Setter");
  204. AppndAttr(Setter, "Property", "TextWrapping");
  205. AppndAttr(Setter, "Value", "Wrap");
  206. Style.AppendChild(Setter);
  207. Setter = doc.CreateElement("Setter");
  208. AppndAttr(Setter, "Property", "IsHitTestVisible");
  209. AppndAttr(Setter, "Value", "false");
  210. Style.AppendChild(Setter);
  211. }
  212. ///<summary>
  213. ///Add resource for TextObject with angle
  214. ///</summary>
  215. private void AddResourceTextObjectAngle()
  216. {
  217. XmlElement Style = doc.CreateElement("Style");
  218. AppndAttr(Style, "TargetType", "UserControl");
  219. AppndAttr(Style, "x", "Key", "http://schemas.microsoft.com/winfx/2006/xaml", "rTxtE");
  220. resources.AppendChild(Style);
  221. XmlElement Setter = doc.CreateElement("Setter");
  222. AppndAttr(Setter, "Property", "Background");
  223. AppndAttr(Setter, "Value", "{x:Null}");
  224. Style.AppendChild(Setter);
  225. Setter = doc.CreateElement("Setter");
  226. AppndAttr(Setter, "Property", "BorderBrush");
  227. AppndAttr(Setter, "Value", "Black");
  228. Style.AppendChild(Setter);
  229. Setter = doc.CreateElement("Setter");
  230. AppndAttr(Setter, "Property", "BorderThickness");
  231. AppndAttr(Setter, "Value", "0");
  232. Style.AppendChild(Setter);
  233. Setter = doc.CreateElement("Setter");
  234. AppndAttr(Setter, "Property", "FontSize");
  235. AppndAttr(Setter, "Value", "10pt");
  236. Style.AppendChild(Setter);
  237. Setter = doc.CreateElement("Setter");
  238. AppndAttr(Setter, "Property", "FontFamily");
  239. AppndAttr(Setter, "Value", "Arial");
  240. Style.AppendChild(Setter);
  241. Setter = doc.CreateElement("Setter");
  242. AppndAttr(Setter, "Property", "Padding");
  243. AppndAttr(Setter, "Value", "2,0,2,0");
  244. Style.AppendChild(Setter);
  245. Setter = doc.CreateElement("Setter");
  246. AppndAttr(Setter, "Property", "IsHitTestVisible");
  247. AppndAttr(Setter, "Value", "false");
  248. Style.AppendChild(Setter);
  249. }
  250. ///<summary>
  251. ///Add resource for Line
  252. ///</summary>
  253. private void AddResourceLine()
  254. {
  255. XmlElement Style = doc.CreateElement("Style");
  256. AppndAttr(Style, "TargetType", "Line");
  257. AppndAttr(Style, "x", "Key", "http://schemas.microsoft.com/winfx/2006/xaml", "rLn");
  258. resources.AppendChild(Style);
  259. XmlElement Setter = doc.CreateElement("Setter");
  260. AppndAttr(Setter, "Property", "Stroke");
  261. AppndAttr(Setter, "Value", "Black");
  262. Style.AppendChild(Setter);
  263. }
  264. ///<summary>
  265. ///Add resource for Rectangle
  266. ///</summary>
  267. private void AddResourceRectangle()
  268. {
  269. XmlElement Style = doc.CreateElement("Style");
  270. AppndAttr(Style, "TargetType", "Rectangle");
  271. AppndAttr(Style, "x", "Key", "http://schemas.microsoft.com/winfx/2006/xaml", "rRc");
  272. resources.AppendChild(Style);
  273. XmlElement Setter = doc.CreateElement("Setter");
  274. AppndAttr(Setter, "Property", "Stroke");
  275. AppndAttr(Setter, "Value", "Black");
  276. Style.AppendChild(Setter);
  277. }
  278. ///<summary>
  279. ///Add resource for Ellipse
  280. ///</summary>
  281. private void AddResourceEllipse()
  282. {
  283. XmlElement Style = doc.CreateElement("Style");
  284. AppndAttr(Style, "TargetType", "Ellipse");
  285. AppndAttr(Style, "x", "Key", "http://schemas.microsoft.com/winfx/2006/xaml", "rEl");
  286. resources.AppendChild(Style);
  287. XmlElement Setter = doc.CreateElement("Setter");
  288. AppndAttr(Setter, "Property", "Stroke");
  289. AppndAttr(Setter, "Value", "Black");
  290. Style.AppendChild(Setter);
  291. }
  292. ///<summary>
  293. ///Add resource for Polygon
  294. ///</summary>
  295. private void AddResourcePolygon()
  296. {
  297. XmlElement Style = doc.CreateElement("Style");
  298. AppndAttr(Style, "TargetType", "Polygon");
  299. AppndAttr(Style, "x", "Key", "http://schemas.microsoft.com/winfx/2006/xaml", "rPl");
  300. resources.AppendChild(Style);
  301. XmlElement Setter = doc.CreateElement("Setter");
  302. AppndAttr(Setter, "Property", "Stroke");
  303. AppndAttr(Setter, "Value", "Black");
  304. Style.AppendChild(Setter);
  305. }
  306. #endregion
  307. private XmlElement CrtTOFrame(float MarginLeft, float MarginTop, float Width, float Height,
  308. string HorizontalAlignment, string VerticalAlignment, string BorderBrush, float BorderThickness,
  309. float LeftLine, float TopLine, float RightLine, float BottomLine, string LeftLineDashStile,
  310. string TopLineDashStile, string RightLineDashStile, string BottomLineDashStile, string colorLeftLine,
  311. string colorTopLine, string colorRightLine, string colorBottomLine, bool Shadow, string ShadowColor,
  312. float ShadowWidth, string Background, string BorderLines
  313. , float PaddingLeft, float PaddingTop,
  314. float PaddingRight, float PaddingBottom, bool Glass, string colorTop, string elementName)
  315. {
  316. XmlElement element = doc.CreateElement(elementName);
  317. MarginLeft = MarginLeft + BorderThickness;
  318. MarginTop = MarginTop + BorderThickness;
  319. if (MarginLeft != 0 || MarginTop != 0)
  320. AppndAttr(element, "Margin", ExportUtils.FloatToString(MarginLeft) + "," +
  321. ExportUtils.FloatToString(MarginTop));
  322. Width = Width + BorderThickness;
  323. Height = Height + BorderThickness;
  324. if (Width != 0)
  325. AppndAttr(element, "Width", ExportUtils.FloatToString(Width));
  326. if (Height != 0)
  327. AppndAttr(element, "Height", ExportUtils.FloatToString(Height));
  328. if (HorizontalAlignment != "Left" && HorizontalAlignment != null && HorizontalAlignment != "" && HorizontalAlignment != "Justify")
  329. AppndAttr(element, "HorizontalAlignment", HorizontalAlignment);
  330. if (VerticalAlignment != "Top" && VerticalAlignment != null && VerticalAlignment != "")
  331. AppndAttr(element, "VerticalAlignment", VerticalAlignment);
  332. // BorderThickness
  333. string[] masLines = BorderLines.Split(',', ' ');//names lines borders
  334. string AllBorderThickness = null;//set lines
  335. bool All = false;
  336. bool None = false;
  337. bool Left = false;
  338. bool Right = false;
  339. bool Top = false;
  340. bool Bottom = false;
  341. for (int i = 0; i < masLines.Length; i++)
  342. {
  343. if (masLines[i] == "All")
  344. All = true;
  345. if (masLines[i] == "None")
  346. None = true;
  347. if (masLines[i] == "Left")
  348. Left = true;
  349. if (masLines[i] == "Right")
  350. Right = true;
  351. if (masLines[i] == "Top")
  352. Top = true;
  353. if (masLines[i] == "Bottom")
  354. Bottom = true;
  355. }
  356. if (!None)
  357. {
  358. if (Left || All)
  359. {
  360. if (LeftLineDashStile != "Solid" && LeftLineDashStile != "Double")
  361. LeftLine = 0;
  362. else
  363. if (LeftLine == 0) LeftLine = 1;
  364. AllBorderThickness = ExportUtils.FloatToString(LeftLine);
  365. }
  366. else if (!Left)
  367. {
  368. LeftLine = 0;
  369. AllBorderThickness = "0";
  370. }
  371. if (Top || All)
  372. {
  373. if (TopLineDashStile != "Solid" && TopLineDashStile != "Double")
  374. TopLine = 0;
  375. else
  376. if (TopLine == 0) TopLine = 1;
  377. AllBorderThickness = AllBorderThickness + "," + ExportUtils.FloatToString(TopLine);
  378. }
  379. else if (!Top)
  380. {
  381. TopLine = 0;
  382. AllBorderThickness = AllBorderThickness + "," + "0";
  383. }
  384. if (Right || All)
  385. {
  386. if (RightLineDashStile != "Solid" && RightLineDashStile != "Double")
  387. RightLine = 0;
  388. else
  389. if (RightLine == 0) RightLine = 1;
  390. AllBorderThickness = AllBorderThickness + "," + ExportUtils.FloatToString(RightLine);
  391. }
  392. else if (!Right)
  393. {
  394. RightLine = 0;
  395. AllBorderThickness = AllBorderThickness + "," + "0";
  396. }
  397. if (Bottom || All)
  398. {
  399. if (BottomLineDashStile != "Solid" && BottomLineDashStile != "Double")
  400. BottomLine = 0;
  401. else
  402. if (BottomLine == 0) BottomLine = 1;
  403. AllBorderThickness = AllBorderThickness + "," + ExportUtils.FloatToString(BottomLine);
  404. }
  405. else if (!Bottom)
  406. {
  407. BottomLine = 0;
  408. AllBorderThickness = AllBorderThickness + "," + "0";
  409. }
  410. }
  411. if (LeftLine == TopLine && TopLine == RightLine && RightLine == BottomLine && BottomLine == LeftLine)
  412. AllBorderThickness = ExportUtils.FloatToString(BottomLine);
  413. if (!None && (Left || Top || Right || Bottom || All) && !(LeftLine == 0 && TopLine == 0 && RightLine == 0 && BottomLine == 0))
  414. AppndAttr(element, "BorderThickness", AllBorderThickness);
  415. //Dash---
  416. if (Left || All)
  417. {
  418. if (LeftLineDashStile == "Dash")
  419. AddLine(MarginLeft, MarginTop, MarginLeft, MarginTop + Height, colorLeftLine, BorderThickness, Dashes.Dash);
  420. if (LeftLineDashStile == "Dot")
  421. AddLine(MarginLeft, MarginTop, MarginLeft, MarginTop + Height, colorLeftLine, BorderThickness, Dashes.Dot);
  422. if (LeftLineDashStile == "DashDot")
  423. AddLine(MarginLeft, MarginTop, MarginLeft, MarginTop + Height, colorLeftLine, BorderThickness, Dashes.DashDot);
  424. if (LeftLineDashStile == "DashDotDot")
  425. AddLine(MarginLeft, MarginTop, MarginLeft, MarginTop + Height, colorLeftLine, BorderThickness, Dashes.DashDotDot);
  426. if (LeftLineDashStile == "Double")
  427. AddLine(MarginLeft + 4, MarginTop + 4, MarginLeft + 4, MarginTop + Height + 4, colorLeftLine, BorderThickness);
  428. }
  429. if (Top || All)
  430. {
  431. if (TopLineDashStile == "Dash")
  432. AddLine(MarginLeft, MarginTop, MarginLeft + Width, MarginTop, colorTopLine, BorderThickness, Dashes.Dash);
  433. if (TopLineDashStile == "Dot")
  434. AddLine(MarginLeft, MarginTop, MarginLeft + Width, MarginTop, colorTopLine, BorderThickness, Dashes.Dot);
  435. if (TopLineDashStile == "DashDot")
  436. AddLine(MarginLeft, MarginTop, MarginLeft + Width, MarginTop, colorTopLine, BorderThickness, Dashes.DashDot);
  437. if (TopLineDashStile == "DashDotDot")
  438. AddLine(MarginLeft, MarginTop, MarginLeft + Width, MarginTop, colorTopLine, BorderThickness, Dashes.DashDotDot);
  439. if (TopLineDashStile == "Double")
  440. AddLine(MarginLeft, MarginTop, MarginLeft + Width + 4, MarginTop + 4, colorTopLine, BorderThickness);
  441. }
  442. if (Right || All)
  443. {
  444. if (RightLineDashStile == "Dash")
  445. AddLine(MarginLeft + Width, MarginTop, MarginLeft + Width, MarginTop + Height, colorRightLine, BorderThickness, Dashes.Dash);
  446. if (RightLineDashStile == "Dot")
  447. AddLine(MarginLeft + Width, MarginTop, MarginLeft + Width, MarginTop + Height, colorRightLine, BorderThickness, Dashes.Dot);
  448. if (RightLineDashStile == "DashDot")
  449. AddLine(MarginLeft + Width, MarginTop, MarginLeft + Width, MarginTop + Height, colorRightLine, BorderThickness, Dashes.DashDot);
  450. if (RightLineDashStile == "DashDotDot")
  451. AddLine(MarginLeft + Width, MarginTop, MarginLeft + Width, MarginTop + Height, colorRightLine, BorderThickness, Dashes.DashDotDot);
  452. if (RightLineDashStile == "Double")
  453. AddLine(MarginLeft + Width, MarginTop, MarginLeft + Width, MarginTop + Height, colorRightLine, BorderThickness);
  454. }
  455. if (Bottom || All)
  456. {
  457. if (BottomLineDashStile == "Dash")
  458. AddLine(MarginLeft, MarginTop + Height, MarginLeft + Width, MarginTop + Height, colorBottomLine, BorderThickness, Dashes.Dash);
  459. if (BottomLineDashStile == "Dot")
  460. AddLine(MarginLeft, MarginTop + Height, MarginLeft + Width, MarginTop + Height, colorBottomLine, BorderThickness, Dashes.Dot);
  461. if (BottomLineDashStile == "DashDot")
  462. AddLine(MarginLeft, MarginTop + Height, MarginLeft + Width, MarginTop + Height, colorBottomLine, BorderThickness, Dashes.DashDot);
  463. if (BottomLineDashStile == "DashDotDot")
  464. AddLine(MarginLeft, MarginTop + Height, MarginLeft + Width, MarginTop + Height, colorBottomLine, BorderThickness, Dashes.DashDotDot);
  465. if (BottomLineDashStile == "Double")
  466. AddLine(MarginLeft, MarginTop + Height, MarginLeft + Width, MarginTop + Height, colorBottomLine, BorderThickness);
  467. }
  468. if (Shadow)
  469. {
  470. AddLine(MarginLeft + ShadowWidth, MarginTop + Height + ShadowWidth / 2, MarginLeft + Width + ShadowWidth, MarginTop + Height + ShadowWidth / 2, ShadowColor, ShadowWidth);
  471. AddLine(MarginLeft + Width + ShadowWidth / 2, MarginTop + ShadowWidth, MarginLeft + Width + ShadowWidth / 2, MarginTop + Height + ShadowWidth, ShadowColor, ShadowWidth);
  472. }
  473. if (!None && BorderBrush != "#ff000000" && BorderBrush != null && BorderBrush != "Black")
  474. AppndAttr(element, "BorderBrush", BorderBrush);
  475. //Background
  476. if (Glass)
  477. {
  478. XmlElement elementBackground = doc.CreateElement(elementName + ".Background");
  479. element.AppendChild(elementBackground);
  480. XmlElement LinearGradientBrush = doc.CreateElement("LinearGradientBrush");
  481. AppndAttr(LinearGradientBrush, "StartPoint", "0, 0");
  482. AppndAttr(LinearGradientBrush, "EndPoint", "0,1");
  483. elementBackground.AppendChild(LinearGradientBrush);
  484. XmlElement GradientStop = doc.CreateElement("GradientStop");
  485. AppndAttr(GradientStop, "Color", colorTop);
  486. AppndAttr(GradientStop, "Offset", "0.5");
  487. LinearGradientBrush.AppendChild(GradientStop);
  488. GradientStop = doc.CreateElement("GradientStop");
  489. AppndAttr(GradientStop, "Color", Background);
  490. AppndAttr(GradientStop, "Offset", "0.5");
  491. LinearGradientBrush.AppendChild(GradientStop);
  492. }
  493. else
  494. if (Background != null)
  495. AppndAttr(element, "Background", Background);
  496. //Paddings
  497. if (PaddingLeft != 2 || PaddingTop != 0 || PaddingRight != 2 || PaddingBottom != 0)
  498. {
  499. string Paddings;
  500. if (PaddingLeft == 0 && PaddingTop == 0 && PaddingRight == 0 && PaddingBottom == 0) Paddings = Convert.ToString(0);
  501. else if (PaddingLeft == PaddingTop && PaddingLeft == PaddingRight && PaddingLeft == PaddingBottom && PaddingLeft != 0) Paddings = ExportUtils.FloatToString(PaddingLeft);//Convert.ToString(PaddingLeft);
  502. else
  503. Paddings = ExportUtils.FloatToString(PaddingLeft) + "," + ExportUtils.FloatToString(PaddingTop) + ","
  504. + ExportUtils.FloatToString(PaddingRight) + "," + ExportUtils.FloatToString(PaddingBottom);
  505. AppndAttr(element, "Padding", Paddings);
  506. }
  507. return element;
  508. }
  509. private void AppendTextAttrs(string HorizontalAlignment, string VerticalAlignment, string Text,
  510. float FontSize, string Foreground, string FontFamily, bool Bold, bool Italic, bool Underline,
  511. bool WordWrap, XmlElement element)
  512. {
  513. if (Text != null && Text != "")
  514. AppndAttr(element, "Text", Text);
  515. if (FontSize != 0 && FontSize != 10)
  516. AppndAttr(element, "FontSize", ExportUtils.FloatToString(FontSize) + "pt");
  517. if (Foreground != null && Foreground != "" && Foreground != "#ff000000")
  518. AppndAttr(element, "Foreground", Foreground);
  519. if (HorizontalAlignment != "Left" && HorizontalAlignment != null && HorizontalAlignment != "" && HorizontalAlignment != "Justify")
  520. AppndAttr(element, "HorizontalContentAlignment", HorizontalAlignment);
  521. if (VerticalAlignment != "Top" && VerticalAlignment != null && VerticalAlignment != "")
  522. AppndAttr(element, "VerticalContentAlignment", VerticalAlignment);
  523. if (FontFamily != "Arial" && FontFamily != null && FontFamily != "")
  524. AppndAttr(element, "FontFamily", FontFamily);
  525. if (Bold)
  526. AppndAttr(element, "FontWeight", "Bold");
  527. if (Italic)
  528. AppndAttr(element, "FontStyle", "Italic");
  529. if (Underline)
  530. AppndAttr(element, "TextDecorations", "Underline");
  531. if (!WordWrap)
  532. AppndAttr(element, "TextWrapping", "NoWrap");
  533. }
  534. #endregion
  535. ///<summary>
  536. ///Add TextObject.
  537. /// </summary>
  538. internal void AddTextObject(float MarginLeft, float MarginTop, float Width, float Height,
  539. string HorizontalAlignment, string VerticalAlignment, string BorderBrush, float BorderThickness,
  540. float LeftLine, float TopLine, float RightLine, float BottomLine, string LeftLineDashStile,
  541. string TopLineDashStile, string RightLineDashStile, string BottomLineDashStile, string colorLeftLine,
  542. string colorTopLine, string colorRightLine, string colorBottomLine, bool Shadow, string ShadowColor,
  543. float ShadowWidth, string Background, string BorderLines, string Text, float FontSize, string Foreground,
  544. string FontFamily, bool Bold, bool Italic, bool Underline, float PaddingLeft, float PaddingTop,
  545. float PaddingRight, float PaddingBottom, bool WordWrap, bool Glass, string colorTop)
  546. {
  547. if (textObjectStyle == true)
  548. {
  549. AddResourceTextObject(); textObjectStyle = false;
  550. }
  551. XmlElement TextBox = CrtTOFrame(MarginLeft, MarginTop, Width, Height,
  552. HorizontalAlignment, VerticalAlignment, BorderBrush, BorderThickness,
  553. LeftLine, TopLine, RightLine, BottomLine, LeftLineDashStile,
  554. TopLineDashStile, RightLineDashStile, BottomLineDashStile, colorLeftLine,
  555. colorTopLine, colorRightLine, colorBottomLine, Shadow, ShadowColor,
  556. ShadowWidth, Background, BorderLines
  557. , PaddingLeft, PaddingTop,
  558. PaddingRight, PaddingBottom, Glass, colorTop, "TextBox");
  559. AppendTextAttrs(HorizontalAlignment, VerticalAlignment, Text,
  560. FontSize, Foreground, FontFamily, Bold, Italic, Underline,
  561. WordWrap, TextBox);
  562. AppndAttr(TextBox, "Style", "{StaticResource rTxt}");
  563. canvas.AppendChild(TextBox);
  564. }
  565. ///<summary>
  566. ///Method for add TextObject with angle
  567. /// </summary>
  568. internal void AddTextObject(float MarginLeft, float MarginTop, float Width, float Height,
  569. string HorizontalAlignment, string VerticalAlignment, string BorderBrush, float BorderThickness,
  570. float LeftLine, float TopLine, float RightLine, float BottomLine, string LeftLineDashStile,
  571. string TopLineDashStile, string RightLineDashStile, string BottomLineDashStile, string colorLeftLine,
  572. string colorTopLine, string colorRightLine, string colorBottomLine, bool Shadow, string ShadowColor, float ShadowWidth, string Background, string BorderLines,
  573. string Text, float FontSize, string Foreground, string FontFamily, bool Bold, bool Italic, bool Underline,
  574. float PaddingLeft, float PaddingTop, float PaddingRight, float PaddingBottom, bool WordWrap, bool Glass, string colorTop, float Angle)
  575. {
  576. if (textObjectAngleStyle == true)
  577. {
  578. AddResourceTextObjectAngle(); textObjectAngleStyle = false;
  579. }
  580. XmlElement UserControl = CrtTOFrame(MarginLeft, MarginTop, Width, Height,
  581. HorizontalAlignment, VerticalAlignment, BorderBrush, BorderThickness,
  582. LeftLine, TopLine, RightLine, BottomLine, LeftLineDashStile,
  583. TopLineDashStile, RightLineDashStile, BottomLineDashStile, colorLeftLine,
  584. colorTopLine, colorRightLine, colorBottomLine, Shadow, ShadowColor,
  585. ShadowWidth, Background, BorderLines
  586. , PaddingLeft, PaddingTop,
  587. PaddingRight, PaddingBottom, Glass, colorTop, "UserControl");
  588. if (!string.IsNullOrEmpty(Text))
  589. {
  590. XmlElement EndoText = doc.CreateElement("TextBox");
  591. AppndAttr(EndoText, "Style", "{StaticResource rTxt}");
  592. AppndAttr(EndoText, "Width", ExportUtils.FloatToString(Width - Math.Max(Math.Max(LeftLine, TopLine),
  593. Math.Max(RightLine, BottomLine))));
  594. AppendTextAttrs(HorizontalAlignment, VerticalAlignment, Text,
  595. FontSize, Foreground, FontFamily, Bold, Italic, Underline,
  596. WordWrap, EndoText);
  597. if (!(PaddingLeft == 2 && PaddingTop == 0 && PaddingRight == 2 & PaddingBottom == 0))
  598. {
  599. string Paddings;
  600. if (PaddingLeft == 0 && PaddingTop == 0 && PaddingRight == 0 && PaddingBottom == 0) Paddings = Convert.ToString(0);
  601. else if (PaddingLeft == PaddingTop && PaddingLeft == PaddingRight && PaddingLeft == PaddingBottom && PaddingLeft != 0) Paddings = ExportUtils.FloatToString(PaddingLeft);//Convert.ToString(PaddingLeft);
  602. else
  603. Paddings = ExportUtils.FloatToString(PaddingLeft) + "," + ExportUtils.FloatToString(PaddingTop) + ","
  604. + ExportUtils.FloatToString(PaddingRight) + "," + ExportUtils.FloatToString(PaddingBottom);
  605. AppndAttr(EndoText, "Padding", "Paddings");
  606. }
  607. if (!WordWrap)
  608. AppndAttr(EndoText, "TextWrapping", "NoWrap");
  609. UserControl.AppendChild(EndoText);
  610. XmlElement TextBoxTransform = doc.CreateElement("TextBox.LayoutTransform");
  611. EndoText.AppendChild(TextBoxTransform);
  612. XmlElement TransformGroup = doc.CreateElement("TransformGroup");
  613. TextBoxTransform.AppendChild(TransformGroup);
  614. XmlElement RotateTransform = doc.CreateElement("RotateTransform");
  615. AppndAttr(RotateTransform, "Angle", ExportUtils.FloatToString(Angle));
  616. TransformGroup.AppendChild(RotateTransform);
  617. }
  618. canvas.AppendChild(UserControl);
  619. }
  620. ///<summary>
  621. ///Add line.
  622. /// </summary>
  623. internal void AddLine(float marginLeft, float marginTop, float x2, float y2, string stroke, float strokeThickness)
  624. {
  625. if (lineStyle == true)
  626. {
  627. AddResourceLine(); lineStyle = false;
  628. }
  629. XmlElement Line = doc.CreateElement("Line");
  630. AppndAttr(Line, "Style", "{StaticResource rLn}");
  631. AddSizeAttrs(width, height, Line);
  632. AddXY12Attrs(marginLeft, marginTop, x2, y2, Line);
  633. AddStrokeAttrs(stroke, strokeThickness, Line);
  634. canvas.AppendChild(Line);
  635. }
  636. ///<summary>
  637. ///Add line with dash.
  638. /// </summary>
  639. internal void AddLine(float marginLeft, float marginTop, float x2, float y2, string stroke, float strokeThickness, Dashes dash)
  640. {
  641. AddLine(marginLeft, marginTop, x2, y2, stroke, strokeThickness);
  642. string StrokeDashArray = null;
  643. switch (dash)
  644. {
  645. case Dashes.Dash: StrokeDashArray = "3"; break;
  646. case Dashes.Dot: StrokeDashArray = "1"; break;
  647. case Dashes.DashDot: StrokeDashArray = "1 3 1"; break;
  648. case Dashes.DashDotDot: StrokeDashArray = "1 1 3 1 1"; break;
  649. }
  650. if (StrokeDashArray != null)
  651. AppndAttr((XmlElement)canvas.LastChild, "StrokeDashArray", StrokeDashArray);
  652. }
  653. /// <summary>
  654. /// Add rectangle.
  655. /// </summary>
  656. internal void AddRectangle(float marginLeft, float marginTop, float width, float height,
  657. string stroke, float strokeThickness, string fill, bool rounded)
  658. {
  659. if (rectangleStyle == true)
  660. {
  661. AddResourceRectangle(); rectangleStyle = false;
  662. }
  663. XmlElement Rectangle = doc.CreateElement("Rectangle");
  664. AppndAttr(Rectangle, "Style", "{StaticResource rRc}");
  665. AddMarginAttrs(marginLeft + strokeThickness, marginTop + strokeThickness, Rectangle);
  666. AddSizeAttrs(width, height, Rectangle);
  667. AddStrokeAttrs(stroke, strokeThickness, Rectangle);
  668. if (fill != null)
  669. AppndAttr(Rectangle, "Fill", fill);
  670. if (rounded)
  671. {
  672. AppndAttr(Rectangle, "RadiusX", "10");
  673. AppndAttr(Rectangle, "RadiusY", "10");
  674. }
  675. canvas.AppendChild(Rectangle);
  676. }
  677. /// <summary>
  678. /// Add ellips.
  679. /// </summary>
  680. internal void AddEllipse(float marginLeft, float marginTop, float width, float height,
  681. string stroke, float strokeThickness, string fill)
  682. {
  683. if (ellipseStyle == true)
  684. {
  685. AddResourceEllipse(); ellipseStyle = false;
  686. }
  687. XmlElement Ellipse = doc.CreateElement("Ellipse");
  688. AppndAttr(Ellipse, "Style", "{StaticResource rEl}");
  689. AddMarginAttrs(marginLeft + strokeThickness, marginTop + strokeThickness, Ellipse);
  690. AddSizeAttrs(width, height, Ellipse);
  691. AddStrokeAttrs(stroke, strokeThickness, Ellipse);
  692. if (fill != null)
  693. AppndAttr(Ellipse, "Fill", fill);
  694. canvas.AppendChild(Ellipse);
  695. }
  696. /// <summary>
  697. /// Add triangle.
  698. /// </summary>
  699. internal void AddTriangle(float marginLeft, float marginTop, float width, float height,
  700. string stroke, float strokeThickness, string fill)
  701. {
  702. if (polygonStyle == true)
  703. {
  704. AddResourcePolygon(); polygonStyle = false;
  705. }
  706. XmlElement Polygon = doc.CreateElement("Polygon");
  707. AppndAttr(Polygon, "Style", "{StaticResource rPl}");
  708. AddMarginAttrs(marginLeft, marginTop, Polygon);
  709. AddSizeAttrs(width, height, Polygon);
  710. string Points;
  711. float x1 = width / 2;
  712. float y1 = 0;
  713. float x2 = width;
  714. float y2 = height;
  715. float x3 = 0;
  716. float y3 = height;
  717. Points = ExportUtils.FloatToString(x1) + "," +
  718. ExportUtils.FloatToString(y1) + "," +
  719. ExportUtils.FloatToString(x2) + "," +
  720. ExportUtils.FloatToString(y2) + "," +
  721. ExportUtils.FloatToString(x3) + "," +
  722. ExportUtils.FloatToString(y3);
  723. AppndAttr(Polygon, "Points", Points);
  724. AddStrokeAttrs(stroke, strokeThickness, Polygon);
  725. if (fill != null)
  726. AppndAttr(Polygon, "Fill", fill);
  727. canvas.AppendChild(Polygon);
  728. }
  729. /// <summary>
  730. /// Add Diamond.
  731. /// </summary>
  732. internal void AddDiamond(float marginLeft, float marginTop, float width, float height,
  733. string stroke, float strokeThickness, string fill)
  734. {
  735. if (polygonStyle == true)
  736. {
  737. AddResourcePolygon(); polygonStyle = false;
  738. }
  739. XmlElement Polygon = doc.CreateElement("Polygon");
  740. AppndAttr(Polygon, "Style", "{StaticResource rPl}");
  741. AddMarginAttrs(marginLeft, marginTop, Polygon);
  742. AddSizeAttrs(width, height, Polygon);
  743. string Points;
  744. float x1 = width / 2; float y1 = 0;
  745. float x2 = width; float y2 = height / 2;
  746. float x3 = width / 2; float y3 = height;
  747. float x4 = 0; float y4 = height / 2;
  748. Points = ExportUtils.FloatToString(x1) + "," +
  749. ExportUtils.FloatToString(y1) + "," +
  750. ExportUtils.FloatToString(x2) + "," +
  751. ExportUtils.FloatToString(y2) + "," +
  752. ExportUtils.FloatToString(x3) + "," +
  753. ExportUtils.FloatToString(y3) + "," +
  754. ExportUtils.FloatToString(x4) + "," +
  755. ExportUtils.FloatToString(y4);
  756. AppndAttr(Polygon, "Points", Points);
  757. AddStrokeAttrs(stroke, strokeThickness, Polygon);
  758. if (fill != null)
  759. AppndAttr(Polygon, "Fill", fill);
  760. canvas.AppendChild(Polygon);
  761. }
  762. /// <summary>
  763. /// Add image
  764. /// </summary>
  765. /// <param name="filename"></param>
  766. /// <param name="format"></param>
  767. /// <param name="name"></param>
  768. /// <param name="left"></param>
  769. /// <param name="top"></param>
  770. /// <param name="width"></param>
  771. /// <param name="height"></param>
  772. internal void AddImage(string filename, string format, string name, float left, float top, float width, float height)
  773. {
  774. if (!String.IsNullOrEmpty(filename))
  775. {
  776. XmlElement Image = doc.CreateElement("Image");
  777. AppndAttr(Image, "Source", filename + "." + format);
  778. if (!string.IsNullOrEmpty(name))
  779. AppndAttr(Image, "Name", name);
  780. AddMarginAttrs(left, top, Image);
  781. AddSizeAttrs(width, height, Image);
  782. canvas.AppendChild(Image);
  783. }
  784. }
  785. /// <summary>
  786. /// Add image without name
  787. /// </summary>
  788. /// <param name="filename"></param>
  789. /// <param name="format"></param>
  790. /// <param name="left"></param>
  791. /// <param name="top"></param>
  792. /// <param name="width"></param>
  793. /// <param name="height"></param>
  794. internal void AddImage(string filename, string format, float left, float top, float width, float height)
  795. {
  796. AddImage(filename, format, null, left, top, width, height);
  797. }
  798. /// <summary>
  799. /// Add page to StackPanel
  800. /// </summary>
  801. /// <param name="PageNum"></param>
  802. internal void AddPage(int PageNum)
  803. {
  804. AddCanvas(PageNum);
  805. }
  806. /// <summary>
  807. /// Save xaml file.
  808. /// </summary>
  809. internal void Save(string filename)
  810. {
  811. doc.Save(filename);
  812. }
  813. /// <summary>
  814. /// Save xaml stream.
  815. /// </summary>
  816. internal void Save(Stream stream)
  817. {
  818. doc.Save(stream);
  819. }
  820. /// <param name="name"></param>
  821. /// <param name="Width"></param>
  822. /// <param name="Height"></param>
  823. /// <param name="PagesCount"></param>
  824. /// <param name="Fill"></param>
  825. /// <param name="isScrolled"></param>
  826. internal XAMLDocument(string name, int PagesCount, string Fill, float Width, float Height, bool isScrolled)
  827. {
  828. textObjectStyle = true;
  829. textObjectAngleStyle = true;
  830. lineStyle = true;
  831. rectangleStyle = true;
  832. ellipseStyle = true;
  833. polygonStyle = true;
  834. width = Width;
  835. height = Height;
  836. pages_count = PagesCount;
  837. fill = Fill;
  838. this.isScrolled = isScrolled;
  839. CreateWindow(name);
  840. AddResources();
  841. if (isScrolled)
  842. {
  843. AddScrollViewer();
  844. AddStackPanel(scrollViewer);
  845. }
  846. else
  847. AddStackPanel(root);
  848. }
  849. }
  850. }