|
| 1 | +using System.Diagnostics; |
| 2 | +using LaTeXEquation.Models; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Syncfusion.DocIO; |
| 5 | +using Syncfusion.DocIO.DLS; |
| 6 | + |
| 7 | +namespace LaTeXEquation.Controllers |
| 8 | +{ |
| 9 | + public class HomeController : Controller |
| 10 | + { |
| 11 | + private readonly ILogger<HomeController> _logger; |
| 12 | + |
| 13 | + public HomeController(ILogger<HomeController> logger) |
| 14 | + { |
| 15 | + _logger = logger; |
| 16 | + } |
| 17 | + |
| 18 | + // Creates a new Word document with mathematical equations |
| 19 | + public IActionResult CreateEquation() |
| 20 | + { |
| 21 | + // Initialize a new Word document and ensure minimal structure |
| 22 | + WordDocument document = new WordDocument(); |
| 23 | + document.EnsureMinimal(); |
| 24 | + |
| 25 | + // Add a main title paragraph |
| 26 | + IWParagraph mainTitle = document.LastSection.AddParagraph(); |
| 27 | + IWTextRange titleText = mainTitle.AppendText("Mathematical Equations"); |
| 28 | + titleText.CharacterFormat.Bold = true; // Make title bold |
| 29 | + titleText.CharacterFormat.FontSize = 18; // Set font size |
| 30 | + mainTitle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; // Center align title |
| 31 | + mainTitle.ParagraphFormat.AfterSpacing = 12f; // Add spacing after title |
| 32 | + |
| 33 | + // Add paragraph for Area of Circle equation |
| 34 | + IWParagraph paragraph1 = document.LastSection.AddParagraph(); |
| 35 | + paragraph1.ParagraphFormat.BeforeSpacing = 8f; // Add spacing before paragraph |
| 36 | + IWTextRange wText1 = paragraph1.AppendText("Area of Circle "); |
| 37 | + wText1.CharacterFormat.Bold = true; // Make label bold |
| 38 | + paragraph1.AppendMath(@"A = \pi r^2"); // Insert LaTeX math equation |
| 39 | + |
| 40 | + // Add paragraph for Quadratic Formula equation |
| 41 | + IWParagraph paragraph2 = document.LastSection.AddParagraph(); |
| 42 | + paragraph2.ParagraphFormat.BeforeSpacing = 8f; |
| 43 | + IWTextRange wText2 = paragraph2.AppendText("Quadratic Formula "); |
| 44 | + wText2.CharacterFormat.Bold = true; |
| 45 | + paragraph2.AppendMath(@"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}"); |
| 46 | + |
| 47 | + // Add paragraph for Fourier Series equation |
| 48 | + IWParagraph paragraph3 = document.LastSection.AddParagraph(); |
| 49 | + paragraph3.ParagraphFormat.BeforeSpacing = 8f; |
| 50 | + IWTextRange wText3 = paragraph3.AppendText("Fourier Series "); |
| 51 | + wText3.CharacterFormat.Bold = true; |
| 52 | + paragraph3.AppendMath(@"f\left(x\right)={a}_{0}+\sum_{n=1}^{\infty}{\left({a}_{n}\cos{\frac{n\pi{x}}{L}}+{b}_{n}\sin{\frac{n\pi{x}}{L}}\right)}"); |
| 53 | + |
| 54 | + // Return the document as a downloadable file |
| 55 | + return CreateFileResult(document, "CreateEquation.docx"); |
| 56 | + } |
| 57 | + |
| 58 | + // Adds a new equation to an existing Word document |
| 59 | + public IActionResult AddEquation() |
| 60 | + { |
| 61 | + // Open existing document from file |
| 62 | + using (FileStream fileStream = new FileStream("Data\\Input.docx", FileMode.Open, FileAccess.Read)) |
| 63 | + { |
| 64 | + WordDocument document = new WordDocument(fileStream, FormatType.Automatic); |
| 65 | + |
| 66 | + // Find the text "Derivative equation" in the document |
| 67 | + TextSelection selection = document.Find("Derivative equation", false, true); |
| 68 | + |
| 69 | + if (selection != null) |
| 70 | + { |
| 71 | + // Get the paragraph containing the found text |
| 72 | + WParagraph targetParagraph = selection.GetAsOneRange().OwnerParagraph as WParagraph; |
| 73 | + |
| 74 | + if (targetParagraph != null) |
| 75 | + { |
| 76 | + // Create a new paragraph for the math equation |
| 77 | + WParagraph newParagraph = new WParagraph(document); |
| 78 | + |
| 79 | + // Create a math object and set its LaTeX representation |
| 80 | + WMath math = new WMath(document); |
| 81 | + math.MathParagraph.LaTeX = @"\frac{d}{dx}\left(x^n\right)=nx^{n-1}"; |
| 82 | + |
| 83 | + // Add the math object to the new paragraph |
| 84 | + newParagraph.ChildEntities.Add(math); |
| 85 | + |
| 86 | + // Insert the new paragraph after the target paragraph |
| 87 | + int index = document.LastSection.Body.ChildEntities.IndexOf(targetParagraph); |
| 88 | + document.LastSection.Body.ChildEntities.Insert(index + 1, newParagraph); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Return the updated document as a downloadable file |
| 93 | + return CreateFileResult(document, "AddEquation.docx"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Edits an existing equation in a Word document |
| 98 | + public IActionResult EditEquation() |
| 99 | + { |
| 100 | + // Open template document from file |
| 101 | + using (FileStream fileStream = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read)) |
| 102 | + { |
| 103 | + WordDocument document = new WordDocument(fileStream, FormatType.Automatic); |
| 104 | + |
| 105 | + // Find the first math object in the document |
| 106 | + WMath? math = document.FindItemByProperty(EntityType.Math, string.Empty, string.Empty) as WMath; |
| 107 | + |
| 108 | + if (math != null) |
| 109 | + { |
| 110 | + // Get the current LaTeX string and replace 'x' with 'k' |
| 111 | + string laTex = math.MathParagraph.LaTeX; |
| 112 | + math.MathParagraph.LaTeX = laTex.Replace("x", "k"); |
| 113 | + } |
| 114 | + |
| 115 | + // Return the updated document as a downloadable file |
| 116 | + return CreateFileResult(document, "EditEquation.docx"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Helper method to create a FileStreamResult for downloading the document |
| 121 | + private FileStreamResult CreateFileResult(WordDocument document, string fileName) |
| 122 | + { |
| 123 | + MemoryStream outputStream = new MemoryStream(); |
| 124 | + document.Save(outputStream, FormatType.Docx); // Save document to memory stream |
| 125 | + outputStream.Position = 0; // Reset stream position |
| 126 | + return File(outputStream, "application/docx", fileName); // Return file as response |
| 127 | + } |
| 128 | + |
| 129 | + public IActionResult Index() |
| 130 | + { |
| 131 | + return View(); |
| 132 | + } |
| 133 | + |
| 134 | + public IActionResult Privacy() |
| 135 | + { |
| 136 | + return View(); |
| 137 | + } |
| 138 | + |
| 139 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 140 | + public IActionResult Error() |
| 141 | + { |
| 142 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments