Skip to content

Commit 1813e82

Browse files
committed
Added the sample for latex video .
1 parent fa2f65a commit 1813e82

File tree

78 files changed

+74927
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+74927
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}
15.2 KB
Binary file not shown.
1.86 MB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace LaTeXEquation.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

Videos/LaTeXEquation/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LaTeXEquation
2+
{
3+
public class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddControllersWithViews();
11+
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (!app.Environment.IsDevelopment())
16+
{
17+
app.UseExceptionHandler("/Home/Error");
18+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
19+
app.UseHsts();
20+
}
21+
22+
app.UseHttpsRedirection();
23+
app.UseStaticFiles();
24+
25+
app.UseRouting();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllerRoute(
30+
name: "default",
31+
pattern: "{controller=Home}/{action=Index}/{id?}");
32+
33+
app.Run();
34+
}
35+
}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:47672",
8+
"sslPort": 44327
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5295",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7277;http://localhost:5295",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}

Videos/LaTeXEquation/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# How to Work with Latex Equations in a Word Document Using the .NET Word Library
2+
3+
This repository provides an example of how to work with LaTeX equations in a Word document using the **Syncfusion .NET Word Library (DocIO)**. It demonstrates how to create, add, and edit LaTeX equations in Word documents.
4+
5+
## Process behind Field Integration
6+
7+
This sample shows how LaTeX syntax can be used to insert professional mathematical equations into Word documents. LaTeX is widely used in scientific and academic fields for typesetting complex formulas.
8+
9+
Using the Syncfusion DocIO library, you can:
10+
11+
- Create equations like area of a circle, quadratic formula, and Fourier series in a new Word document.
12+
- Add equations dynamically to an existing document at specific locations.
13+
- Edit equations by modifying LaTeX strings to update variables.
14+
15+
## Steps to use the sample
16+
17+
1. Open the ASP.NET Core application where the Syncfusion DocIO package is installed.
18+
2. Run the application and click the following buttons:
19+
- **CreateEquation**: Creates a Word document with multiple LaTeX equations.
20+
- **AddEquation**: Inserts a new LaTeX equation into an existing document
21+
- **EditEquation**: Updates an existing LaTeX equation in the document.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div>
6+
<h2 style="margin-bottom: 20px; font-size: 22px;">Working with LaTeX Equations</h2>
7+
<div>
8+
<button style="width: 200px; margin-bottom: 20px; height: 40px; display: block; font-size: 18px;"
9+
onclick="location.href='@Url.Action("CreateEquation", "Home")'">
10+
Create Equation
11+
</button>
12+
<button style="width: 200px; margin-bottom: 20px; height: 40px; display: block; font-size: 18px;"
13+
onclick="location.href='@Url.Action("AddEquation", "Home")'">
14+
Add Equation
15+
</button>
16+
<button style="width: 200px; margin-bottom: 20px; height: 40px; display: block; font-size: 18px;"
17+
onclick="location.href='@Url.Action("EditEquation", "Home")'">
18+
Edit Equation
19+
</button>
20+
</div>
21+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>

0 commit comments

Comments
 (0)