Free Support Forum - aspose.com

Separating Design and Layout Templates

I have a client that wants to be able to create presentations where the theme varies based on who is creating the presentation. What I thought would be ideal would be to have 2 templates, one for layout - specifying what data goes where, and one for design - specifying background images and font styles for headers and the like. Is this something that would be easy to do with Aspose?


As an example, lets say we have 2 customers: Acme and WidgetsInc and we have 2 different reports we want to generate in PPT: InvoiceReport and TPSReport. What I want to avoid is having one template for each customer/report combination - AcmeInvoiceReport.ppt, AcmeTPSReport.ppt, WidgetsIncInvoiceReport.ppt, and WidgetsIncTPSReport.ppt. Since there a lot of customers and a lot of reports, this would be a maintenance nightmare. Instead what I would want would be to have 1 layout template for each report - InvoiceReport.ppt, TPSReport.ppt - and 1 design/theme layout for each customer - Acme.pot, WidgetsInc.pot.

I am evaluating Aspose for this purpose, and would like to know what might be involved in doing something like this.

Thank you for your time,
~Jason Walker

Dear Jason,

Thank you for considering Aspose.Slides.

Yes, it would be very easy. Aspose.Slides provide an API namely Slide.ChangeMaster. With this API, you can change the master of the normal slides.

You will import master from your design/theme layout and change the master of normal slides in your report presentations with the newly imported master.

You can import master by using Presentation.CloneSlide method. Whenever you clone slide from other presentation (in this case from your design template), its master slide is also copied. Later you can delete this extraneous slide.

I will also provide you an example, to show you how can we change presentation designs to our template presentation design.

Dear Jason,

I have attached a project having C# code. When you run it, it asks you to choose one of two templates, and then it changes the design of source.ppt to the chosen template design.

The archive file contains a folder that contains

Template1.pot

Template2.pot

Source.ppt

After running successfully, you get outWithTemplate.ppt having the contents of source.ppt but design of Template1.pot or Template2.pot (whatever you will choose).

Just run the solution, add a reference to Aspose.Slides and change the dirPath variable to the directory containing all the above 3 files.

You can also see outWithTemplate.ppt generated by me.

This is the code.

void ChangeTemplate()

{

string dirPath = @"d:\downloads";

Console.WriteLine("Which template do you want to follow?");

Console.WriteLine("Press 1 for Templat1.");

Console.WriteLine("Press 2 for Templat2.");

char ch = (char)(Console.ReadKey().Key);

string template;

if (ch == '1')

template = "Template1.pot";

else

template = "Template2.pot";

Presentation srcTemplate = new Presentation(dirPath + @"\" + template);

//Source file whose template is to be changed

Presentation srcPres = new Presentation(dirPath + @"\source.ppt");

//Clone slide from template to import master and delete extraneous slide

Slide sld = srcTemplate.CloneSlide(srcTemplate.GetSlideByPosition(1),

1, srcPres, new SortedList());

//Save the master id of the newly cloned slide

uint mstrId = sld.MasterId;

//Delete the extraneous slide

srcPres.Slides.Remove(sld);

//Get the master slide

Slide mstrSld = srcPres.GetSlideById(mstrId);

//Change the master of all the normal slides in source presentation

for (int i = 1; i <= srcPres.Slides.LastSlidePosition; i++)

{

Slide normSld = srcPres.GetSlideByPosition(i);

normSld.ChangeMaster(mstrSld, true);

}

//Finally write the source presentation with new template

//on disk

srcPres.Write(dirPath + @"\outWithTemplate.ppt");

Console.WriteLine("\noutWithTemplate.ppt Created.");

}

Here is the code in JAVA, it does the same thing as the above code in C#

JAVA Code:

-----------------------------------------------------------------

public static void ChangeTemplate() throws Exception

{

String dirPath = "D:\\downloads\\Change Design From Template";

System.out.print("Which template do you want to follow?");

System.out.print("Press 1 for Template1.");

System.out.print("Press 2 for Template2.");

char ch = (char) System.in.read();

String template="";

if (ch == '1')

template = "Template1.pot";

else

template = "Template2.pot";

Presentation srcTemplate = new Presentation(new FileInputStream( dirPath + "\\" + template));

//Source file whose template is to be changed

Presentation srcPres = new Presentation(new FileInputStream( dirPath + "\\source.ppt" ));

//Clone slide from template to import master and delete extraneous slide

Slide sld = srcTemplate.cloneSlide(srcTemplate.getSlideByPosition(1),

1, srcPres, new TreeMap());

//Save the master id of the newly cloned slide

long mstrId = sld.getMasterId();

//Delete the extraneous slide

srcPres.getSlides().remove(sld);

//Get the master slide

Slide mstrSld = srcPres.getSlideById(mstrId);

//Change the master of all the normal slides in source presentation

int lastSlidePos=srcPres.getSlides().getLastSlidePosition();

for (int i = 1; i <= lastSlidePos; i++)

{

Slide normSld = srcPres.getSlideByPosition(i);

normSld.changeMaster(mstrSld, true);

}

//Finally write the source presentation with new template on disk

srcPres.write(new FileOutputStream(dirPath + "\\outWithTemplate.ppt"));

System.out.print("\noutWithTemplate.ppt Created.");

}

-----------------------------------------------------------------