We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to apply potx template to an existing PPTX file

Hi,

I have a requirement of changing the template (ot theme) of an existing PPTX file. i.e there is already a file which follwos some template and i need to update the template to a current one. Only the background, formatting ets should change whereas the data should remain intact.

Is this possible using Aspose.Java? How you guide me how to go about it ?

Thanks in advance and regards,

Kiran.

Hi Kiran,

I have observed the requirement shared by you and like to share that Aspose.Slides allows you to apply the master from template POTX to a source presentation. However, this comes with some prerequisites. There are layout slides in PPTX and every slide has some layout slide type that resides in the used master of the presentation. Now, if you wish to apply new master to any of source slide the layout slide type of source slide must be available in target master layout slides. E.g, if the source slide has layout type Title and you wish to change its master then you need to ensure that layout slide type of Title must be available in new master. If it is not available and you still apply the master then Aspose.Slides will throw exception that you need to handle. Please visit this documentation link for your kind reference. Please share, if I may help you further in this regard.

Many Thanks,

Hi,

Can you share the code for the above implementation? I did go through the documentation link that you sent , but was unable to figure out the implementation.

For eg: I have a pptx file in a PresenatationEx object pres1 and the potx template in another PresentationEx object pres2. How do i apply the styles from pres2 to pres1 and save in a different pptx (ie the new pptx file has the data of old pptx file and the style / formatting of the potx file).

Can you give me a contact number where this can de clarified better?

Thanks and Regards,

Kiran

Hi Kiran,

I have observed the requirements shared by you and have generated the sample code to serve the purpose on your end. Please try using the following sample code on your end to apply the template on source presentation slides. The following code copy the template master to source presentation and apply that on individual slide. In the end it saves a new presentation with changed master theme.

public static void MergePPTX(String presPath,String SourcePres, String TargeFile, String templateFile)
{
try {
PresentationEx srcpres = new PresentationEx(presPath+SourcePres);
PresentationEx MasterPres = new PresentationEx(presPath + templateFile);

PresentationEx newPres = new PresentationEx();
SlideEx slide = null;

// newPres.getSlides().removeAt(0);
// newPres.getMasters().removeAt(0);

int idx = srcpres.getMasters().addClone(MasterPres.getMasters().get_Item(0));
MasterSlideEx mas = newPres.getMasters().get_Item(idx);

// Clone the desired slide to the end of the collection of slides in the same PPTX
for (int i = 0; i < srcpres.getSlides().getCount(); i++)
{
slide=srcpres.getSlides().get_Item(i);

try
{
byte type=slide.getLayoutSlide().getLayoutType();
slide.getLayoutSlide().setMasterSlide(mas);

} catch (PptxException ex)
{
//Changing layout of source slide to some default layout slide in cloned master
slide.setLayoutSlide(mas.getLayoutSlides().get_Item(1));
}
}

srcpres.getMasters().removeUnused(true);

// Write the modified pptx to disk
srcpres.write(presPath + TargeFile + “.pptx”);
} catch (Exception e) {
e.printStackTrace();
}
}


Many Thanks,

Hi,

I tried the code posted by you but got the following errors:

1) class com.aspose.slides.pf4dd765c.pbdb106a0.h: Index is less than 0 or more than or equal to the list count.

Parameter name: index

Actual value was 6.

com.aspose.slides.Collections.ArrayList.do(Unknown Source)

com.aspose.slides.Collections.ArrayList.get_Item(Unknown Source)

com.aspose.slides.MasterSlidesEx.get_Item(Unknown Source)

com.aspose.slides.MasterSlideExCollection.get_Item(Unknown Source)

com.infosys.finacleportal.batch.TestJNPI.main2(TestJNPI.java:100)

com.infosys.finacleportal.batch.TestJNPI.main(TestJNPI.java:76)

at line -> "MasterSlideEx mas = newPres.getMasters().get_Item(idx); "

Request you to help me solve the issues.

P.S: I'm using the latest jars downloaded today.

Reagrds,

Kiran.

Hi Kiran,


I have observed the stack trace shared by you and request you to please share the source presentation and source template with us so that I may use them on my end for investigation and helping you out. I will really appreciate your cooperation in this regard.

Many Thanks,

The earlier files i tried with are confidential files, thus i tried it with some sample files. The error still exists. The stack trace is :

class com.aspose.slides.pf4dd765c.pbdb106a0.h: Index is less than 0 or more than or equal to the list count.

Parameter name: index

Actual value was 1.

com.aspose.slides.Collections.ArrayList.do(Unknown Source)

com.aspose.slides.Collections.ArrayList.get_Item(Unknown Source)

com.aspose.slides.MasterSlidesEx.get_Item(Unknown Source)

com.aspose.slides.MasterSlideExCollection.get_Item(Unknown Source)

at line "MasterSlideEx mas = newPres.getMasters().get_Item(idx);"

I'm also attaching the files.

PS: Sample.potx is the master template and the Sample1.pptx is the source fiel that needs to be changed to the existing template.

Hi Kiran,

Thanks for sharing the requested information. Please use the following sample code on your end to serve the purpose as I have made necessary rectification in the sample code. Please share if there is any further issue incurring on your end.

public static void MergePPTX(String presPath,String SourcePres, String TargeFile, String templateFile)
{
try {
PresentationEx srcpres = new PresentationEx(presPath+SourcePres);
PresentationEx MasterPres = new PresentationEx(presPath + templateFile);

PresentationEx newPres = new PresentationEx();
SlideEx slide = null;

// newPres.getSlides().removeAt(0);
// newPres.getMasters().removeAt(0);
int idx = srcpres.getMasters().addClone(MasterPres.getMasters().get_Item(0));
MasterSlideEx mas = srcpres.getMasters().get_Item(idx);

// Clone the desired slide to the end of the collection of slides in the same PPTX
for (int i = 0; i < srcpres.getSlides().getCount(); i++)
{
slide=srcpres.getSlides().get_Item(i);

try
{
byte type=slide.getLayoutSlide().getLayoutType();
slide.getLayoutSlide().setMasterSlide(mas);

} catch (PptxException ex)
{
//Changing layout of source slide to some default layout slide in cloned master
slide.setLayoutSlide(mas.getLayoutSlides().get_Item(1));
}
}

srcpres.getMasters().removeUnused(true);

// Write the modified pptx to disk
srcpres.write(presPath + TargeFile + “.pptx”);
} catch (Exception e) {
e.printStackTrace();
}
}

Many Thanks,

Hi Mudassir,

Tahnk you so much for the code. It is working fine except for a few minor issues. I have noticed that "Title slide" and "Section Header" layouts are not getting formatted properly. I'm attaching a sample that I tested the code with. Slide # 1 & 2 in the "sample edited.pptx" are not in the proper format. "Sample.pptx" is the Master presentation and "Sample1.pptx" is the file that has to be changed.

Kindly look into this issue.

Regards,

Kiran

Hi Kiran,


I have worked with the presentation files shared by you and have been able to observed the issue with master getting applied on first two slides. An issue with ID SLIDESJAVA-34225 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be resolved.

We are sorry for your inconvenience,

The issues you have found earlier (filed as SLIDESJAVA-34225) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.