PPT to PDF Conversion in C#: PptxReadException: Index Out of Range

Hi

While converting a PPT to PDF I am getting the following exception.

Aspose.Slides.PptxReadException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter ‘index’)
—> 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.get_Item(Int32 index)
at e.(ChartCategoryCollection ,  )
at e.(ChartCategoryCollection ,  ,  )
at e.(IChartData ,  , e )
at e.(IChart ,  , e )
at e.(IChart ,  ,  , e )
at e.(IShapeCollection , IChart ,  ,  )
at e.(IShapeCollection ,  ,  , Boolean )
at .( , IGroupShape ,  )
at .(IGroupShape ,  ,  )
at .(IBaseSlide , e ,  )
at .(Slide )
at .(IPresentation )
at .(e , IPresentation )
at .(Presentation , Stream , InterruptionToken )
— End of inner exception stack trace —
at .(Presentation , Stream , InterruptionToken )
at Aspose.Slides.Presentation.(Stream , Boolean )
at Aspose.Slides.Presentation.(Stream , Boolean )
at Aspose.Slides.Presentation…ctor(Stream stream, LoadOptions loadOptions)
at Aspose.Slides.Presentation…ctor(Stream stream)

What are the possible reasons and fixes for this.

Thank you

@himanshu_surana

Cause

The PptxReadException you’re encountering during the conversion of a PPT file to PDF typically indicates that there is an issue with the content of the PPT file itself. The specific error message suggests that there might be an attempt to access an index in a collection that is out of range, which can happen if the PPT file contains corrupted data or unsupported features.

Solution

To resolve this issue, you can try the following steps:

  1. Check the PPT File: Open the PPT file in Microsoft PowerPoint and ensure that it does not contain any corrupted elements or unsupported features. Save it again in a different format (like PPTX) and then try converting it again.

  2. Update Aspose.Slides: Ensure that you are using the latest version of Aspose.Slides for .NET. Updates often include bug fixes and improvements that may resolve your issue.

  3. Use LoadOptions: When loading the presentation, specify LoadOptions to handle potential issues more gracefully. For example, set LoadFormat to Pptx if you know the format of the file.

  4. Error Handling: Implement error handling in your code to catch exceptions and log details about the problematic slides or shapes.

Code

Here’s a sample code snippet that demonstrates how to use LoadOptions while loading a presentation:

using Aspose.Slides;
using Aspose.Slides.Export;

string srcFileName = "path_to_your_ppt_file.pptx";
string destFileName = "output.pdf";

try
{
    // Load the presentation with LoadOptions
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.LoadFormat = LoadFormat.Pptx; // Specify the format if known

    using (Presentation pres = new Presentation(srcFileName, loadOptions))
    {
        // Save the presentation to PDF
        pres.Save(destFileName, SaveFormat.Pdf);
    }
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Next Steps

  1. Test the above code with a known good PPT file to ensure that your conversion logic works correctly.
  2. If the issue persists, isolate the problematic slides by converting them individually to identify which slide is causing the exception.
  3. If you continue to face issues, please share the specific PPT file or a sample that reproduces the problem for further investigation.

Please let me know if you need any further assistance!

@himanshu_surana,
Thank you for reporting on the issue. We are sorry that you encountered this problem. We need more details to investigate the case. Please share the following additional files and information:

  • The sample presentation file
  • The code example to reproduce the exception
  • The OS version on which the error occurs
  • The .NET target platform in your application project
  • The Aspose.Slides version you are using