Merge Comments from 2 or more document.
We have a requirement to combine only the comments from 2 or more documents ( Doc V1-1.docx, Doc V1-2.docx, Doc V1-3.docx).
Doc V1-1.docx, Doc V1-2.docx, Doc V1-3.docx are same document except for comments.
Can you please let us know how we can achieve this?
We used compare function - however we are not able to get the name of user who had provided the comments in the compared document.
Attached are the sample files which i am trying to merge comments.
Please help with the sample code.
@isha.r You can use Comment.Author property to get author of the comment. I have used the following simple code and can successful get comments in the compare result:
Document doc1 = new Document(@"C:\Temp\Sample FORM V1-1.docx");
Document doc2 = new Document(@"C:\Temp\Sample FORM V1-2.docx");
doc1.Compare(doc2, "test", DateTime.Now);
// Get authros and text of the comments
foreach (Comment c in doc1.GetChildNodes(NodeType.Comment, true))
Console.WriteLine($"{c.Author}: {c.ToString(SaveFormat.Text).Trim()}");
doc1.Save(@"C:\Temp\out.docx");
@isha.r Unfortunately, using Compare method you can compare only two documents. But your can accept revisions in the comparison result document and then compare the resulting document with another document.
Document doc1 = new Document(@"C:\Temp\Sample FORM V1-1.docx");
Document doc2 = new Document(@"C:\Temp\Sample FORM V1-2.docx");
Document doc3 = new Document(@"C:\Temp\Sample FORM V1-3.docx");
doc1.Compare(doc2, "test", DateTime.Now);
doc1.AcceptAllRevisions();
doc1.Compare(doc3, "test", DateTime.Now);
// .......
[HttpPost]
public IActionResult MergeComments(List<IFormFile> files)
{
if (files.Count < 2)
{
ViewBag.Error = "Please upload at least two Word documents.";
return View("Index");
}
try
{
var uploadsFolder = Path.Combine(_environment.WebRootPath, "uploads");
Directory.CreateDirectory(uploadsFolder); // Create the "uploads" directory if it doesn't exist
var documentPaths = new List<string>();
// Save the uploaded files to the server and collect their paths
foreach (var file in files)
{
var filePath = Path.Combine(uploadsFolder, file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}
documentPaths.Add(filePath);
}
// Create copies of the original documents to perform merging and comparison
var documentCopies = new List<Document>();
foreach (var documentPath in documentPaths)
{
var doc = new Document(documentPath);
var copyPath = Path.Combine(uploadsFolder, "copy_" + Path.GetFileName(documentPath));
// Save a copy of the document
doc.Save(copyPath);
documentCopies.Add(new Document(copyPath));
// Accept all revisions in the original document
doc.AcceptAllRevisions();
}
// Merge comments from multiple documents
Document mergedDocument = null;
foreach (var doc in documentCopies)
{
if (mergedDocument == null)
{
mergedDocument = doc;
}
else
{
// Compare and merge the current document into the mergedDocument
mergedDocument.Compare(doc, "test", DateTime.Now);
}
}
// Get authors and text of the comments from the merged document
var comments = new List<string>();
foreach (Comment c in mergedDocument.GetChildNodes(NodeType.Comment, true))
{
comments.Add($"{c.Author}: {c.ToString(SaveFormat.Text).Trim()}");
}
// Save the merged document with comments
var outputPath = Path.Combine(_environment.WebRootPath, "merged", "out.docx");
mergedDocument.Save(outputPath);
// Provide the merged document for download
var mergedFileName = "MergedDocument.docx";
return File(System.IO.File.ReadAllBytes(outputPath), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mergedFileName);
}
catch (Exception ex)
{
ViewBag.Error = $"An error occurred: {ex.Message}";
return View("Index");
}
}
but i am getting error:‘Compared documents must not have revisions.’
Please help to modify the code to achieve it
Modified my code but still same error :Please help me modify my code where i am going wrong
Modified code
[HttpPost]
public IActionResult MergeComments(List<IFormFile> files)
{
if (files.Count < 2)
{
ViewBag.Error = "Please upload at least two Word documents.";
return View("Index");
}
try
{
var uploadsFolder = Path.Combine(_environment.WebRootPath, "uploads");
Directory.CreateDirectory(uploadsFolder); // Create the "uploads" directory if it doesn't exist
var documentPaths = new List<string>();
// Save the uploaded files to the server and collect their paths
foreach (var file in files)
{
var filePath = Path.Combine(uploadsFolder, file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fileStream);
}
documentPaths.Add(filePath);
}
// Create copies of the original documents to perform merging and comparison
var documentCopies = new List<Document>();
foreach (var documentPath in documentPaths)
{
var doc = new Document(documentPath);
var copyPath = Path.Combine(uploadsFolder, "copy_" + Path.GetFileName(documentPath));
// Save a copy of the document
doc.Save(copyPath);
documentCopies.Add(new Document(copyPath));
// Accept all revisions in the original document
doc.AcceptAllRevisions();
}
// Merge comments from multiple documents
Document mergedDocument = null;
foreach (var doc in documentCopies)
{
if (mergedDocument == null)
{
mergedDocument = doc;
}
else
{
// Compare and merge the current document into the mergedDocument
mergedDocument.AcceptAllRevisions();
mergedDocument.Compare(doc, "test", DateTime.Now);
}
}
// Get authors and text of the comments from the merged document
var comments = new List<string>();
foreach (Comment c in mergedDocument.GetChildNodes(NodeType.Comment, true))
{
comments.Add($"{c.Author}: {c.ToString(SaveFormat.Text).Trim()}");
}
// Save the merged document with comments
var outputPath = Path.Combine(_environment.WebRootPath, "merged", "out.docx");
mergedDocument.Save(outputPath);
// Provide the merged document for download
var mergedFileName = "MergedDocument.docx";
return File(System.IO.File.ReadAllBytes(outputPath), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mergedFileName);
}
catch (Exception ex)
{
ViewBag.Error = $"An error occurred: {ex.Message}";
return View("Index");
}
}