AddOMathStructs.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This example shows how to add the various Office Math structures
  15. // to the document content using the DsWord OMath API.
  16. // Note that at this time export of OMath content to PDF/images is not supported.
  17. public class AddOMathStructs
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. var doc = new GcWordDocument();
  22. doc.Body.AddParagraph("OMath content created in DsWord", doc.Styles[BuiltInStyleId.Title]);
  23.  
  24. // Style for paragraphs containing OMath elements:
  25. var styleOmath = doc.Styles.Add("omath", doc.Styles[BuiltInStyleId.Title]);
  26. styleOmath.ParagraphFormat.Indentation.LeftIndent = 36;
  27. // Style for narrative text in OMath paragraphs:
  28. var styleText = doc.Styles.Add("text", StyleType.Character);
  29. styleText.Font.Size = 14;
  30. // Captions style:
  31. var styleCap1 = doc.Styles.Add("styleCap1", doc.Styles[BuiltInStyleId.Heading1]);
  32. styleCap1.ParagraphFormat.Spacing.SpaceBeforeAuto = false;
  33. styleCap1.ParagraphFormat.Spacing.SpaceBefore = 18;
  34. var capA = "\nExample 1: ";
  35. var capB = "\nExample 2: ";
  36.  
  37. OMath addA(string caption)
  38. {
  39. doc.Body.AddParagraph(caption, styleCap1);
  40. var p = doc.Body.AddParagraph(styleOmath);
  41. p.AddRun(capA, styleText);
  42. return p.AddOMath();
  43. }
  44. OMath addB()
  45. {
  46. var p = doc.Body.AddParagraph(styleOmath);
  47. p.AddRun(capB, styleText);
  48. return p.AddOMath();
  49. }
  50.  
  51. // OMathAccent
  52. var om = addA("OMathAccent");
  53. om.AddAccent("a", "\u20d7");
  54. om = addB();
  55. var accent = om.AddAccent("", "\u0308");
  56. accent.Base.AddFraction("2", "5", null);
  57.  
  58. // OMathBar
  59. om = addA("OMathBar");
  60. om.AddBar("2x+1=3");
  61. om = addB();
  62. var bar = om.AddBar();
  63. bar.Base.AddFunction("cos", "2x");
  64. bar.Position = OMathVerticalJustification.Top;
  65.  
  66. // OMathBorderBox
  67. om = addA("OMathBorderBox");
  68. om.AddBorderBox("xyz");
  69. om = addB();
  70. var bbox = om.AddBorderBox("2+2*");
  71. bbox.Base.AddFraction("3", "3", null);
  72. bbox.Base.AddRun(" = 5");
  73. bbox.HideLeft = true;
  74. bbox.HideTop = true;
  75. bbox.HideRight = true;
  76. bbox.HideBottom = true;
  77. bbox.StrikeBottomLeftToTopRight = true;
  78. bbox.StrikeTopLeftToBottomRight = true;
  79.  
  80. // OMathBox
  81. om = addA("OMathBox");
  82. om.AddRun("a");
  83. var box = om.AddBox("==");
  84. om.AddRun("b");
  85. box.IsOperatorEmulator = true;
  86. om = addB();
  87. var nary = om.AddNary("x", "0", "1", "∭");
  88. box = nary.Base.AddBox("dx");
  89. box.IsDifferential = true;
  90.  
  91. // OMathDelimiter
  92. om = addA("OMathDelimiter");
  93. om.AddDelimiter(new string[] {"a", "b", "c"});
  94. om = addB();
  95. var delimiter = om.AddDelimiter(new string[] { "5", "7" }, "[", "]", "\\");
  96. delimiter.Items.Add().AddRadical("n", "7");
  97. var styleNorm = doc.Styles.Add("Norm", StyleType.Character);
  98. styleNorm.Font.Italic = false;
  99. styleNorm.Font.Color.RGB = Color.Red;
  100. styleNorm.Font.Size = 20;
  101.  
  102. // OMathEquationArray
  103. om = addA("OMathEquationArray");
  104. om.AddEquationArray(new string[] { "a", "b", "c" });
  105. om = addB();
  106. var ea = om.AddEquationArray();
  107. ea.Items.Add().AddDelimiter(new string[] { "x", "y", "z" });
  108. ea.Items.Add().AddFraction("π", "2", OMathFractionType.Skewed);
  109.  
  110. // OMathFraction
  111. om = addA("OMathFraction");
  112. om.AddFraction("π", "4", null);
  113. om = addB();
  114. var fr = om.AddFraction();
  115. var func = fr.Numerator.AddFunction("sin", "");
  116. func.Argument.AddFraction("π", "3", null);
  117. fr.Denominator.AddRadical("e", "3");
  118.  
  119. // OMathFunction
  120. om = addA("OMathFunction");
  121. om.AddFunction("ln", "2e");
  122. om = addB();
  123. var f = om.AddFunction("cos", "");
  124. f.Argument.AddFraction("π", "2", null);
  125.  
  126. // OMathGroupCharacter
  127. om = addA("OMathGroupCharacter");
  128. om.AddGroupCharacter("mgh", "^");
  129. om = addB();
  130. var lu = om.AddLimitUpper("", "x");
  131. var gc = lu.Base.AddGroupCharacter("xyz", "\u23DE");
  132. gc.Position = OMathVerticalJustification.Top;
  133. gc.VerticalJustification = OMathVerticalJustification.Bottom;
  134.  
  135. // OMathLimitLower
  136. om = addA("OMathLimitLower");
  137. om.AddLimitLower("abc", "z");
  138. om = addB();
  139. var ll = om.AddLimitLower();
  140. ll.Base.AddBorderBox("2x+3");
  141. ll.Limit.AddFraction("2", "5", null);
  142.  
  143. // OMathLimitUpper
  144. om = addA("OMathLimitUpper");
  145. om.AddLimitUpper("abc", "y");
  146. om = addB();
  147. lu = om.AddLimitUpper();
  148. lu.Base.AddRadical("e", "3");
  149. lu.Limit.AddFunction("cos", "2x");
  150.  
  151. // OMathMatrix
  152. om = addA("OMathMatrix");
  153. om.AddMatrix(new int?[2, 2] { { 1, 2 }, { 3, 4 } });
  154. om = addB();
  155. delimiter = om.AddDelimiter();
  156. var item = delimiter.Items.Add();
  157. var matrix = item.AddMatrix(3, 1);
  158. matrix.Rows[0].Cells[0].AddRun("U");
  159. matrix.Rows[0].Cells[0].AddDelimiter(new string[] { "t" });
  160. matrix.Rows[1].Cells[0].AddRun("V");
  161. matrix.Rows[1].Cells[0].AddDelimiter(new string[] { "t" });
  162. matrix.Rows[2].Cells[0].AddRun("W");
  163. matrix.Rows[2].Cells[0].AddDelimiter(new string[] { "t" });
  164.  
  165. // OMathNary
  166. om = addA("OMathNary");
  167. om.AddNary("xⅆx", "0", "∞", "∭");
  168. om = addB();
  169. nary = om.AddNary("", "", "", "∏");
  170. nary.Subscript.AddFraction("π", "3", null);
  171. nary.Superscript.AddFraction("2π", "3", null);
  172. nary.Base.AddFunction("cos", "x");
  173.  
  174. // OMathPhantom
  175. om = addA("OMathPhantom");
  176. var del = om.AddDelimiter(null, "<", ">", "");
  177. var ph = del.Items.Add().AddPhantom("argument", false);
  178. ph.Show = false;
  179. om = addB();
  180. var radical = om.AddRadical();
  181. radical.HideDegree = true;
  182. radical.HideDegree = true;
  183. ph = radical.Radicand.AddPhantom();
  184. ph.Show = true;
  185. ph.Argument.AddFraction("x", "y", null);
  186. ph.Smash = true;
  187.  
  188. // OMathPreSubSuperscript
  189. om = addA("OMathPreSubSuperscript");
  190. om.AddPreSubSuperscript("Y", "1", "n");
  191. om = addB();
  192. var pss = om.AddPreSubSuperscript("", "-x", "2x");
  193. pss.Base.AddRadical("n", "").HideDegree = true;
  194.  
  195. // OMathRadical
  196. om = addA("OMathRadical");
  197. om.AddRadical("x", "3");
  198. om = addB();
  199. radical = om.AddRadical();
  200. radical.Degree.AddFraction("2", "3", OMathFractionType.Skewed);
  201. radical.Radicand.AddFunction("cos", "2x");
  202.  
  203. // OMathSubscript
  204. om = addA("OMathSubscript");
  205. om.AddSubscript("x", "linear");
  206. om = addB();
  207. func = om.AddFunction("", "2x+1");
  208. func.Name.AddSubscript("log", "3");
  209.  
  210. // OMathSubSuperscript
  211. om = addA("OMathSubSuperscript");
  212. om.AddSubSuperscript("Y", "1", "n");
  213. om = addB();
  214. var subs = om.AddSubSuperscript("", "-x", "2x");
  215. subs.Base.AddRadical("n", "").HideDegree = true;
  216.  
  217. // OMathSuperscript
  218. om = addA("OMathSuperscript");
  219. om.AddSuperscript("x", "2");
  220. om = addB();
  221. func = om.AddFunction();
  222. func.Name.AddSuperscript("cos", "2");
  223. var frac = func.Argument.AddFraction();
  224. frac.Numerator.AddRun("7x");
  225. frac.Denominator.AddRun("2π");
  226.  
  227. // Done:
  228. return doc;
  229. }
  230. }
  231. }
  232.