Dear Aspose Support Team,
We are currently using a C# function to remove watermarks from .pptx
files utilizing the Aspose.Slides library. We have a valid Aspose.Total for .NET license.
Our function loads the presentation using the new Presentation(strFilePath)
constructor. However, we are encountering an exception at this step when attempting to load certain .pptx
files.
The exception message is:
“Unable to cast object of type ‘Aspose.Slides.? ?’ to type ‘Aspose.Slides.?’”
We have attached a sample file that triggers this issue.
Could you please help us understand:
- Why this exception might be occurring during the loading of the presentation?
- Whether the file could be corrupted or saved in an unsupported format?
- If there are any best practices to safely validate or handle such files before processing them?
We appreciate your support and look forward to your guidance.
Thank you,
bool RemoveWatermarkFromPowerpoint(string strFilePath, string strOutputFile, out string strError)
{
bool bResult = true;
strError = "";
try
{
using (Presentation presentation = new Presentation(strFilePath))
{
string strFooterId = "";
if (presentation.DocumentProperties.ContainsCustomProperty("footer_id"))
{
strFooterId = presentation.DocumentProperties["footer_id"].ToString();
}
foreach (ISlide slide in presentation.Slides)
{
slide.Background.Type = Aspose.Slides.BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = Aspose.Slides.FillType.NoFill;
}
if (presentation.DocumentProperties.ContainsCustomProperty(m_strProperty))
{
presentation.DocumentProperties.RemoveCustomProperty(m_strProperty);
}
if (!string.IsNullOrEmpty(strFooterId))
{
foreach (ISlide slide in presentation.Slides)
{
var shapeCollection = new List<IShape>();
foreach (var shape in slide.Shapes)
{
if (shape is AutoShape &&
shape.Name.Equals("GalaxkeyFooter", StringComparison.OrdinalIgnoreCase))
{
shapeCollection.Add(shape);
}
}
foreach (var shape in shapeCollection)
{
slide.Shapes.Remove(shape);
}
}
}
presentation.Save(strOutputFile, Aspose.Slides.Export.SaveFormat.Pptx);
}
}
catch (Exception ex)
{
strError = ex.Message;
bResult = false;
}
return bResult;
}
PPTXFile.zip (3.3 MB)
@Ayush2105
Cause
The exception you’re encountering when loading certain .pptx
files with Aspose.Slides is likely due to file corruption, unsupported features in the presentation, or issues with embedded objects. The error message indicates a type casting issue, which typically arises when the library encounters unexpected data structures within the file.
Solution
-
File Validation: First, ensure that the .pptx
file is not corrupted. Open the file in Microsoft PowerPoint to verify that it loads without issues. If it does, the problem may be related to specific content in the file that Aspose.Slides cannot handle.
-
Load Options: Utilize LoadOptions
to manage how the presentation is loaded. For example, set options to ignore embedded binary objects, which may resolve issues related to such content.
-
Error Handling: Enhance your error handling to provide more context about the exception. This will assist in diagnosing the issue further.
Code
Here’s an updated version of your function that includes LoadOptions
to skip embedded binary objects:
bool RemoveWatermarkFromPowerpoint(string strFilePath, string strOutputFile, out string strError)
{
bool bResult = true;
strError = "";
try
{
LoadOptions loadOptions = new LoadOptions()
{
DeleteEmbeddedBinaryObjects = true // Skip embedded binary objects
};
using (Presentation presentation = new Presentation(strFilePath, loadOptions))
{
// Your existing logic to remove watermarks
// ...
presentation.Save(strOutputFile, Aspose.Slides.Export.SaveFormat.Pptx);
}
}
catch (Exception ex)
{
strError = ex.Message;
bResult = false;
}
return bResult;
}
Next Steps
- Test the updated function with the problematic
.pptx
files.
- If the issue persists, share the specific presentation file with Aspose support for further analysis.
- Review the Aspose.Slides documentation for any known issues or limitations regarding the types of presentations you are working with.
Please implement these suggestions and let me know if you need further assistance!
The load options didn’t resolve the issue. Also, the PowerPoint file is already attached and it’s not corrupted.
@Ayush2105,
Thanks for the template PowerPoint presentation file.
I reproduced your mentioned issue by using your template PowerPoint presentation (PPTX) file. I found an exception: “Aspose.Slides.PptxReadException: ‘Unable to cast object of type ‘e’ to type ‘’.’” when loading the PPTX file into Aspose.Slides for .NET object model.
Presentation document = new Aspose.Slides.Presentation("e:\\test2\\AsposeBug.pptx");
We require thorough evaluation of the issue. 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): SLIDESNET-45040
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.
I will wait for the issue to be resolved. Please update me as soon as possible and provide a resolution once it’s available. Thanks!
@Ayush2105,
Sure, we will keep you updated as soon as there are any developments. We hope your issue gets resolved soon.
@Ayush2105,
We are pleased to inform you that your reported issue (Ticket ID: “SLIDESNET-45040”) has been successfully resolved. The fix/enhancement is scheduled to be included in the upcoming release of Aspose.Slides for .NET (25.8), planned for the second half of August 2025. Rest assured, we will promptly notify you as soon as the new version becomes available.