I’m working with Word documents that have been edited by multiple authors. Some of the changes were made with Track Changes enabled, and others were made without it. I want to compare two versions of the document using Aspose.Words, and for each line that contains changes, I want to:
- Show the author name in the suggestion popup if the change was made with Track Changes enabled.
- Show “Unknown” as the author if the change was made with Track Changes disabled.
I’ve written code using the Aspose.Words.Compare
method to compare the original and revised documents and extract revision information. However, when I run the comparison, I’m only seeing one author for all the tracked changes, even though multiple users have made edits.
Here’s a snippet of how I’m performing the comparison and accessing revision authors:
private async Task<Stream> GetComparedDocumentStreamAsync(ImportRevisionsCommand request, StudyDocument studyDocument)
{
var originalDocumentStream = await GetOriginalDocumentStreamAsync(studyDocument);
var importedDocumentStream = request.File.OpenReadStream();
if (importedDocumentStream == null || importedDocumentStream.Length == 0)
{
_logger.LogError("----- Imported document stream is null or empty for Document ID: {DocumentId}", request.DocumentId);
throw new ModelValidationException(ErrorConstants.IMPORTED_STREM_ERROR);
}
var authors = GetTrackedRevisionAuthors(importedDocumentStream);
string authorForComparison = (authors[1] ?? "Unknown").Replace(" ", "");
//string authorForComparison = authors[1] ?? "Unknown";
// Rewind stream after reading revisions
//importedDocumentStream.Position = 0;
var compareResponseStream = await _asposeService.Compare(originalDocumentStream, importedDocumentStream, authorForComparison);
return compareResponseStream;
}
private List<string> GetTrackedRevisionAuthors(Stream documentStream)
{
documentStream.Position = 0;
Aspose.Words.Document doc = new Aspose.Words.Document(documentStream);
var authors = new HashSet<string>();
foreach (Revision revision in doc.Revisions)
{
string author = string.IsNullOrWhiteSpace(revision.Author) ? "Unknown" : revision.Author;
authors.Add(author);
}
return authors.ToList();
}
This is the compare method we have written:
public async Task<Stream> Compare(Stream document1, Stream document2, string userName)
{
Document doc1 = new Document(document1);
Document doc2 = new Document(document2);
var response = await Compare(doc1, doc2, userName);
return new MemoryStream(response);
}
public async Task<byte[]> Compare(Document document1, Document document2, string userName)
{
Aspose.Words.Comparing.CompareOptions compareOptions = new Aspose.Words.Comparing.CompareOptions
{
IgnoreFormatting = false,
IgnoreCaseChanges = false,
IgnoreTables = false,
IgnoreFields = false,
IgnoreFootnotes = false,
IgnoreComments = false,
IgnoreTextboxes = false,
IgnoreHeadersAndFooters = false,
Granularity = Granularity.CharLevel,
CompareMoves = true
};
document1.AcceptAllRevisions();
document2.AcceptAllRevisions();
document1.TrackRevisions = true;
// Compare the documents and track revisions
document1.Compare(document2, userName, DateTime.Now, compareOptions);
using (MemoryStream memoryStream = new MemoryStream())
{
// Save the compared document to memory stream
document1.Save(memoryStream, SaveFormat.Docx);
memoryStream.Position = 0;
return await Task.FromResult<byte[]>(memoryStream.ToArray());
}
}
This is the metadata file code which we are using the Compare method:
public void Compare(Document document, string author, DateTime dateTime, CompareOptions options);
Please provide the solution for this.