How to Add Comments from Revision Text after Comparing Documents using .NET

I have two word documents, and im comparing those 2 documents and extracting the difference.

I want to add a comment to a particular word.

Say for example : I have DocumentA(Original) and DocumentB(Changed)
In DocumentB I have removed some text. It will strike off when I save the differences DocumentC.

On removed text in DocumentC which will be strikeoff, I want to add comment on particular removed word to say it is removed.

Right now in your examples i can see only comments on paragraphs not on particular word in paragraph.

Can you please provide with a sample that adds comments on revision text.

@manipriya

When you compare Word documents, the final document may contains the revisions. You need to move the cursor to the parent node of revision and insert comment. Following code example shows how to insert comment at the position of revision.

Document doc1 = new Document(MyDir + "1.docx");
Document doc2 = new Document(MyDir + "2.docx");
//Compare documents
doc1.Compare(doc2, "a.w", DateTime.Now);

//get the first revision and insert comment. 
Revision revision = doc1.Revisions[0];

DocumentBuilder builder = new DocumentBuilder(doc1);
builder.MoveTo(revision.ParentNode);
Comment comment = new Comment(doc1, "Aspose.Words", "AW", DateTime.Today);
builder.CurrentParagraph.AppendChild(comment);
comment.Paragraphs.Add(new Paragraph(doc1));
comment.FirstParagraph.Runs.Add(new Run(doc1, "Comment text."));

doc1.Save(MyDir + "21.5.docx");

We have attached the input and output documents with this post for your kind reference. Hope this helps you.
21.5.docx (10.3 KB)
2.docx (11.3 KB)
1.docx (11.1 KB)

We suggest you please read the following articles.

Hi @tahir.manzoor

Thank you for the reply.

Im looking for some thing like adding comment on particular word or sentence modified.

I am uploading 4 documents, where

1.docx - original

2.docx - modified

Adding_Comment_At_End_of_Line.docx - comparing 1.docx and 2.docx
The code that you provided is adding the comment at the end of line.

Expected.docx this is what im looking for. If any possibilty in doing so.
in expected.docx im replacing the previous value(which is in 1.docx) with current value(which is in 2.docx) and adding previous value(which is in 1.docx) as comment.

Please Find the attached documents.

1.docx (11.1 KB)
2.docx (11.8 KB)
Adding_Comment_At_End_of_Line.docx (10.7 KB)
Expected.docx (15.9 KB)

@manipriya

Could you please share the code example (source code without compilation errors) that you are using here for testing? We will investigate the issue and provide you more information on it.

hi @tahir.manzoor
using below code.

public string Compare(string originalDocument, string changedDocument, string comparisonDocument)
{
    var doc1 = new Document(originalDocument);
    var doc2 = new Document(changedDocument);
    if (doc1.Revisions.Count == 0 && doc2.Revisions.Count == 0)
        doc1.Compare(doc2, "a", DateTime.Now);
    var result = new List<ExtractChangeParameters>();
    foreach (Revision revision in doc1.Revisions)
    {
        DocumentBuilder builder = new DocumentBuilder(doc1);
        builder.MoveTo(revision.ParentNode);
        Comment comment = new Comment(doc1, "a", "AW", DateTime.Today);

        var changeType = new ExtractChangeParameters();
        switch (revision.RevisionType)
        {
            case RevisionType.Insertion:
            {
                changeType.ChangeType = "Insertion";
                changeType.TextAfter = revision.Group.Text;
                changeType.TextBefore = revision.ParentNode.GetText();

                builder.CurrentParagraph.AppendChild(comment);
                comment.Paragraphs.Add(new Paragraph(doc1));
                comment.FirstParagraph.Runs.Add(new Run(doc1, revision.Group.Text));
                break;
            }
            case RevisionType.Deletion:
            {
                changeType.ChangeType = "Deletion";
                changeType.TextAfter = revision.Group.Text;
                changeType.TextBefore = revision.ParentNode.GetText();

                builder.CurrentParagraph.AppendChild(comment);
                comment.Paragraphs.Add(new Paragraph(doc1));
                comment.FirstParagraph.Runs.Add(new Run(doc1, revision.Group.Text));
                break;
            }

            result.Add(changeType);
        }
        doc1.Save(comparisonDocument);
    }
}

public class ExtractChanges
{

    public string TextBefore { get; set; }

    public string TextAfter { get; set; }

    public string ChangeType { get; set; }
}

@manipriya

You are getting multiple comments in output document because you are iterating over revisions of document. There are multiple revisions in a paragraph.

We suggest you please iterate over the RevisionGroup collection instead of Revisions collection. Following code example shows how to iterate over revision group and insert comment into document with revision group text. Hope this helps you.

Document doc1 = new Document(MyDir + "input1.docx");
Document doc2 = new Document(MyDir + "input2.docx");

if (doc1.Revisions.Count == 0 && doc2.Revisions.Count == 0)
    doc1.Compare(doc2, "a", DateTime.Now);

DocumentBuilder builder = new DocumentBuilder(doc1);
Revision[] revisions = doc1.Revisions.ToArray();
foreach (RevisionGroup group in doc1.Revisions.Groups)
{
    foreach (Revision revision in revisions)
    {
        if (group == revision.Group)
        {
            Console.WriteLine(revision.Group.Text);
            ///// write code to insert comment : start
            ///
            builder.MoveTo(revision.ParentNode);
            Comment comment = new Comment(doc1, "comment", "AW", DateTime.Today);
            //Console.WriteLine(revision.Group.Text);
            //var changeType = new ExtractChangeParameters();
            switch (revision.RevisionType)
            {
                case RevisionType.Insertion:
                    {
                        builder.CurrentParagraph.AppendChild(comment);
                        comment.Paragraphs.Add(new Paragraph(doc1));
                        comment.FirstParagraph.Runs.Add(new Run(doc1, group.Text));
                        break;
                    }
                case RevisionType.Deletion:
                    {
                        builder.CurrentParagraph.AppendChild(comment);
                        comment.Paragraphs.Add(new Paragraph(doc1));
                        comment.FirstParagraph.Runs.Add(new Run(doc1, group.Text));
                        break;
                    }
            }
            ///// write code to insert comment : end
            break;
        }
    }
} 
doc1.Save(MyDir + "output.docx");

Hi @tahir.manzoor

Can i get something like the below image?

image.png (73.7 KB)

How are you is removed

so it can point the whole sentence and add comment on second line instead on first line?

and also can you please provide sample code for pdf comparision with comments for it as well?

@manipriya

You can add the comments into document with revision text as suggested in this thread. Could you please share some more detail about this query along with input and expected output documents?

If you want to compare PDF files, you can do it using Document.Compare method. Aspose.Words for .NET supports the import of PDF.

Please share some detail about this query as well. We will then provide you more information on it.

Hi @tahir.manzoor

Can you share a sample code for pdf comparision like below.

doc1.Compare(doc2, "a", DateTime.Now);

foreach (Revision revision in doc1.Revisions){
    switch (revision.RevisionType){
        case RevisionType.Insertion:
            added++;
            break;
        case RevisionType.Deletion:
            deleted++;
            break;
    }

    Console.WriteLine(revision.RevisionType + ": " + revision.ParentNode);
}
doc1.Save(comparisonDocument);

This should add the revision comments similar way we are doing for word document.
It should have the following property for showing comments which is not supported in current version

document.LayoutOptions.RevisionOptions.ShowRevisionBalloons =true

highligted property is not part of current version…
in current version there are properties below

doc1.Save(comparisonDocument); // this is docx
Document doc = new Document(comparisonDocument); //opening docx
doc.LayoutOptions.RevisionOptions.ShowInBalloons=  Aspose.Words.Layout.ShowInBalloons.FormatAndDelete;
doc.LayoutOptions.RevisionOptions.ShowOriginalRevision = true;
doc.LayoutOptions.RevisionOptions.ShowRevisionMarks = true;
doc.LayoutOptions.RevisionOptions.ShowRevisionBars = true;
doc1.Save(comparisonDocument); //this would be my saved pdf format.

applying above properties makes my pdf look worst.
please suggest solution for pdf comparsion and add revision changes in comments.

@manipriya

You can import PDF files into Aspose.Words’ DOM and compare the PDF files using Document.Compare method. However, the output may not be good. So, we suggest you please use Aspose.PDF for comparing PDF files.

If you want to use Aspose.Words for comparing PDF files, please attach the following resources here for testing:

  • Your input PDF documents.
  • Please attach the output PDF file that shows the undesired behavior.
  • Please attach the expected output PDF file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi @tahir.manzoor

I am sharing samples for pdfs to compare the changes.

So here I would like to get the differences like inserted and deleted text(revisions) in comments section like how it is done in word document comparison.

when i use compare method of PDF i didnt find any changes in newly saved pdf.

code i have already shared above for pdf comparison.

2pdfs_to_compare.zip (88.6 KB)

@manipriya

Please use LayoutOptions.RevisionOptions property as shown below to render the revisions in output PDF.

Document pdf1 = new Document(MyDir + "pdf-1.pdf");
Document pdf2 = new Document(MyDir + "pdf-2.pdf");

pdf1.Compare(pdf2, "aw", DateTime.Now);

pdf1.LayoutOptions.RevisionOptions.ShowInBalloons = ShowInBalloons.FormatAndDelete;
pdf1.Save(MyDir + "output.pdf");