Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare Word Documents in ASP.NET Core
<!--drag and drop file plugin-->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/js/fileinput.min.js"></script>
<!--end of drag and drop-->
[HttpPost]
public FileResult UploadFiles(List<IFormFile> files, string outputFormat)
{
if(files.Count()==0)
{
return null;
}
string fileName = "result.docx";
// Upload files
var file1 = Path.Combine("wwwroot/uploads", files[0].FileName);
var file2 = Path.Combine("wwwroot/uploads", files[1].FileName);
using (var stream = new FileStream(file1, FileMode.Create))
{
files[0].CopyTo(stream);
}
using (var stream = new FileStream(file2, FileMode.Create))
{
files[1].CopyTo(stream);
}
// Load Word documents
Document doc1 = new Document(file1);
Document doc2 = new Document(file2);
// Set comparison features
CompareOptions compareOptions = new CompareOptions();
compareOptions.IgnoreFormatting = true;
compareOptions.IgnoreCaseChanges = true;
compareOptions.IgnoreComments = true;
compareOptions.IgnoreTables = true;
compareOptions.IgnoreFields = true;
compareOptions.IgnoreFootnotes = true;
compareOptions.IgnoreTextboxes = true;
compareOptions.IgnoreHeadersAndFooters = true;
compareOptions.Target = ComparisonTargetType.New;
var outputStream = new MemoryStream();
// Compare Word documents
doc1.Compare(doc2, "John Doe", DateTime.Now, compareOptions);
if (outputFormat == "DOCX")
{
// For comparison result in DOCX
doc1.Save(outputStream, SaveFormat.Docx);
outputStream.Position = 0;
// Return generated Word file
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Rtf, fileName);
}
else
{
// For comparison result in PDF
fileName = "result.pdf";
doc1.Save(outputStream, SaveFormat.Pdf);
outputStream.Position = 0;
// Return generated PDF file
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
}
}
@{
ViewData["Title"] = "MS Word Comparison in ASP.NET";
}
<h2 class="text-info">Compare Two Word DOC/DOCX Documents</h2>
<p class="text-info">Compare the contents of MS Word documents and get the results in DOCX or PDF format.</p>
<br />
<form asp-controller="Home" asp-action="UploadFiles" method="post" class="form-inline dropzone" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6" align="center">
<div class="form-group">
<input type="file" id="input-id" name="files" accept=".doc, .docx" class="form-control file" data-preview-file-type="text" />
</div>
</div>
<div class="col-md-6" align="center">
<div class="form-group">
<input type="file" id="input-id2" name="files" accept=".doc, .docx" class="form-control file" />
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-12" align="center">
<div class="form-group">
<p class="text-info">
<strong>Output format:</strong>
<select name="outputFormat" class="form-control">
<option value="DOCX">DOCX</option>
<option value="PDF">PDF</option>
</select>
</p>
</div>
<div class="col-md-12" align="center">
<div class="form-group">
<button type="submit" class="form-control btn btn-success">Compare and Download</button>
</div>
</div>
</div>
</div>
</form>
<script>
// Drag and drop plugin options
$("#input-id").fileinput({ 'showUpload': false, 'previewFileType': 'any', 'showClose': false });
$("#input-id2").fileinput({ 'showUpload': false, 'previewFileType': 'any', 'showClose': false });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment