-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathHomeController.cs
More file actions
145 lines (121 loc) · 6.33 KB
/
Copy pathHomeController.cs
File metadata and controls
145 lines (121 loc) · 6.33 KB
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
using System.Diagnostics;
using LaTeXEquation.Models;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
namespace LaTeXEquation.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
// Creates a new Word document with mathematical equations
public IActionResult CreateEquation()
{
// Initialize a new Word document and ensure minimal structure
WordDocument document = new WordDocument();
document.EnsureMinimal();
// Add a main title paragraph
IWParagraph mainTitle = document.LastSection.AddParagraph();
IWTextRange titleText = mainTitle.AppendText("Mathematical Equations");
titleText.CharacterFormat.Bold = true; // Make title bold
titleText.CharacterFormat.FontSize = 18; // Set font size
mainTitle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; // Center align title
mainTitle.ParagraphFormat.AfterSpacing = 12f; // Add spacing after title
// Add paragraph for Area of Circle equation
IWParagraph paragraph1 = document.LastSection.AddParagraph();
paragraph1.ParagraphFormat.BeforeSpacing = 8f; // Add spacing before paragraph
IWTextRange wText1 = paragraph1.AppendText("Area of Circle ");
wText1.CharacterFormat.Bold = true; // Make label bold
paragraph1.AppendMath(@"A = \pi r^2"); // Insert LaTeX math equation
// Add paragraph for Quadratic Formula equation
IWParagraph paragraph2 = document.LastSection.AddParagraph();
paragraph2.ParagraphFormat.BeforeSpacing = 8f;
IWTextRange wText2 = paragraph2.AppendText("Quadratic Formula ");
wText2.CharacterFormat.Bold = true;
paragraph2.AppendMath(@"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}");
// Add paragraph for Fourier Series equation
IWParagraph paragraph3 = document.LastSection.AddParagraph();
paragraph3.ParagraphFormat.BeforeSpacing = 8f;
IWTextRange wText3 = paragraph3.AppendText("Fourier Series ");
wText3.CharacterFormat.Bold = true;
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)}");
// Return the document as a downloadable file
return CreateFileResult(document, "CreateEquation.docx");
}
// Adds a new equation to an existing Word document
public IActionResult AddEquation()
{
// Open existing document from file
using (FileStream fileStream = new FileStream("Data\\Input.docx", FileMode.Open, FileAccess.Read))
{
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
// Find the text "Derivative equation" in the document
TextSelection selection = document.Find("Derivative equation", false, true);
if (selection != null)
{
// Get the paragraph containing the found text
WParagraph targetParagraph = selection.GetAsOneRange().OwnerParagraph as WParagraph;
if (targetParagraph != null)
{
// Create a new paragraph for the math equation
WParagraph newParagraph = new WParagraph(document);
// Create a math object and set its LaTeX representation
WMath math = new WMath(document);
math.MathParagraph.LaTeX = @"\frac{d}{dx}\left(x^n\right)=nx^{n-1}";
// Add the math object to the new paragraph
newParagraph.ChildEntities.Add(math);
// Insert the new paragraph after the target paragraph
int index = document.LastSection.Body.ChildEntities.IndexOf(targetParagraph);
document.LastSection.Body.ChildEntities.Insert(index + 1, newParagraph);
}
}
// Return the updated document as a downloadable file
return CreateFileResult(document, "AddEquation.docx");
}
}
// Edits an existing equation in a Word document
public IActionResult EditEquation()
{
// Open template document from file
using (FileStream fileStream = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read))
{
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
// Find the first math object in the document
WMath? math = document.FindItemByProperty(EntityType.Math, string.Empty, string.Empty) as WMath;
if (math != null)
{
// Get the current LaTeX string and replace 'x' with 'k'
string laTex = math.MathParagraph.LaTeX;
math.MathParagraph.LaTeX = laTex.Replace("x", "k");
}
// Return the updated document as a downloadable file
return CreateFileResult(document, "EditEquation.docx");
}
}
// Helper method to create a FileStreamResult for downloading the document
private FileStreamResult CreateFileResult(WordDocument document, string fileName)
{
MemoryStream outputStream = new MemoryStream();
document.Save(outputStream, FormatType.Docx); // Save document to memory stream
outputStream.Position = 0; // Reset stream position
return File(outputStream, "application/docx", fileName); // Return file as response
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}