-
Notifications
You must be signed in to change notification settings - Fork 203
/
CreateEquation.cs
766 lines (679 loc) · 33.4 KB
/
CreateEquation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
using System.Collections.Generic;
#endregion
using System;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
using Syncfusion.Office;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
using System.IO;
using COLOR = Syncfusion.Drawing;
using System.Reflection;
using Syncfusion.iOS.Buttons;
#if __UNIFIED__
using Foundation;
using UIKit;
using CoreGraphics;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using CGRect = System.Drawing.RectangleF;
using CGPoint = System.Drawing.PointF;
using CGSize = System.Drawing.SizeF;
#endif
namespace SampleBrowser
{
public partial class CreateEquation : SampleView
{
CGRect frameRect = new CGRect();
float frameMargin = 8.0f;
UILabel label;
UILabel imageTypelabel;
UIButton imageType;
UIButton button;
//RadioButton
SfRadioGroup radioGroup = new SfRadioGroup();
SfRadioButton docxButton = new SfRadioButton();
SfRadioButton pdfButton = new SfRadioButton();
public CreateEquation()
{
label = new UILabel();
imageTypelabel = new UILabel();
imageType = new UIButton();
button = new UIButton(UIButtonType.System);
button.TouchUpInside += OnButtonClicked;
}
void LoadAllowedTextsLabel()
{
label.Frame = frameRect;
label.TextColor = UIColor.FromRGB(38 / 255.0f, 38 / 255.0f, 38 / 255.0f);
label.Text = "This sample demonstrates how to create a Word document with mathematical equations using Essential DocIO.";
label.Lines = 0;
label.Font = UIFont.SystemFontOfSize(15);
label.LineBreakMode = UILineBreakMode.WordWrap;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
label.Font = UIFont.SystemFontOfSize(18);
label.Frame = new CGRect(5, 5, frameRect.Location.X + frameRect.Size.Width, 70);
}
else
{
label.Frame = new CGRect(frameRect.Location.X, 5, frameRect.Size.Width, 70);
}
this.AddSubview(label);
#region ImageFormat Label
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
imageTypelabel.Font = UIFont.SystemFontOfSize(18);
imageTypelabel.Frame = new CGRect(10, 45, frameRect.Location.X + frameRect.Size.Width - 20, 50);
imageType.Frame = new CGRect(10, 95, frameRect.Location.X + frameRect.Size.Width - 20, 40);
}
else
{
imageTypelabel.Frame = new CGRect(10, 75, frameRect.Location.X + frameRect.Size.Width - 20, 50);
imageType.Frame = new CGRect(10, 100, frameRect.Location.X + frameRect.Size.Width - 20, 40);
}
//filter Label
imageTypelabel.TextColor = UIColor.Black;
imageTypelabel.BackgroundColor = UIColor.Clear;
imageTypelabel.Text = "Save As:";
imageTypelabel.TextAlignment = UITextAlignment.Left;
imageTypelabel.Font = UIFont.FromName("Helvetica", 16f);
this.AddSubview(imageTypelabel);
#endregion
#region Radio Buttons
radioGroup.Axis = UILayoutConstraintAxis.Horizontal;
docxButton.SetTitle("DOCX", UIControlState.Normal);
radioGroup.AddArrangedSubview(docxButton);
pdfButton.SetTitle("PDF", UIControlState.Normal);
radioGroup.AddArrangedSubview(pdfButton);
docxButton.IsChecked = true;
radioGroup.Distribution = UIStackViewDistribution.FillProportionally;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
radioGroup.Frame = new CGRect(110, 55, frameRect.Width - 500, 30);
}
else
{
radioGroup.Frame = new CGRect(110, 85, frameRect.Width - 90, 30);
}
this.AddSubview(radioGroup);
#endregion
button.SetTitle("Generate", UIControlState.Normal);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(5, 90, frameRect.Location.X + frameRect.Size.Width, 10);
}
else
{
button.Frame = new CGRect(frameRect.Location.X, 130, frameRect.Size.Width, 10);
}
button.TouchUpInside += OnButtonClicked;
this.AddSubview(button);
}
void OnButtonClicked(object sender, EventArgs e)
{
string resourcePath = "SampleBrowser.Samples.DocIO.Templates.CreateEquation.docx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
// Loads the stream into Word Document.
WordDocument document = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic);
//Gets the last section in the document
WSection section = document.LastSection;
//Sets page margins
document.LastSection.PageSetup.Margins.All = 72;
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
//Appends text to paragraph
IWTextRange textRange = paragraph.AppendText("Mathematical equations");
textRange.CharacterFormat.FontSize = 28;
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
paragraph.ParagraphFormat.AfterSpacing = 12;
#region Sum to the power of n
//Adds new paragraph to the section
paragraph = AddParagraph(section, "This is an expansion of the sum (1+X) to the power of n.");
//Creates an equation with sum to the power of N
CreateSumToThePowerOfN(paragraph);
#endregion
#region Fourier series
//Adds new paragraph to the section
paragraph = AddParagraph(section, "This is a Fourier series for the function of period 2L");
//Creates a Fourier series equation
CreateFourierseries(paragraph);
#endregion
#region Triple scalar product
//Adds new paragraph to the section
paragraph = AddParagraph(section, "This is an expansion of triple scalar product");
//Creates a triple scalar product equation
CreateTripleScalarProduct(paragraph);
#endregion
#region Gamma function
//Adds new paragraph to the section
paragraph = AddParagraph(section, "This is an expansion of gamma function");
//Creates a gamma function equation
CreateGammaFunction(paragraph);
#endregion
#region Vector relation
//Adds new paragraph to the section
paragraph = AddParagraph(section, "This is an expansion of vector relation ");
//Creates a vector relation equation
CreateVectorRelation(paragraph);
#endregion
string fileName = null;
string ContentType = null;
MemoryStream ms = new MemoryStream();
if (pdfButton != null && (bool)pdfButton.IsChecked)
{
fileName = "CreateEquation.pdf";
ContentType = "application/pdf";
DocIORenderer renderer = new DocIORenderer();
PdfDocument pdfDoc = renderer.ConvertToPDF(document);
pdfDoc.Save(ms);
pdfDoc.Close();
}
else
{
fileName = "CreateEquation.docx";
ContentType = "application/msword";
document.Save(ms, FormatType.Docx);
}
//Reset the stream position
ms.Position = 0;
document.Close();
if (ms != null)
{
SaveiOS iOSSave = new SaveiOS();
iOSSave.Save(fileName, ContentType, ms as MemoryStream);
}
}
public override void LayoutSubviews()
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
frameMargin = 0.0f;
}
frameRect = Frame;
frameRect.Location = new CGPoint(frameRect.Location.X + frameMargin, frameRect.Location.Y + 1.5f * frameMargin);
frameRect.Size = new CGSize(frameRect.Size.Width - (frameRect.Location.X + frameMargin), frameRect.Size.Height - (frameRect.Location.Y + 1.5f * frameMargin));
LoadAllowedTextsLabel();
base.LayoutSubviews();
}
#region Helper Methods
/// <summary>
/// Adds new paragraph into the section
/// </summary>
/// <param name="section">Represents a section in Word document</param>
/// <param name="text">Represents a text to append in paragraph</param>
/// <returns>Returns a paragraph to add equation</returns>
private IWParagraph AddParagraph(IWSection section, string text)
{
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
//Adds new paragraph to add text
paragraph = section.AddParagraph();
//Appends text to paragraph
paragraph.AppendText(text);
paragraph.ParagraphFormat.AfterSpacing = 12;
paragraph.ParagraphFormat.BeforeSpacing = 12;
//Adds new paragraph to add equation
paragraph = section.AddParagraph();
paragraph.ParagraphFormat.AfterSpacing = 12;
return paragraph;
}
/// <summary>
/// Creates an expansion of triple scalar product
/// </summary>
/// <param name="paragraph">Represents a paragraph to add MathML element</param>
private void CreateTripleScalarProduct(IWParagraph paragraph)
{
WordDocument document = paragraph.Document;
//Creates a MathML element
WMath math = paragraph.AppendMath();
//Adds an office math
IOfficeMath officeMath = math.MathParagraph.Maths.Add();
#region Math text
//Unicode value of middle dot
string middleDot = "\u00B7";
string multiplicationSign = "\u00D7";
string text = "A" + middleDot + "B" + multiplicationSign + "C";
//Adds a math item
IOfficeMathRunElement officeMathParaItem = AddMathText(document, officeMath, text);
//Sets style for math text
officeMathParaItem.MathFormat.Style = MathStyleType.Bold;
//Adds math text
officeMathParaItem = AddMathText(document, officeMath, "=");
//Sets style for math text
officeMathParaItem.MathFormat.Style = MathStyleType.Bold;
//Adds math text
text = "A" + multiplicationSign + "B" + middleDot + "C";
officeMathParaItem = AddMathText(document, officeMath, text);
//Sets style for math text
officeMathParaItem.MathFormat.Style = MathStyleType.Bold;
//Adds math text
officeMathParaItem = AddMathText(document, officeMath, "=");
#endregion
#region Delimiter
//Adds a delimiter
IOfficeMathDelimiter mathDelimiter = officeMath.Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Sets begin character for delimiter
mathDelimiter.BeginCharacter = "|";
//Sets end character for delimiter
mathDelimiter.EndCharacter = "|";
//Apply formats for delimiter
mathDelimiter.ControlProperties = new WCharacterFormat(document);
(mathDelimiter.ControlProperties as WCharacterFormat).Italic = true;
//Adds arguments for delimiter
officeMath = mathDelimiter.Equation.Add() as IOfficeMath;
#region Matrix
//Add matrix into delimiter
IOfficeMathMatrix mathMatrix = officeMath.Functions.Add(MathFunctionType.Matrix) as IOfficeMathMatrix;
//Add columns in matrix
mathMatrix.Columns.Add();
mathMatrix.Columns.Add();
mathMatrix.Columns.Add();
#region First row
//Adds a new row
IOfficeMathMatrixRow mathMatrixRow = mathMatrix.Rows.Add();
///Add values to row
AddMatrixRowValues(document, mathMatrixRow, "A");
#endregion
#region Second row
//Adds a new row
mathMatrixRow = mathMatrix.Rows.Add();
///Add values to row
AddMatrixRowValues(document, mathMatrixRow, "B");
#endregion
#region Third row
//Adds a new row
mathMatrixRow = mathMatrix.Rows.Add();
///Add values to row
AddMatrixRowValues(document, mathMatrixRow, "C");
#endregion
#endregion
#endregion
}
/// <summary>
/// Creates an expansion of vector relation
/// </summary>
/// <param name="paragraph">Represents a paragraph to add MathML element</param>
private void CreateVectorRelation(IWParagraph paragraph)
{
WordDocument document = paragraph.Document;
//Creates a MathML element
WMath math = paragraph.AppendMath();
IOfficeMath officeMath = math.MathParagraph.Maths.Add();
//Adds an accent equation
AddMathAccent(document, officeMath, 8407, "A");
//Adds a math text
string middleDot = "\u00B7";
officeMath = math.MathParagraph.Maths.Add();
IOfficeMathRunElement officeMathParaItem = AddMathText(document, officeMath, middleDot);
//Adds an accent equation
officeMath = math.MathParagraph.Maths.Add();
AddMathAccent(document, officeMath, 8407, "B");
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
string multiplicationSign = "\u00D7";
officeMathParaItem = AddMathText(document, officeMath, multiplicationSign);
//Adds an accent equation
officeMath = math.MathParagraph.Maths.Add();
AddMathAccent(document, officeMath, 8407, "C");
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
officeMathParaItem = AddMathText(document, officeMath, "=");
//Adds an accent equation
officeMath = math.MathParagraph.Maths.Add();
AddMathAccent(document, officeMath, 8407, "A");
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
officeMathParaItem = AddMathText(document, officeMath, multiplicationSign);
//Adds an accent equation
officeMath = math.MathParagraph.Maths.Add();
AddMathAccent(document, officeMath, 8407, "B");
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
officeMathParaItem = AddMathText(document, officeMath, middleDot);
//Adds an accent equation
officeMath = math.MathParagraph.Maths.Add();
AddMathAccent(document, officeMath, 8407, "C");
}
/// <summary>
/// Converts short value to string
/// </summary>
/// <param name="value">Represents a short value</param>
private static string ConvertShortToString(short value)
{
char chrValue = Convert.ToChar(value);
string strValue = Convert.ToString(chrValue);
return strValue;
}
/// <summary>
/// Creates an equation with sum to the power of N
/// </summary>
/// <param name="paragraph">Represents a paragraph to add MathML element</param>
private void CreateSumToThePowerOfN(IWParagraph paragraph)
{
WordDocument document = paragraph.Document;
//Creates a new MathML element
WMath math = paragraph.AppendMath();
IOfficeMath officeMath = math.MathParagraph.Maths.Add();
//Adds a math script element
IOfficeMathScript mathScript = AddMathScript(officeMath, MathScriptType.Superscript);
#region Delimiter equation
//Adds a delimiter
IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Adds an office math in the delimiter
officeMath = mathDelimiter.Equation.Add() as IOfficeMath;
//Adds a math text
IOfficeMathRunElement mathParaItem = AddMathText(document, officeMath, "1+x");
//Adds a math text
mathParaItem = AddMathText(document, mathScript.Script, "n");
#endregion
//Adds a math text
officeMath = math.MathParagraph.Maths.Add(1);
mathParaItem = AddMathText(document, officeMath, "=1+");
#region Fraction equation
//Adds a math fraction
IOfficeMathFraction mathFraction = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.Fraction) as IOfficeMathFraction;
//Adds a numerator text
AddMathText(document, mathFraction.Numerator, "nx");
//Adds a denominator text
AddMathText(document, mathFraction.Denominator, "1!");
#endregion
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
mathParaItem = AddMathText(document, officeMath, "+");
#region Fraction equation
//Adds a math fraction
mathFraction = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.Fraction) as IOfficeMathFraction;
#region Numerator
//Adds a numerator text
AddMathText(document, mathFraction.Numerator, "n");
//Adds a delimiter
mathDelimiter = mathFraction.Numerator.Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Adds a math text for delimiter
officeMath = mathDelimiter.Equation.Add() as IOfficeMath;
AddMathText(document, officeMath, "n-1");
//Adds a math script
mathScript = mathFraction.Numerator.Functions.Add(MathFunctionType.SubSuperscript) as IOfficeMathScript;
//Adds a math text for Superscript
AddMathText(document, mathScript.Equation, "x");
AddMathText(document, mathScript.Script, "2");
#endregion
#region Denominator
//Adds a math text for denominator
AddMathText(document, mathFraction.Denominator, "2!");
#endregion
#endregion
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
AddMathText(document, officeMath, "+...");
}
/// <summary>
/// Creates an expansion of Gamma function
/// </summary>
/// <param name="paragraph">Represents a paragraph to add MathML element</param>
private void CreateGammaFunction(IWParagraph paragraph)
{
WordDocument document = paragraph.Document;
//Creates a new MathML element
WMath math = paragraph.AppendMath();
//Adds a math text
IOfficeMath officeMath = math.MathParagraph.Maths.Add();
//Unicode value of capital gamma
string capitalGamma = "\u0393";
IOfficeMathRunElement officeMathParaItem = AddMathText(document, officeMath, capitalGamma);
//Sets MathML style format for the text
officeMathParaItem.MathFormat.Style = MathStyleType.Regular;
//Adds a delimiter equation
IOfficeMathDelimiter mathDelimiter = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Adds a equation to the delimiter
officeMath = mathDelimiter.Equation.Add();
//Adds a math text
officeMathParaItem = AddMathText(document, officeMath, "z");
//Adds a math text
officeMath = math.MathParagraph.Maths.Add();
officeMathParaItem = AddMathText(document, officeMath, "=");
//Adds an n array element
IOfficeMathNArray mathNAry = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.NArray) as IOfficeMathNArray;
//Adds a math text
AddMathText(document, mathNAry.Subscript, "0");
//Adds a math text
string infinitySymbol = "\u221E";
AddMathText(document, mathNAry.Superscript, infinitySymbol);
//Adds a math superscript
IOfficeMathScript mathScript = AddMathScript(mathNAry.Equation, MathScriptType.Superscript);
//Adds a math text for Superscript
AddMathText(document, mathScript.Equation, "t");
AddMathText(document, mathScript.Script, "z-1");
//Adds a Superscript
mathScript = AddMathScript(mathNAry.Equation, MathScriptType.Superscript);
AddMathText(document, mathScript.Equation, "e");
AddMathText(document, mathScript.Script, "-t");
//Adds a math text in n Array equation
AddMathText(document, mathNAry.Equation, "dt");
//Adds a math text
AddMathText(document, math.MathParagraph.Maths.Add(), "=");
//Adds a fraction equation
IOfficeMathFraction mathFraction = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.Fraction) as IOfficeMathFraction;
//Adds a math script
mathScript = AddMathScript(mathFraction.Numerator, MathScriptType.Superscript);
//Adds a math text for Superscript
AddMathText(document, mathScript.Equation, "e");
AddMathText(document, mathScript.Script, "-");
//Unicode of small gamma
string smallGamma = "\u03B3";
AddMathText(document, mathScript.Script, smallGamma);
AddMathText(document, mathScript.Script, "z");
//Adds a math text for denominator
AddMathText(document, mathFraction.Denominator, "z");
//Adds an n-array element
mathNAry = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.NArray) as IOfficeMathNArray;
//Unicode value of n-array product
string symbol = "\u220F";
//Adds a n-array character
mathNAry.NArrayCharacter = symbol;
//Adds a math text
AddMathText(document, mathNAry.Subscript, "k=1");
//Adds a math text
AddMathText(document, mathNAry.Superscript, infinitySymbol);
//Adds a math script
mathScript = AddMathScript(mathNAry.Equation, MathScriptType.Superscript);
//Adds a math delimiter element
mathDelimiter = mathScript.Equation.Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Adds a equation to the delimiter equation collection
officeMath = mathDelimiter.Equation.Add();
//Adds a math text
AddMathText(document, officeMath, "1+");
//Adds a fraction element
mathFraction = officeMath.Functions.Add(MathFunctionType.Fraction) as IOfficeMathFraction;
//Adds a math text for numerator
AddMathText(document, mathFraction.Numerator, "z");
//Adds a math text for denominator
AddMathText(document, mathFraction.Denominator, "k");
//Adds a math text
AddMathText(document, mathScript.Script, "-1");
//Adds a Superscript equation
mathScript = AddMathScript(mathNAry.Equation, MathScriptType.Superscript);
//Adds a math text for Superscript
AddMathText(document, mathScript.Equation, "e");
AddMathText(document, mathScript.Script, "z");
officeMathParaItem = AddMathText(document, mathScript.Script, "/");
officeMathParaItem.MathFormat.HasLiteral = true;
AddMathText(document, mathScript.Script, "k");
//Adds a math text
AddMathText(document, math.MathParagraph.Maths.Add(), ",");
//Adds a math text
officeMathParaItem = AddMathText(document, math.MathParagraph.Maths.Add(), " ");
//Sets style for math text
officeMathParaItem.MathFormat.Style = MathStyleType.Regular;
//Adds a math text
AddMathText(document, math.MathParagraph.Maths.Add(), smallGamma);
string text = "\u2248" + "0.577216";
AddMathText(document, math.MathParagraph.Maths.Add(), text);
}
/// <summary>
/// Creates a Fourier series equation
/// </summary>
/// <param name="paragraph">Represents a paragraph to add MathML element</param>
private void CreateFourierseries(IWParagraph paragraph)
{
WordDocument document = paragraph.Document;
//Creates a new MathML element
WMath math = paragraph.AppendMath();
//Adds a math
IOfficeMath officeMath = math.MathParagraph.Maths.Add();
//Adds a math text
AddMathText(document, officeMath, "f");
//Adds a math delimiter
IOfficeMathDelimiter mathDelimiter = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Adds a math in the delimiter
officeMath = mathDelimiter.Equation.Add() as IOfficeMath;
//Adds a math text
AddMathText(document, officeMath, "x");
AddMathText(document, math.MathParagraph.Maths.Add(), "=");
//Adds a Subscript equation
IOfficeMathScript mathScript = AddMathScript(math.MathParagraph.Maths.Add(), MathScriptType.Subscript);
//Adds a math text
AddMathText(document, mathScript.Equation, "a");
AddMathText(document, mathScript.Script, "0");
//Adds a math text
AddMathText(document, math.MathParagraph.Maths.Add(), "+");
//Adds a math n-array
IOfficeMathNArray mathNAry = math.MathParagraph.Maths.Add().Functions.Add(MathFunctionType.NArray) as IOfficeMathNArray;
//Unicode value of n-array summation
string sigma = "\u2211";
//Sets the value as the n-array character
mathNAry.NArrayCharacter = sigma;
mathNAry.HasGrow = true;
//Adds a math text
AddMathText(document, mathNAry.Subscript, "n=1");
//Adds a math text
string infinitySymbol = "\u221E";
AddMathText(document, mathNAry.Superscript, infinitySymbol);
//Adds a math delimiter
mathDelimiter = mathNAry.Equation.Functions.Add(MathFunctionType.Delimiter) as IOfficeMathDelimiter;
//Adds an math in the delimiter equation collection
officeMath = mathDelimiter.Equation.Add() as IOfficeMath;
//Adds a math script
mathScript = AddMathScript(officeMath, MathScriptType.Subscript);
//Adds a math text
AddMathText(document, mathScript.Equation, "a");
//Adds a math text
AddMathText(document, mathScript.Script, "n");
//Adds a math function
IOfficeMathFunction mathFunction = officeMath.Functions.Add(MathFunctionType.Function) as IOfficeMathFunction;
//Adds a math text
IOfficeMathRunElement mathParaItem = AddMathText(document, mathFunction.FunctionName, "cos");
mathParaItem.MathFormat.Style = MathStyleType.Regular;
//Adds a math fraction
IOfficeMathFraction mathFraction = mathFunction.Equation.Functions.Add(MathFunctionType.Fraction) as IOfficeMathFraction;
//Adds a math text
//Unicode value of PI
string pi = "\uD835\uDF0B";
string text = "n" + pi + "x";
AddMathText(document, mathFraction.Numerator, text);
AddMathText(document, mathFraction.Denominator, "L");
//Adds a math text
AddMathText(document, officeMath, "+");
//Adds a math script
mathScript = AddMathScript(officeMath, MathScriptType.Subscript);
//Adds a math text
AddMathText(document, mathScript.Equation, "b");
AddMathText(document, mathScript.Script, "n");
//Adds a function
mathFunction = officeMath.Functions.Add(MathFunctionType.Function) as IOfficeMathFunction;
//Adds a math text
mathParaItem = AddMathText(document, mathFunction.FunctionName, "sin");
mathParaItem.MathFormat.Style = MathStyleType.Regular;
//Adds a math fraction element
mathFraction = mathFunction.Equation.Functions.Add(MathFunctionType.Fraction) as IOfficeMathFraction;
//Adds a math text for numerator
AddMathText(document, mathFraction.Numerator, text);
//Adds a math text for denominator
AddMathText(document, mathFraction.Denominator, "L");
}
/// <summary>
/// Adds a math text
/// </summary>
/// <param name="document">Represents a Word document to add math text</param>
/// <param name="officeMath">Represents an office math to add math text</param>
/// <param name="text">Represents the text to set for math item</param>
private IOfficeMathRunElement AddMathText(WordDocument document, IOfficeMath officeMath, string text)
{
//Adds math text
IOfficeMathRunElement officeMathParaItem = officeMath.Functions.Add(MathFunctionType.RunElement) as IOfficeMathRunElement;
officeMathParaItem.Item = new WTextRange(document);
//Set math text value
(officeMathParaItem.Item as WTextRange).Text = text;
return officeMathParaItem;
}
/// <summary>
/// Adds a math Subscript or Superscript equation
/// </summary>
private IOfficeMathScript AddMathScript(IOfficeMath officeMath, MathScriptType mathScriptType)
{
IOfficeMathScript mathScript = officeMath.Functions.Add(MathFunctionType.SubSuperscript) as IOfficeMathScript;
//Sets the script type as Subscript or Superscript
mathScript.ScriptType = mathScriptType;
return mathScript;
}
/// <summary>
/// Adds a accent equation
/// </summary>
/// <param name="document">Represents a Word document</param>
/// <param name="officeMath">Represents a office math to add accent equation</param>
/// <param name="accentCharValue">Represents a accent character</param>
/// <param name="text">Represents a text for accent equation</param>
private void AddMathAccent(WordDocument document, IOfficeMath officeMath, short accentCharValue, string text)
{
IOfficeMathAccent mathAccent = officeMath.Functions.Add(MathFunctionType.Accent) as IOfficeMathAccent;
//Sets the accent character from short value
mathAccent.AccentCharacter = ConvertShortToString(accentCharValue);
//Adds a math text
IOfficeMathRunElement officeMathParaItem = AddMathText(document, mathAccent.Equation, text);
}
/// <summary>
/// Add values in matrix row
/// </summary>
/// <param name="document">Represents a Word document to add matrix</param>
/// <param name="mathMatrixRow">Represents a matrix row to add values</param>
/// <param name="text">Represents a base text value for Subscript and Superscript equation</param>
private void AddMatrixRowValues(WordDocument document, IOfficeMathMatrixRow mathMatrixRow, string text)
{
//Adds arguments for matrix row
IOfficeMath officeMath = mathMatrixRow.Arguments[0];
//Adds a Subscript
IOfficeMathScript mathScript = AddMathScript(officeMath, MathScriptType.Subscript);
//Adds a math text
IOfficeMathRunElement officeMathParaItem = AddMathText(document, mathScript.Equation, text);
//Adds math text
officeMathParaItem = AddMathText(document, mathScript.Script, "x");
//Adds arguments for matrix row
officeMath = mathMatrixRow.Arguments[1];
//Adds a script
mathScript = AddMathScript(officeMath, MathScriptType.Subscript);
//Adds math text
officeMathParaItem = AddMathText(document, mathScript.Equation, text);
//Adds math text
officeMathParaItem = AddMathText(document, mathScript.Script, "y");
//Adds arguments for matrix row
officeMath = mathMatrixRow.Arguments[2];
//Adds a script
mathScript = AddMathScript(officeMath, MathScriptType.Subscript);
//Adds math text
officeMathParaItem = AddMathText(document, mathScript.Equation, text);
//Adds math text
officeMathParaItem = AddMathText(document, mathScript.Script, "z");
}
#endregion
}
}