Issue with the import revision order in word doc

When I use aspose.compare() to compare the original Word document with the edited version, the results don’t maintain the order of the edits as expected. Is there a way to preserve the editing sequence and make the output look better?

example:- i have a sentence “hello there how are you doing?”
i first insert world after hello then remove all the text after that
expected - hello world t̶h̶e̶r̶e̶ ̶h̶o̶w̶ ̶a̶r̶e̶ ̶y̶o̶u̶ ̶d̶o̶i̶n̶g̶
output which i am getting - hello t̶h̶e̶r̶e̶ ̶h̶o̶w̶ ̶a̶r̶e̶ ̶y̶o̶u̶ ̶d̶o̶i̶n̶g̶world

@mgowthamm

To preserve the order of edits when comparing documents using Aspose.Words, you can utilize the compare method effectively. If the output does not reflect the expected sequence of changes, follow these steps to resolve the issue:

  1. Review the Comparison Logic: Ensure that you are comparing the documents in the correct order. The first document should be the original, and the second should be the edited version.

  2. Use CompareOptions: Customize the comparison settings using the CompareOptions class. This allows you to specify how changes are tracked and displayed. For example, you can set options to ignore certain types of changes or to include formatting changes.

  3. Check for Overlapping Edits: If multiple edits occur in close proximity, the comparison might not display them in the expected order. Simplify the edits or break them into smaller changes to maintain the order.

  4. Save the Compared Document: After performing the comparison, save the document to review the changes visually. This will help you understand how the edits are represented.

Here’s a minimal code example to illustrate how to compare two documents:

import com.aspose.words.*;

public class DocumentComparison {
    public static void main(String[] args) throws Exception {
        Document originalDoc = new Document("path/to/original.docx");
        Document editedDoc = new Document("path/to/edited.docx");

        // Compare documents
        originalDoc.compare(editedDoc, "Editor", new Date());

        // Save the compared document
        originalDoc.save("path/to/comparedDocument.docx");
    }
}

By following these steps and using the provided code snippet, you will achieve a more accurate representation of the editing sequence in your compared document. If you have any further questions or need additional assistance, please let me know!

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<CompareResponseDto> Compare(Document document1, Document document2, CompareMode compareMode = CompareMode.Preview)
{
    Aspose.Words.Comparing.CompareOptions compareOptions = new Aspose.Words.Comparing.CompareOptions
    {
        IgnoreFormatting = true,
        IgnoreCaseChanges = false,
        IgnoreTables = false,
        IgnoreFields = false,
        IgnoreFootnotes = false,
        IgnoreComments = false,
        IgnoreTextboxes = false,
        IgnoreHeadersAndFooters = false,
        Granularity = Granularity.WordLevel,
        CompareMoves = true
    };

    document1.Compare(document2, "Comparison", DateTime.Now, compareOptions);

    if (document1.HasRevisions)
    {
        RevisionOptions revisionOptions = document1.LayoutOptions.RevisionOptions;                
        revisionOptions.InsertedTextColor = RevisionColor.Green;
        revisionOptions.DeletedTextColor = RevisionColor.Red;

        HtmlFixedSaveOptions opt = new HtmlFixedSaveOptions();
        opt.PrettyFormat = true;
        opt.ExportEmbeddedCss = true;
        opt.ExportEmbeddedFonts = true;
        opt.ExportEmbeddedImages = true;
        opt.ExportEmbeddedSvg = true;
        opt.ShowPageBorder = false;

        if(compareMode == CompareMode.Download)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                document1.Save(memoryStream, SaveFormat.Docx);
                memoryStream.Position = 0;
                return await Task.FromResult<CompareResponseDto>(new CompareResponseDto { CompareMode=compareMode, Document = memoryStream.ToArray()});
            }
        }
        else {
            using (MemoryStream ms = new MemoryStream())
            {
                document1.Save(ms, opt);
                ms.Position = 0;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(ms);
                return await Task.FromResult<CompareResponseDto>(new CompareResponseDto { CompareMode = compareMode, DocumentString = xmlDoc.InnerXml });
            }
        }
    }
    else
    {
        return await Task.FromResult<CompareResponseDto>(new CompareResponseDto { CompareMode=compareMode, Message=Constants.NO_CHANGE_IN_THE_CONTENT});
    }
   
}

this is the compare logic as you can see the first document is original and the second one is edited

@mgowthamm Could you please attach your problematic input, output and expected output documents in DOCX format here for our reference? We will check the issue and provide you more information.

edited.docx (16.5 KB)

original.docx (23.2 KB)

using original and edited document for comparison
ill be attaching expected and output coming from aspose.

output from aspose.docx (19.0 KB)

expected output.docx (25.1 KB)

@mgowthamm Thank you for additional information. As I can see Aspose.Words result corresponds MS Word comparison result:

Document v1 = new Document(@"C:\Temp\original.docx");
Document v2 = new Document(@"C:\Temp\edited.docx");
CompareOptions opt = new CompareOptions();
opt.Target = ComparisonTargetType.New;
v1.Compare(v2, "AW", DateTime.Now, opt);
v1.Save(@"C:\Temp\out.docx");

Aspose.Words: out.docx (20.4 KB)
MS Word: ms.docx (24.5 KB)

Could you please explain how you get the expected output you have attached above?

I obtained the expected output using Microsoft Word by navigating to the Review tab, selecting the Compare option, and then choosing Combine to merge the original and edited documents.

@mgowthamm Thank you for additional information. Unfortunately, Aspose.Words does not support Combine feature (MS Word Review Tab > Compare > Combine). This feature request is logged as WORDSNET-16956 and is not yet scheduled for development.

@alexey.noskov
is there any other way to achive combine feature in Aspose.words?

@mgowthamm No, unfortunately, there is no other way to achieve this using Aspose.Words. Currently only compare feature is supported.

@alexey.noskov
Thank you very much for your response and for letting me know about the status. I really appreciate your help!

1 Like