Error when comparing two doc file

Found this error on specific file, idk whats wrong with the docx file


System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

at System.Collections.Generic.List`1.System.Collections.IList.get_Item
at Syncfusion.DocIO.DLS.ParagraphItemCollection.get_Item
at Syncfusion.DocIO.DLS.WParagraph.GetNextWord
at Syncfusion.DocIO.DLS.WParagraph.Compare
at Syncfusion.DocIO.DLS.WTextBody.Compare
at Syncfusion.DocIO.DLS.WordDocument.WordCompare
at Syncfusion.DocIO.DLS.WordDocument.Compare
at Syncfusion.DocIO.DLS.WParagraph.CompareFieldResult
at Syncfusion.DocIO.DLS.WParagraph.CompareField
at Syncfusion.DocIO.DLS.WParagraph.Compare
at Syncfusion.DocIO.DLS.WTextBody.Compare
at Syncfusion.DocIO.DLS.WSection.CompareHeaderFooter
at Syncfusion.DocIO.DLS.WordDocument.WordCompare
at Syncfusion.DocIO.DLS.WordDocument.Compare
at WebApplication1.Controllers.DocumentEditorController.CompareDocuments ( /app/Controllers/DocumentEditorController.cs:272 )
at WebApplication1.Controllers.DocumentEditorController.Compare ( /app/Controllers/DocumentEditorController.cs:253 )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute

4 Replies

AP Adithya Putra November 7, 2024 08:03 AM UTC

Check the docx files


Attachment: Archive_3_ba71690c.zip


DS Dharanya Sakthivel Syncfusion Team November 8, 2024 01:28 PM UTC

Hi Adithya,

We tried to reproduce the exception you are facing by comparing the provided documents on our end but we did not encounter any exception, and the comparison document was generated successfully. Kindly refer to the attached sample which we tried at our end.

If you are still facing the issue, kindly provide the following details:

  1. Confirm if the problem occurs when running the attached sample on your side.
  2. Share the complete code snippet or a sample that reproduces the issue.

Based on these details, we will proceed further to replicate the problem and then share more details about it.

Regards,
Dharanya.


Attachment: CompareWorddocuments_36581807.zip


AS Adrian Scott November 20, 2024 01:16 PM UTC

The error you’re encountering, specifically System.ArgumentOutOfRangeException, occurs when attempting to access an index that is out of range within a collection. In the context of comparing two DOCX files using Syncfusion’s library, this usually points to an issue with the structure or content of the specific DOCX file.

Why This Happens

  1. Corrupted or Malformed DOCX File: The file may have invalid formatting or unsupported elements that the Syncfusion library cannot process correctly.
  2. Unsupported Elements in the Document: Features like complex fields, custom styles, or embedded objects may not be handled as expected during comparison.
  3. Library Version Mismatch: Using an outdated version of the Syncfusion library may lead to errors when handling certain document elements.

How to Fix It

  1. Validate the DOCX Files
    Open the problematic DOCX file in Microsoft Word and inspect it for any formatting issues. Use the "Inspect Document" feature in Word to detect and remove any problematic elements.

  2. Update Syncfusion Library
    Ensure you are using the latest version of the Syncfusion DocIO library, as newer versions often include bug fixes and enhanced support for complex document elements.

  3. Handle Exceptions Gracefully
    Modify your code to handle exceptions during the comparison process. For example:


    try { document1.Compare(document2, ComparisonOptions.Default); } catch (ArgumentOutOfRangeException ex) { // Log the error and handle gracefully Console.WriteLine("Error comparing documents: " + ex.Message); }
  4. Check Document Structure
    If the issue persists, parse the documents to identify problematic paragraphs, sections, or fields. You can iterate through the document body and log elements to pinpoint where the error occurs:


    foreach (WSection section in document.Sections) { foreach (WParagraph paragraph in section.Body.Paragraphs) { Console.WriteLine(paragraph.Text); } }
  5. Reach Out to Experts
    If you’re unable to resolve the issue, it may require specialized knowledge in Syncfusion and .NET Core. A trusted .NET Core development company, like CMARIX, can assist in diagnosing and fixing such errors efficiently.



DS Dharanya Sakthivel Syncfusion Team November 21, 2024 09:30 AM UTC

Hi Adrian,

Thank you for your suggestions.

Adithya, kindly try the provided suggestions by checking whether the document is corrupted or if an older version of the packages is being used. To resolve this, verify if the Word document opens correctly in Microsoft Word, or update to the latest package version.

If you are still facing the issue, kindly provide the following details as mentioned earlier:

  1. Confirm if the problem occurs when running the attached sample on your side.
  2. Share the complete code snippet or a sample that reproduces the issue.

Based on these details, we will proceed further to replicate the problem and then share more details about it.

Regards,
Dharanya.


Loader.
Up arrow icon