Aspose.Pdf.Comparison.SideBySidePdfComparer Error: Index was out of range

Sometime when adding multiple ‘ExcludeAreas’ the Compare methode is running into an error:
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter ‘index’)

                    // Set the comparison options
                    SideBySideComparisonOptions optionsForEachPage = new SideBySideComparisonOptions
                    {
                        ComparisonArea1 = comparisonArea1,
                        ComparisonArea2 = comparisonArea2,
                        ExcludeAreas1 = excludeAreas1.ToArray(),
                        ExcludeAreas2 = excludeAreas2.ToArray(),
                        ComparisonMode = comparisonMode,
                        AdditionalChangeMarks = additionalChangeMarks,
                        ExcludeTables = excludeTables
                    };


                   SideBySidePdfComparer.Compare(pdfDocumentA.Pages[i], pdfDocumentB.Pages[i], outputCompareSideBySidePdf, optionsForEachPage);

@horst.hoefle

The error “Index was out of range” typically occurs when you attempt to access an element in a collection using an index that is either negative or exceeds the number of elements in that collection. In your case, this error may arise when adding multiple ExcludeAreas to the SideBySideComparisonOptions.

Here are a few steps you can take to troubleshoot and resolve this issue:

  1. Check the Size of ExcludeAreas: Before passing the ExcludeAreas to the SideBySideComparisonOptions, ensure that the arrays excludeAreas1 and excludeAreas2 are not empty and that their sizes match the expected number of areas you want to exclude.

  2. Validate Page Count: Ensure that the pages you are comparing (pdfDocumentA.Pages[i] and pdfDocumentB.Pages[i]) exist and that i is within the bounds of the page count for both documents. If one document has fewer pages than the other, this could lead to an index error.

  3. Debugging: You can add some debugging statements to log the sizes of excludeAreas1, excludeAreas2, and the current index i to help identify where the issue might be occurring.

  4. Review Documentation: Make sure you are following the correct usage patterns as outlined in the Aspose documentation for comparing PDF documents. This includes ensuring that the ComparisonArea1 and ComparisonArea2 are correctly defined and that the ExcludeAreas are properly set up.

Here is a sample code snippet that includes checks for the sizes of the arrays:

// Ensure exclude areas are not null and have valid sizes
if (excludeAreas1 != null && excludeAreas1.Count > 0 && excludeAreas2 != null && excludeAreas2.Count > 0)
{
    // Set the comparison options
    SideBySideComparisonOptions optionsForEachPage = new SideBySideComparisonOptions
    {
        ComparisonArea1 = comparisonArea1,
        ComparisonArea2 = comparisonArea2,
        ExcludeAreas1 = excludeAreas1.ToArray(),
        ExcludeAreas2 = excludeAreas2.ToArray(),
        ComparisonMode = comparisonMode,
        AdditionalChangeMarks = additionalChangeMarks,
        ExcludeTables = excludeTables
    };

    // Ensure the page index is valid
    if (i < pdfDocumentA.Pages.Count && i < pdfDocumentB.Pages.Count)
    {
        SideBySidePdfComparer.Compare(pdfDocumentA.Pages[i], pdfDocumentB.Pages[i], outputCompareSideBySidePdf, optionsForEachPage);
    }
    else
    {
        // Handle the case where the page index is out of range
        Console.WriteLine("Page index is out of range for one of the documents.");
    }
}
else
{
    Console.WriteLine("Exclude areas are not properly defined.");
}

By implementing these checks, you should be able to avoid the “Index was out of range” error and ensure that your comparison logic works as intended. If the problem persists, consider reaching out to the Aspose support community for further assistance.

This has already been implemented, the error occurs on some pages and not on others even though the same ExcludeAreas are used everywhere.

@horst.hoefle
Please attach the documents with which this is happening.

image.png (50,1 KB)

AsposeTestFileCompareA_IndexOutOfRange.pdf (99,6 KB)

AsposeTestFileCompareB_IndexOutOfRange.pdf (104,2 KB)

I have added two entries for the ExludeAreas1 and ExludeAreas2, both have the same values and will be shown in image.png.

Aspose.PDF Version 25.2.0

ComparisonMode = normal,
AdditionalChangeMarks = false,
ExcludeTables = true

@horst.hoefle
Thanks for writing to us.
I have reproduced the bug and will create a task for the development team.

@horst.hoefle
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-59341

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you for your support, at the moment it is not our highest priority.

@horst.hoefle
Thanks even more for taking the time to write to us.