Update changes in document comparison API for .NET

GroupDocs.Comparison.Common.ICompareResult compareResult = comparisonDocx.Compare(SourceStrm, TargetStrm, objSettings);
GroupDocs.Comparison.Common.Changes.ChangeInfo[] changes = compareResult.GetChanges();
for(var i=0;i < changes.Length;i++)
{
if(changes[i].Type == GroupDocs.Comparison.Common.TypeChanged.StyleChanged)
{
changes[i].Action = ComparisonAction.Reject;
}
}
compareResult.UpdateChanges(changes);

Using the above code trying to get the StyleChanges and Rejecting them from ChangesInfo[] and Update it to ICompareResult set, but it throws an exception as “Object reference not set to an instance” for some records in ChangeInfo[] list.

Can you help me out here with any solution.

@1132

We cannot reproduce this issue using API version 20.7 and following code:

using (Comparer comparer = new Comparer("source.docx"))
{
	comparer.Add("target.docx");
    comparer.Compare();
    ChangeInfo[] changes = comparer.GetChanges();
    changes[0].ComparisonAction = ComparisonAction.Reject;
    comparer.ApplyChanges(File.Create("result.docx"), new SaveOptions(), new ApplyChangeOptions() { Changes = changes });
}

Have a look at the migration notes and this documentation article.
Please try this code example and let us know if issue persists. You can also download sample project from GitHub.

We are using version 19.3.1.0, can we resolve the issue in 19 version.

@1132

Please share following details and we’ll test this issue with 19.3.1:

  • Sample application or complete code
  • Source and target files

@atirtahir3 Installed the latest Groupdocs.Comparison dll version 20.7.0.0, used the suggested code but getting an exception “The type initializer for ‘Aspose.CAD.Image’ threw an exception.” at comparer.Compare().
In my application we are using Aspose.Word V 16.4.0.0 dll.
Please help me out.

@1132

Please update Aspose.Words to latest version as well and then let us know if issue persists.

Hi, thank you for your quick response. Before compare am modifying the document by removing TOC from it and passing the same document as a source file to the Compare. When applying the changes to compare result an exception is thrown as “Object reference not set to an instance”. So am getting the execption when i remove the TOC, without that am not facing any issue with ApplyChanges() .

@atirtahir3 can you help me out here, as i need to remove the TOC from the source and destination document before compare.

Thank you in advance.

@1132

Please share sample source and target files that are creating this issue. We’ll then look into it.

Attached zip file of document which is used to remove TOC and compare.
Am using below code of Aspose.Word for TOC removing.

   string SourcePath = Server.MapPath("ResultDoc.docx");
  Aspose.Words.Document doc = new Aspose.Words.Document(System.Web.HttpContext.Current.Server.MapPath("SourceDoc.docx"));<a class="attachment" href="/uploads/groupdocs/2076">SourceDocument.zip</a> (21.6 KB)


      ArrayList tocList = new ArrayList();
        foreach (Field field in doc.Range.Fields)
           {
               if (field.Type == FieldType.FieldTOC)
               tocList.Add(field);
           }

        foreach (FieldToc toc in tocList)
        {
            Aspose.Words.Layout.LayoutCollector collector = new Aspose.Words.Layout.LayoutCollector(doc);
            int tocPageNumber = collector.GetStartPageIndex(toc.Start.ParentParagraph);
            Console.WriteLine("Removing TOC at Page# " + tocPageNumber);
            toc.Remove();

            foreach (Aspose.Words.Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
            {
                if (collector.GetStartPageIndex(para) == tocPageNumber && collector.GetNumPagesSpanned(para) == 0)
                    para.Remove();
            }
        }
        doc.Save(SourcePath , Aspose.Words.SaveFormat.Docx);

Thank you @atirtahir3 .SourceDocument.zip (21.6 KB)

@1132

Please share both problematic (exact files that are raising the exception) source and target DOCX files.

@atirtahir3Documents.zip (43.8 KB)

@atirtahir3 Attached the zip file of source and destination documents.

Can we use the temporary license for Groupdocs comparison V 20.7.

@1132

Please have a look at this screencast - Remove TOC and compare.zip (3.8 MB). We are using the source-code that you shared to remove TOC of SourceDoc.docx and then we used the resultant document as a source for comparison purpose.
Can you please prepare a sample (e.g. console) application using that issue could be reproduced? We’ll then further investigate this matter.
NOTE: We cannot reproduce this issue even without applying license.

Yes, you can avail a temporary license here.

@atirtahir3 As you shared a screencast in that result your getting the TOC in output document. But we need to remove it in both Source and destination then need to compare.

@1132

We removed TOC from both source and destination documents. Then compared them, there was no issue. Please share a simple console application to reproduce this issue.

As i mentioned in this comment when ApplyChanges() are done an exception is thrown.

  GroupDocs.Comparison.Result.ChangeInfo[] changes = comparer.GetChanges();
                for (int i = 0; i < changes.Length; i++)
                {
                    if (changes[i].Type == GroupDocs.Comparison.Options.ChangeType.StyleChanged)
                    {
                        changes[i].ComparisonAction = GroupDocs.Comparison.Result.ComparisonAction.Reject;
                    }
                }
 comparer.ApplyChanges(resultPath, new ApplyChangeOptions { Changes = changes });

Can you please test this at your end,
1.Get changes from compare using comparer.GetChanges().
2.Using for loop Reject the StyleChanges and apply the changes to comparer.
3.Then an exception is thrown when ApplyChanges() are done.

@atirtahir3 Instead of console application am sharing you the code which i did after compare is done and when exception is thrown

@1132

Yes, this issue is now reproduced at our end. Hence, we’ve logged it in our internal issue tracking system with ID COMPARISONNET-2423 for further investigation and resolution. As there’s any update, you’ll be notified.

@atirtahir3 When can we expect the release.

@atirtahir3 Can i create the duplicate object of comparer resutl, as i have a requirement where i need to use the original compare result after the ApplyChanges are done.

1.Need to create a duplicate object of compare result.
2.When ApplyChanges() are done to the original(comparer) result the changes should not be reflected in duplicate object.

Is there any possibility to achieve this.

Thank you @atirtahir3 for quick response.

@1132

Please note that these issues are handled on first come first served basis. We’ll notify you as there’s any progress update.

We’re also investigating this scenario with ID COMPARISONNET-2424.