Skip to content

Commit c687973

Browse files
Bold Create Agentsuriyaprakasamr
andcommitted
Add Side-by-Side-Comparison sample for ASP.NET Core DOCX Editor
New sample comparing original and revised Word documents using Syncfusion DocIO with side-by-side Document Editors, tracked revisions view, and download of the compared document. Co-authored-by: suriyaprakasamr <302142999+suriyaprakasamr@users.noreply.github.com>
1 parent f5f781e commit c687973

26 files changed

Lines changed: 1111 additions & 0 deletions

‎.gitignore‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# Build results
5+
[Dd]ebug/
6+
[Rr]elease/
7+
x64/
8+
x86/
9+
[Bb]in/
10+
[Oo]bj/
11+
[Ll]og/
12+
[Ll]ogs/
13+
14+
# .NET build artifacts
15+
*.dll
16+
*.exe
17+
*.pdb
18+
*.cache
19+
20+
# Visual Studio / Rider / VS Code metadata
21+
.vs/
22+
.vscode/
23+
.idea/
24+
*.suo
25+
*.user
26+
*.userosscache
27+
*.sln.docstates
28+
29+
# NuGet
30+
*.nupkg
31+
packages/
32+
**/packages/*
33+
!**/packages/build/
34+
35+
# Node
36+
node_modules/
37+
package-lock.json
38+
npm-debug.log*
39+
yarn-error.log*
40+
41+
# Publish output
42+
[Pp]ublishOutput/
43+
publish/
44+
45+
# Runtime data
46+
*.log
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Newtonsoft.Json;
3+
using WDocument = Syncfusion.DocIO.DLS.WordDocument;
4+
using WFormatType = Syncfusion.DocIO.FormatType;
5+
6+
namespace DocumentEditor.Controllers
7+
{
8+
[ApiController]
9+
[Route("api/[controller]")]
10+
public class DocumentEditorController : Controller
11+
{
12+
/// <summary>
13+
/// Imports a DOCX file and returns its SFDT (Syncfusion Document Text) JSON content.
14+
/// </summary>
15+
[Route("Import")]
16+
public string Import(IFormCollection data)
17+
{
18+
if (data.Files.Count == 0)
19+
{
20+
return string.Empty;
21+
}
22+
23+
IFormFile file = data.Files[0];
24+
int index = file.FileName.LastIndexOf('.');
25+
string extension = index > -1 && index < file.FileName.Length - 1 ?
26+
file.FileName.Substring(index) : ".docx";
27+
28+
if (extension == ".sfdt")
29+
{
30+
using (var streamReader = new StreamReader(file.OpenReadStream()))
31+
{
32+
return streamReader.ReadToEnd();
33+
}
34+
}
35+
36+
Stream stream = new MemoryStream();
37+
file.CopyTo(stream);
38+
stream.Position = 0;
39+
40+
// Convert the DOCX stream to SFDT DOM.
41+
Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx);
42+
string json = JsonConvert.SerializeObject(document);
43+
document.Dispose();
44+
return json;
45+
}
46+
47+
/// <summary>
48+
/// Compares an original and a revised DOCX document and returns the compared document as SFDT content with tracked changes.
49+
/// </summary>
50+
[Route("CompareDocuments")]
51+
public string CompareDocuments(IFormCollection data)
52+
{
53+
if (data.Files.Count < 2)
54+
{
55+
return string.Empty;
56+
}
57+
58+
IFormFile? originalFile = data.Files.FirstOrDefault(f => f.Name == "originalFile");
59+
IFormFile? revisedFile = data.Files.FirstOrDefault(f => f.Name == "revisedFile");
60+
if (originalFile == null || revisedFile == null)
61+
{
62+
return string.Empty;
63+
}
64+
65+
string author = data.TryGetValue("author", out var authorValue) ? authorValue.ToString() : "Author";
66+
string dateTime = data.TryGetValue("dateTime", out var dateValue) ? dateValue.ToString() : DateTime.UtcNow.ToString("o");
67+
68+
var originalStream = new MemoryStream();
69+
originalFile.CopyTo(originalStream);
70+
originalStream.Position = 0;
71+
72+
var revisedStream = new MemoryStream();
73+
revisedFile.CopyTo(revisedStream);
74+
revisedStream.Position = 0;
75+
76+
// Load the original and revised documents using the Syncfusion .NET Word (DocIO) library.
77+
WDocument originalDocument = new WDocument();
78+
originalDocument.Settings.SkipIncrementalSaveValidation = true;
79+
originalDocument.Open(originalStream, WFormatType.Automatic);
80+
81+
WDocument revisedDocument = new WDocument();
82+
revisedDocument.Settings.SkipIncrementalSaveValidation = true;
83+
revisedDocument.Open(revisedStream, WFormatType.Automatic);
84+
85+
// Compare the revised document against the original document.
86+
originalDocument.Compare(revisedDocument, author, Convert.ToDateTime(dateTime));
87+
88+
// Convert the compared Word document to SFDT content.
89+
Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(originalDocument);
90+
string json = JsonConvert.SerializeObject(document);
91+
document.Dispose();
92+
originalDocument.Dispose();
93+
revisedDocument.Dispose();
94+
return json;
95+
}
96+
}
97+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="35.1.40" />
11+
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="35.1.40" />
12+
<PackageReference Include="Syncfusion.EJ2.WordEditor.AspNet.Core" Version="35.1.40" />
13+
<PackageReference Include="Syncfusion.Licensing" Version="35.1.40" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.RazorPages;
3+
using System.Diagnostics;
4+
5+
namespace DocumentEditor.Pages
6+
{
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string? RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
public void OnGet()
16+
{
17+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
18+
}
19+
}
20+
21+
}

0 commit comments

Comments
 (0)