Conversion to new version throwing exceptions

Hi, I'm frustrated with your Programmer Reference or Documentation, I cannot figure out how to convert old Slides code to new version, without putting code into production and having our data fail. Here is the latest code that I converted by "guessing". Please correct:

Old Code:

for (int idx = 0; idx < aPresentation.Fonts.Count; idx++)
{
Aspose.Slides.FontEntity font = aPresentation.Fonts[idx];

if (font.FontName == "ZapfDingbats")
{font.FontName = "Wingdings";}
}

New Code:

foreach (ISlide slide in aPresentation.Slides)
{
foreach (AutoShape shape in slide.Shapes)
{
foreach (IParagraph paragraph in shape.TextFrame.Paragraphs)
{
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.SymbolFont.FontName == "ZapfDingbats")
portion.PortionFormat.SymbolFont = new FontData("Wingdings");
}
}
}
}

Thanks,

Karen Schmidt

Hi Karen,

I have observed the sample code shared. I have seen that you are trying to change the symbol font. For normal text LatinFont is used and for East Asian Languages EastAsianFont is used. Can you please try using the following sample code on your end to serve the purpose.

foreach (ISlide slide in aPresentation.Slides)
{
foreach (AutoShape shape in slide.Shapes)
{
foreach (IParagraph paragraph in shape.TextFrame.Paragraphs)
{
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.SymbolFont.FontName == “ZapfDingbats”)
portion.PortionFormat.SymbolFont = new FontData(“Wingdings”);
if (portion.PortionFormat.LatinFont.FontName == “ZapfDingbats”)
portion.PortionFormat.LatinFont = new FontData(“Wingdings”);
if (portion.PortionFormat.EastAsianFont.FontName == “ZapfDingbats”)
portion.PortionFormat.EastAsianFont = new FontData(“Wingdings”);
}
}
}
}

Many Thanks,

Hi, I've tracked down the exception, I put a try catch around the foreach block, to capture the exception message, and it tells me this:

Unable to cast object of type 'Aspose.Slides.MasterSlide' to type 'Aspose.Slides.ISlide'.

that is puzzling, because in your example documentation, it uses the ISlideCollection enumeration for the presentation for either ppt or pptx. Is that correct? I am using the code as you suggested.

Thanks,

Karen Schmidt

Hi Karen Schmidt,

I have observed the exception message shared by you and it seems that you are trying to access the MasterSlideCollection and casting IMasterSlide to ISlide. Please remember that there are three slides collections in Aspose.Slides Presentation class:

  • IMasterSlideCollection holds IMasterSlide collection
  • ILayoutSlideCollection holds ILayoutSlide collection for selected master slide
  • ISlideCollection holds ISlide collection

These three slide collections have the respective slides types. When accessing any of slide collection you need to access that with particular slide type. I hope this information will be helpful. Please share, if I may help you further in this regard.


Many Thanks,