[.NET] Strange results trying to save a presentation with a different Master

Hi,

I'm using the latest .NET Aspose.Slides version with PP2003 trying to save a presentation with a different master.

I have following code:

Presentation originalPresentation = new Presentation(pathToOriginalPresentation);

Presentation presentationContainingNewMasters = new Presentation(pathContainingMastersToApply);

Slide mainMaster = presentationContainingNewMasters.MainMaster;

//Code to retrieve TitleMaster is copied from ASPOSE demo.

Slide titleMaster = AsposeCommon.GetTitleMaster(presentationContainingNewMasters);

if(titleMaster == null)

{

titleMaster = mainMaster;

}

foreach(Slide slide in originalPresentation.Slides)

{

if(slide.Layout == SlideLayout.TitleSlide)

{

slide.ChangeMaster(titleMaster);

}

else

{

slide.ChangeMaster(mainMaster);

}

}

originalPresentation.Write(pathToSaveTheResultingPresentation);

This code uses the files PresentationInOldStyle.ppt and StyleToApply.ppt. This results in a new presentation: PresentationInNewStyle.ppt (all .ppt are in attached .zip file).

After running the code I open the resulting ppt: In there, the old master is kind-of half applied.I goto View-> Master->Slide Master. The masters show as white rectangles with animated border. Clicking on them reveals the original masters (not the ones I tried to apply). If I try to save the the presentation, I get 100% CPU usage and eventually have to kill the process to get rid of it.

Has anyone else ran into something like this? Is my code wrong?

Thanks for any help and suggestions!

Dom

To change master you should already have old and new masters in your presentation.
ChangeMaster function doesn’t copy any slides from one presentation to another.

Hi Alexey,

Thanks for your help.

I have changed the code to do following:

  1. Create new presentation
  2. Copy new masters into new presentation
  3. Copy content into new presentation
  4. Apply masters to content.

This is the code:

Presentation originalPresentation = new Presentation(pathToOriginalPresentation);

originalPresentation.Slides.Remove(originalPresentation.MainMaster);

Presentation presentationContainingNewMasters = new Presentation(pathContainingMastersToApply);

Slide mainMaster = presentationContainingNewMasters.MainMaster;

//Code to retrieve TitleMaster is copied from ASPOSE demo.

Slide titleMaster = AsposeCommon.GetTitleMaster(presentationContainingNewMasters);

if(titleMaster == null)

{

titleMaster = mainMaster;

}

Presentation newPresentation = new Presentation();

SortedList temp = new SortedList();

//Insert the masters first:

Slide newMaster = presentationContainingNewMasters.CloneSlide(mainMaster, 0, newPresentation, temp);

Slide newTitleMaster = presentationContainingNewMasters.CloneSlide(titleMaster, 1, newPresentation, temp);

//Insert the presentation and apply the masters to them:

for(int idx = 0; i < originalPresentation.Slides.Count; idx++)

{

Slide newSlide = originalPresentation.CloneSlide(originalPresentation.Slides[idx],newPresentation.Slides.LastSlidePosition + 1, newPresentation, temp);

if(newSlide.Layout == SlideLayout.TitleSlide)

{

newSlide.ChangeMaster(newTitleMaster);

}

else

{

newSlide.ChangeMaster(newMaster);

}

}

newPresentation.DeleteUnusedMasters();

newPresentation.Write(pathToSaveTheResultingPresentation);

this works partially, the new masters are applied to the content. BUT:

  1. I have an empty first slide
  2. The placeholders in the content are formatted wrongly. (I tried attaching a .zip containing my presentations, but it doesn't seem to pickup my attachment). I experimented with the ChangeMaster() overloads, setting the boolean field to false and true, but whatever way, the result is always the same.

Is there anything wrong with my code?

Thanks heaps for any help!

Dominic

You shouldn’t clone TitleMaster slide or any master slides.
Instead you should clone any slide inherited from this master.
In this case master slides will be cloned automatically.
After that you can delete cloned (normal) slide if you don’t need it.
At the end you can change master of your slides.

Hi Alexey,

Thanks for your help.
Still have a problem, though:

I clone the contents slides of the presentation containing the masters I want into a new presentation (this creates an empty presentation with one empty slide and its master. I can delete the empty slide after having inserted the new slides.). At this stage I would like to delete my unwanted content slides but can't because afterwards I have to call DeleteUnusedMasters() and that would remove my new masters, too. I tried to retrieve MasterId to differentiate between masters, but they seem to always be 0 once in the new presentation.

In the end I managed to get it to almost work. Almost, because the font of my new masters is not carried over.

My convoluted code looks like this. There MUST be an easier, better way:


/*
* Steps to change master:
* 1. Insert content slides of presentation containing new masters. This will automatically
* copy the masters over (cloning the masters directly won't work).
* 2. An empty first slide and its master have been automatically inserted into the new
* presentation. Get rid of these.
* 2. Copy the content slides into the presentation.
* 3. Change masters so the content slides use the new masters.
* 4. Delete unused masters to get rid of the old masters and the blank one if present.
* 5. Delete the content slides of the original presentation.
*/
Presentation originalPresentation = new Presentation(pathToOriginalPresentation);

Presentation presentationContainingNewMasters = new Presentation(pathContainingMastersToApply);

Presentation newPresentation = new Presentation();
SortedList temp = new SortedList(); //We don't do anything with this, but the Aspose CloneSlide() method requires it as input parameter.

//Copy content slides of presentation containing new masters into new
//slide show. Deleting the (unwanted) content straight away didn't work
//because 1. Can't delete all slides of a presentation.
// 2. If I delete the content, DeleteUnusedMasters() will delete not
// only the master of the blank slide but also the new masters.
//Therefore keep the the content slides and delete them later.
ArrayList al = new ArrayList();
for(int j = 0; j < presentationContainingNewMasters.Slides.Count; j++)
{
Slide s = presentationContainingNewMasters.CloneSlide(presentationContainingNewMasters.Slides[j],
newPresentation.Slides.LastSlidePosition + 1,
newPresentation, temp);
al.Add(s);
}

//the first slide added is empty. Remove it and its master:
newPresentation.Slides.RemoveAt(0);
newPresentation.DeleteUnusedMasters();
//Get the masters that have to be applied:
Slide newTitleMaster = AsposeCommon.GetTitleMaster(newPresentation);
Slide newMaster = newPresentation.MainMaster;

//Insert the content slides:
for(int idx = 0; idx < originalPresentation.Slides.Count; idx++)
{
Slide newSlide = originalPresentation.CloneSlide(originalPresentation.Slides[idx],
newPresentation.Slides.LastSlidePosition + 1,
newPresentation,
temp);
}

if(newTitleMaster == null)
{
newTitleMaster = newMaster;
}

//Apply the new masters to the content slides:
foreach(Slide s in newPresentation.Slides)
{
if(s.Layout == SlideLayout.TitleSlide)
{
s.ChangeMaster(newTitleMaster, true);
}
else
{
s.ChangeMaster(newMaster, true);
}
}

newPresentation.DeleteUnusedMasters();
//Remove the content slides of the presentation containing
//the masters.
IEnumerator std = al.GetEnumerator();
while(std.MoveNext())
{
Slide s = std.Current as Slide;
newPresentation.Slides.Remove(s);
}

newPresentation.Write(pathToSaveTheResultingPresentation);

Any suggestions to improve this very welcome! And even more important, how do I get the font from my new master carried over?

Cheers,
Dominic

Could my problem be the same as in the ‘garbled text when cloning’ thread?

It’s almost the same problem but with small difference.
I’m trying to solve both of them.

Alexey,

Thanks! Release 2.4.5.0 fixed my problem.

cheers,

Dom