public IActionResult EditEquation(string Button, string Group1) { if (Button == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/Mathematical Equation.docx"; string contenttype = "application/vnd.ms-word.document.12"; // Load Template document stream. FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (Button == "View Template") { return(File(fileStream, contenttype, "Mathematical Equation.docx")); } // 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"; string filename = ""; MemoryStream ms = new MemoryStream(); #region Document SaveOption if (Group1 == "WordDocx") { filename = "EditEquation.docx"; contenttype = "application/msword"; document.Save(ms, FormatType.Docx); } else { filename = "EditEquation.pdf"; contenttype = "application/pdf"; DocIORenderer renderer = new DocIORenderer(); renderer.ConvertToPDF(document).Save(ms); } #endregion Document SaveOption document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
public MemoryStream EditEquation(string documentType) { string basePath = @"wwwroot/"; string dataPath = basePath + @"data/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); } } }
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)); }
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"; //Saving the document as .docx document.Save("Sample.docx", FormatType.Docx); document.Close(); //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("Sample.docx") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.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()); } } else { // Exit this.Close(); } } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
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()); }