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’)
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:
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.
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.
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.
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.
@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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.