/// <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 = new WMath(document); IOfficeMath officeMath = math.MathPara.Maths.Add(0); //Adds a math script element IOfficeMathScript mathScript = AddMathScript(officeMath, 0, MathScriptType.Superscript); #region Delimiter equation //Adds a delimiter IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions.Add(0, MathFunctionType.Delimiter) as IOfficeMathDelimiter; //Adds an office math in the delimiter officeMath = mathDelimiter.Equation.Add(0) as IOfficeMath; //Adds a math text IOfficeMathParaItem mathParaItem = AddMathText(document, officeMath, 0, "1+x"); //Adds a math text mathParaItem = AddMathText(document, mathScript.Script, 0, "n"); #endregion //Adds a math text officeMath = math.MathPara.Maths.Add(1); mathParaItem = AddMathText(document, officeMath, 0, "=1+"); #region Fraction equation //Adds a math fraction IOfficeMathFraction mathFraction = math.MathPara.Maths.Add(2).Functions.Add(0, MathFunctionType.Fraction) as IOfficeMathFraction; //Adds a numerator text AddMathText(document, mathFraction.Numerator, 0, "nx"); //Adds a denominator text AddMathText(document, mathFraction.Denominator, 0, "1!"); #endregion //Adds a math text officeMath = math.MathPara.Maths.Add(3); mathParaItem = AddMathText(document, officeMath, 0, "+"); #region Fraction equation //Adds a math fraction mathFraction = math.MathPara.Maths.Add(4).Functions.Add(0, MathFunctionType.Fraction) as IOfficeMathFraction; #region Numerator //Adds a numerator text AddMathText(document, mathFraction.Numerator, 0, "n"); //Adds a delimiter mathDelimiter = mathFraction.Numerator.Functions.Add(1, MathFunctionType.Delimiter) as IOfficeMathDelimiter; //Adds a math text for delimiter officeMath = mathDelimiter.Equation.Add(0) as IOfficeMath; AddMathText(document, officeMath, 0, "n-1"); //Adds a math script mathScript = mathFraction.Numerator.Functions.Add(2, MathFunctionType.SubSuperScript) as IOfficeMathScript; //Adds a math text for Superscript AddMathText(document, mathScript.Equation, 0, "x"); AddMathText(document, mathScript.Script, 0, "2"); #endregion #region Denominator //Adds a math text for denominator AddMathText(document, mathFraction.Denominator, 0, "2!"); #endregion #endregion //Adds a math text officeMath = math.MathPara.Maths.Add(5); AddMathText(document, officeMath, 0, "+..."); //Adds MathML element into paragraph paragraph.Items.Add(math); }
/// <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 = new WMath(document); //Adds a math text IOfficeMath officeMath = math.MathPara.Maths.Add(0); //Unicode value of capital gamma string capitalGamma = "\u0393"; IOfficeMathParaItem officeMathParaItem = AddMathText(document, officeMath, 0, capitalGamma); //Sets MathML style format for the text officeMathParaItem.MathFormat.Style = MathStyleType.Regular; //Adds a delimiter equation IOfficeMathDelimiter mathDelimiter = math.MathPara.Maths.Add(1).Functions.Add(0, MathFunctionType.Delimiter) as IOfficeMathDelimiter; //Adds a equation to the delimiter officeMath = mathDelimiter.Equation.Add(0); //Adds a math text officeMathParaItem = AddMathText(document, officeMath, 0, "z"); //Adds a math text officeMath = math.MathPara.Maths.Add(2); officeMathParaItem = AddMathText(document, officeMath, 0, "="); //Adds an n array element IOfficeMathNArray mathNAry = math.MathPara.Maths.Add(3).Functions.Add(0, MathFunctionType.NArray) as IOfficeMathNArray; //Adds a math text AddMathText(document, mathNAry.Subscript, 0, "0"); //Adds a math text string infinitySymbol = "\u221E"; AddMathText(document, mathNAry.Superscript, 0, infinitySymbol); //Adds a math superscript IOfficeMathScript mathScript = AddMathScript(mathNAry.Equation, 0, MathScriptType.Superscript); //Adds a math text for Superscript AddMathText(document, mathScript.Equation, 0, "t"); AddMathText(document, mathScript.Script, 0, "z-1"); //Adds a Superscript mathScript = AddMathScript(mathNAry.Equation, 1, MathScriptType.Superscript); AddMathText(document, mathScript.Equation, 0, "e"); AddMathText(document, mathScript.Script, 0, "-t"); //Adds a math text in n Array equation AddMathText(document, mathNAry.Equation, 2, "dt"); //Adds a math text AddMathText(document, math.MathPara.Maths.Add(4), 0, "="); //Adds a fraction equation IOfficeMathFraction mathFraction = math.MathPara.Maths.Add(5).Functions.Add(0, MathFunctionType.Fraction) as IOfficeMathFraction; //Adds a math script mathScript = AddMathScript(mathFraction.Numerator, 0, MathScriptType.Superscript); //Adds a math text for Superscript AddMathText(document, mathScript.Equation, 0, "e"); AddMathText(document, mathScript.Script, 0, "-"); //Unicode of small gamma string smallGamma = "\u03B3"; AddMathText(document, mathScript.Script, 1, smallGamma); AddMathText(document, mathScript.Script, 2, "z"); //Adds a math text for denominator AddMathText(document, mathFraction.Denominator, 0, "z"); //Adds an n-array element mathNAry = math.MathPara.Maths.Add(6).Functions.Add(0, 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, 0, "k=1"); //Adds a math text AddMathText(document, mathNAry.Superscript, 0, infinitySymbol); //Adds a math script mathScript = AddMathScript(mathNAry.Equation, 0, MathScriptType.Superscript); //Adds a math delimiter element mathDelimiter = mathScript.Equation.Functions.Add(0, MathFunctionType.Delimiter) as IOfficeMathDelimiter; //Adds a equation to the delimiter equation collection officeMath = mathDelimiter.Equation.Add(0); //Adds a math text AddMathText(document, officeMath, 0, "1+"); //Adds a fraction element mathFraction = officeMath.Functions.Add(1, MathFunctionType.Fraction) as IOfficeMathFraction; //Adds a math text for numerator AddMathText(document, mathFraction.Numerator, 0, "z"); //Adds a math text for denominator AddMathText(document, mathFraction.Denominator, 0, "k"); //Adds a math text AddMathText(document, mathScript.Script, 0, "-1"); //Adds a Superscript equation mathScript = AddMathScript(mathNAry.Equation, 1, MathScriptType.Superscript); //Adds a math text for Superscript AddMathText(document, mathScript.Equation, 0, "e"); AddMathText(document, mathScript.Script, 0, "z"); officeMathParaItem = AddMathText(document, mathScript.Script, 1, "/"); officeMathParaItem.MathFormat.HasLiteral = true; AddMathText(document, mathScript.Script, 2, "k"); //Adds a math text AddMathText(document, math.MathPara.Maths.Add(7), 0, ","); //Adds a math text officeMathParaItem = AddMathText(document, math.MathPara.Maths.Add(8), 0, " "); //Sets style for math text officeMathParaItem.MathFormat.Style = MathStyleType.Regular; //Adds a math text AddMathText(document, math.MathPara.Maths.Add(9), 0, smallGamma); string text = "\u2248" + "0.577216"; AddMathText(document, math.MathPara.Maths.Add(10), 0, text); //Adds MathML element into paragraph paragraph.Items.Add(math); }
/// <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> /// 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 = new WMath(document); //Adds an office math IOfficeMath officeMath = math.MathPara.Maths.Add(0); #region Math text //Unicode value of middle dot string middleDot = "\u00B7"; string text = "A" + middleDot + "B×C"; //Adds a math item IOfficeMathParaItem officeMathParaItem = AddMathText(document, officeMath, 0, text); //Sets style for math text officeMathParaItem.MathFormat.Style = MathStyleType.Bold; //Adds math text officeMathParaItem = AddMathText(document, officeMath, 1, "="); //Sets style for math text officeMathParaItem.MathFormat.Style = MathStyleType.Bold; //Adds math text text = "A×B" + middleDot + "C"; officeMathParaItem = AddMathText(document, officeMath, 2, text); //Sets style for math text officeMathParaItem.MathFormat.Style = MathStyleType.Bold; //Adds math text officeMathParaItem = AddMathText(document, officeMath, 3, "="); #endregion #region Delimiter //Adds a delimiter IOfficeMathDelimiter mathDelimiter = officeMath.Functions.Add(4, 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(0) as IOfficeMath; #region Matrix //Add matrix into delimiter IOfficeMathMatrix mathMatrix = officeMath.Functions.Add(0, MathFunctionType.Matrix) as IOfficeMathMatrix; #region First row //Adds a new row IOfficeMathMatrixRow mathMatrixRow = mathMatrix.Rows.Add(0); ///Add values to row AddMatrixRowValues(document, mathMatrixRow, "A"); #endregion #region Second row //Adds a new row mathMatrixRow = mathMatrix.Rows.Add(1); ///Add values to row AddMatrixRowValues(document, mathMatrixRow, "B"); #endregion #region Third row //Adds a new row mathMatrixRow = mathMatrix.Rows.Add(2); ///Add values to row AddMatrixRowValues(document, mathMatrixRow, "C"); #endregion #endregion #endregion //Adds MathML element into paragraph paragraph.Items.Add(math); }
private void button1_Click(object sender, System.EventArgs e) { try { // Get XML file path. string dataPath = Application.StartupPath + @"..\..\..\..\..\..\..\common\Data\DocIO\"; //Opens an existing Word document WordDocument document = new WordDocument(dataPath + "Mathematical Equation.docx"); //Gets the last section in the document WSection section = document.LastSection; //Gets paragraph from Word document WParagraph paragraph = document.LastSection.Body.ChildEntities[3] as WParagraph; //Gets MathML from the paragraph WMath math = paragraph.ChildEntities[0] as WMath; //Gets the radical equation IOfficeMathRadical mathRadical = math.MathParagraph.Maths[0].Functions[1] as IOfficeMathRadical; //Gets the fraction equation in radical IOfficeMathFraction mathFraction = mathRadical.Equation.Functions[0] as IOfficeMathFraction; //Gets the n-array in fraction IOfficeMathNArray mathNAry = mathFraction.Numerator.Functions[0] as IOfficeMathNArray; //Gets the math script in n-array IOfficeMathScript mathScript = mathNAry.Equation.Functions[0] as IOfficeMathScript; //Gets the delimiter in math script IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions[0] as IOfficeMathDelimiter; //Gets the math script in delimiter mathScript = mathDelimiter.Equation[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script IOfficeMathRunElement mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the math bar in delimiter IOfficeMathBar mathBar = mathDelimiter.Equation[0].Functions[2] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[6] as WParagraph; //Gets MathML from the paragraph math = paragraph.ChildEntities[0] as WMath; //Gets the math script equation mathScript = math.MathParagraph.Maths[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[7] as WParagraph; //Gets MathML from the paragraph WMath math2 = paragraph.ChildEntities[0] as WMath; //Gets bar equation mathBar = math2.MathParagraph.Maths[0].Functions[0] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Gets the math text (mathParaItem.Item as WTextRange).Text = "x"; //Save as docx format if (wordDocxRadioBtn.Checked) { //Saving the document as .docx document.Save("Edit Equation.docx", FormatType.Docx); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Edit Equation.docx") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Edit Equation.docx"); #endif //Exit this.Close(); } catch (Win32Exception ex) { MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } } //Save as pdf format else if (pdfRadioBtn.Checked) { DocToPDFConverter converter = new DocToPDFConverter(); //Convert word document into PDF document PdfDocument pdfDoc = converter.ConvertToPDF(document); //Save the pdf file pdfDoc.Save("Edit Equation.pdf"); converter.Dispose(); pdfDoc.Close(); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated PDF?", " Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Edit Equation.pdf") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Edit Equation.pdf"); #endif //Exit this.Close(); } catch (Exception ex) { MessageBoxAdv.Show("PDF Viewer is not installed in this system"); Console.WriteLine(ex.ToString()); } } } else { // Exit this.Close(); } } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
public IActionResult EditEquation(string Button) { if (Button == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/Mathematical Equation.docx"; string contenttype1 = "application/vnd.ms-word.document.12"; // Load Template document stream. FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Creates an empty Word document instance. WordDocument document = new WordDocument(); // Opens template document. document.Open(fileStream, FormatType.Docx); fileStream.Dispose(); fileStream = null; //Gets the last section in the document WSection section = document.LastSection; //Gets paragraph from Word document WParagraph paragraph = document.LastSection.Body.ChildEntities[3] as WParagraph; //Gets MathML from the paragraph WMath math = paragraph.ChildEntities[0] as WMath; //Gets the radical equation IOfficeMathRadical mathRadical = math.MathParagraph.Maths[0].Functions[1] as IOfficeMathRadical; //Gets the fraction equation in radical IOfficeMathFraction mathFraction = mathRadical.Equation.Functions[0] as IOfficeMathFraction; //Gets the n-array in fraction IOfficeMathNArray mathNAry = mathFraction.Numerator.Functions[0] as IOfficeMathNArray; //Gets the math script in n-array IOfficeMathScript mathScript = mathNAry.Equation.Functions[0] as IOfficeMathScript; //Gets the delimiter in math script IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions[0] as IOfficeMathDelimiter; //Gets the math script in delimiter mathScript = mathDelimiter.Equation[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script IOfficeMathRunElement mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the math bar in delimiter IOfficeMathBar mathBar = mathDelimiter.Equation[0].Functions[2] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[6] as WParagraph; //Gets MathML from the paragraph math = paragraph.ChildEntities[0] as WMath; //Gets the math script equation mathScript = math.MathParagraph.Maths[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[7] as WParagraph; //Gets MathML from the paragraph WMath math2 = paragraph.ChildEntities[0] as WMath; //Gets bar equation mathBar = math2.MathParagraph.Maths[0].Functions[0] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Gets the math text (mathParaItem.Item as WTextRange).Text = "x"; FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
public ActionResult EditEquation(string Button) { if (Button == null) { return(View()); } //Opens an existing Word document WordDocument document = new WordDocument(ResolveApplicationDataPath("Mathematical Equation.docx", "Data\\DocIO")); //Gets the last section in the document WSection section = document.LastSection; //Gets paragraph from Word document WParagraph paragraph = document.LastSection.Body.ChildEntities[3] as WParagraph; //Gets MathML from the paragraph WMath math = paragraph.ChildEntities[0] as WMath; //Gets the radical equation IOfficeMathRadical mathRadical = math.MathParagraph.Maths[0].Functions[1] as IOfficeMathRadical; //Gets the fraction equation in radical IOfficeMathFraction mathFraction = mathRadical.Equation.Functions[0] as IOfficeMathFraction; //Gets the n-array in fraction IOfficeMathNArray mathNAry = mathFraction.Numerator.Functions[0] as IOfficeMathNArray; //Gets the math script in n-array IOfficeMathScript mathScript = mathNAry.Equation.Functions[0] as IOfficeMathScript; //Gets the delimiter in math script IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions[0] as IOfficeMathDelimiter; //Gets the math script in delimiter mathScript = mathDelimiter.Equation[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script IOfficeMathRunElement mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the math bar in delimiter IOfficeMathBar mathBar = mathDelimiter.Equation[0].Functions[2] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[6] as WParagraph; //Gets MathML from the paragraph math = paragraph.ChildEntities[0] as WMath; //Gets the math script equation mathScript = math.MathParagraph.Maths[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[7] as WParagraph; //Gets MathML from the paragraph WMath math2 = paragraph.ChildEntities[0] as WMath; //Gets bar equation mathBar = math2.MathParagraph.Maths[0].Functions[0] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Gets the math text (mathParaItem.Item as WTextRange).Text = "x"; return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); }
public ActionResult EditEquation(string Button, string Group1) { if (Button == null) { return(View()); } if (Button == "View Template") { return(new TemplateResult("Mathematical Equation.docx", ResolveApplicationDataPath("Data\\DocIO"), HttpContext.ApplicationInstance.Response)); } //Opens an existing Word document WordDocument document = new WordDocument(ResolveApplicationDataPath("Mathematical Equation.docx", "Data\\DocIO")); //Gets the last section in the document WSection section = document.LastSection; //Gets paragraph from Word document WParagraph paragraph = document.LastSection.Body.ChildEntities[3] as WParagraph; //Gets MathML from the paragraph WMath math = paragraph.ChildEntities[0] as WMath; //Gets the radical equation IOfficeMathRadical mathRadical = math.MathParagraph.Maths[0].Functions[1] as IOfficeMathRadical; //Gets the fraction equation in radical IOfficeMathFraction mathFraction = mathRadical.Equation.Functions[0] as IOfficeMathFraction; //Gets the n-array in fraction IOfficeMathNArray mathNAry = mathFraction.Numerator.Functions[0] as IOfficeMathNArray; //Gets the math script in n-array IOfficeMathScript mathScript = mathNAry.Equation.Functions[0] as IOfficeMathScript; //Gets the delimiter in math script IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions[0] as IOfficeMathDelimiter; //Gets the math script in delimiter mathScript = mathDelimiter.Equation[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script IOfficeMathRunElement mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the math bar in delimiter IOfficeMathBar mathBar = mathDelimiter.Equation[0].Functions[2] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[6] as WParagraph; //Gets MathML from the paragraph math = paragraph.ChildEntities[0] as WMath; //Gets the math script equation mathScript = math.MathParagraph.Maths[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[7] as WParagraph; //Gets MathML from the paragraph WMath math2 = paragraph.ChildEntities[0] as WMath; //Gets bar equation mathBar = math2.MathParagraph.Maths[0].Functions[0] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Gets the math text (mathParaItem.Item as WTextRange).Text = "x"; //Save as .docx format if (Group1 == "WordDocx") { return(document.ExportAsActionResult("EditEquation.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .pdf format else if (Group1 == "Pdf") { DocToPDFConverter converter = new DocToPDFConverter(); PdfDocument pdfDoc = converter.ConvertToPDF(document); document.Close(); converter.Dispose(); return(pdfDoc.ExportAsActionResult("EditEquation.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } return(View()); }
public MemoryStream EditEquation(string documentType) { string basePath = @"wwwroot/"; string dataPath = basePath + @"/DocIO/Mathematical Equation.docx"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Creates an empty Word document instance WordDocument document = new WordDocument(); // Opens template document document.Open(fileStream, FormatType.Docx); fileStream.Dispose(); fileStream = null; //Gets the last section in the document WSection section = document.LastSection; //Gets paragraph from Word document WParagraph paragraph = document.LastSection.Body.ChildEntities[3] as WParagraph; //Gets MathML from the paragraph WMath math = paragraph.ChildEntities[0] as WMath; //Gets the radical equation IOfficeMathRadical mathRadical = math.MathParagraph.Maths[0].Functions[1] as IOfficeMathRadical; //Gets the fraction equation in radical IOfficeMathFraction mathFraction = mathRadical.Equation.Functions[0] as IOfficeMathFraction; //Gets the n-array in fraction IOfficeMathNArray mathNAry = mathFraction.Numerator.Functions[0] as IOfficeMathNArray; //Gets the math script in n-array IOfficeMathScript mathScript = mathNAry.Equation.Functions[0] as IOfficeMathScript; //Gets the delimiter in math script IOfficeMathDelimiter mathDelimiter = mathScript.Equation.Functions[0] as IOfficeMathDelimiter; //Gets the math script in delimiter mathScript = mathDelimiter.Equation[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script IOfficeMathRunElement mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the math bar in delimiter IOfficeMathBar mathBar = mathDelimiter.Equation[0].Functions[2] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[6] as WParagraph; //Gets MathML from the paragraph math = paragraph.ChildEntities[0] as WMath; //Gets the math script equation mathScript = math.MathParagraph.Maths[0].Functions[0] as IOfficeMathScript; //Gets the math run element in math script mathParaItem = mathScript.Equation.Functions[0] as IOfficeMathRunElement; //Modifies the math text value (mathParaItem.Item as WTextRange).Text = "x"; //Gets the paragraph from Word document paragraph = document.LastSection.Body.ChildEntities[7] as WParagraph; //Gets MathML from the paragraph WMath math2 = paragraph.ChildEntities[0] as WMath; //Gets bar equation mathBar = math2.MathParagraph.Maths[0].Functions[0] as IOfficeMathBar; //Gets the math run element in bar mathParaItem = mathBar.Equation.Functions[0] as IOfficeMathRunElement; //Gets the math text (mathParaItem.Item as WTextRange).Text = "x"; FormatType type = FormatType.Docx; //Save as .pdf format if (documentType == "PDF") { DocIORenderer render = new DocIORenderer(); // Converts Word document into PDF document. PdfDocument pdf = render.ConvertToPDF(document); //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created PDF document to MemoryStream pdf.Save(stream); render.Dispose(); pdf.Close(); document.Close(); stream.Position = 0; return(stream); } } else { using (MemoryStream stream = new MemoryStream()) { //Save the created Word document to MemoryStream document.Save(stream, type); document.Close(); stream.Position = 0; return(stream); } } }