Error: Method not found: 'System.Collections.IEnumerator Aspose.Pdf.PageCollection.GetEnumerator()'

I’m using the latest version of Aspose.PDF with .NET 9. I apply an ImageStamp to each page of a PDF document using the following logic:

If I use a foreach (var page in inputDocument.Pages), it fails with the error:

Method not found: 'System.Collections.IEnumerator Aspose.Pdf.PageCollection.GetEnumerator()'

and if I use for loop like below, its not setting the image background in linux container but working in windows.

var pages = inputDocument.Pages.Count;
for (int i = 1; i <= pages; i++)
{}

@kishor338

The error message you’re encountering, Method not found: 'System.Collections.IEnumerator Aspose.Pdf.PageCollection.GetEnumerator()', suggests that there may be an issue with the version of the Aspose.PDF library you are using. This method is typically used for iterating over the pages in a PDF document.

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

  1. Check Version Compatibility: Ensure that you are using a compatible version of Aspose.PDF with your .NET framework. The latest versions of Aspose.PDF may have changes that are not compatible with older versions of .NET.

  2. Use a Different Loop: Since the foreach loop is causing an error, you can continue using the for loop as you have shown. However, make sure that you are correctly applying the ImageStamp to each page. Here’s an example of how to do this:

    // Load the PDF document
    Document inputDocument = new Document("input.pdf");
    ImageStamp imageStamp = new ImageStamp("image.png");
    imageStamp.Background = true; // Set the stamp as background
    
    // Apply the stamp to each page
    for (int i = 1; i <= inputDocument.Pages.Count; i++)
    {
        Page page = inputDocument.Pages[i];
        page.AddStamp(imageStamp);
    }
    
    // Save the updated document
    inputDocument.Save("output.pdf");
    
  3. Check Environment Differences: Since you mentioned that the for loop works on Windows but not in a Linux container, ensure that all dependencies and the environment are correctly set up in the Linux container. Sometimes, file paths or permissions can differ between environments.

  4. Update Aspose.PDF: If you are not using the latest version of Aspose.PDF, consider updating to the latest version, as it may contain bug fixes and improvements that could resolve your issue.

If the problem persists after trying these steps, you may want to reach out to Aspose support or check the documentation for any specific notes regarding the PageCollection class and its enumerator.

Feel free to ask if you have any further questions or need additional assistance!