I need to combine several presentations into a single file while retaining the original order of the slides but couldn't find an example on your site. Many examples use pres.getSlides().get(i) but this returns the slides in random order. The examples using pres.getSlideByPosition(i) don't show how to loop through and clone all of the slides in a presentation.
The Java code below seems to be working. Can you review and let me know if you see any errors or issues that I may have missed?
It uses pres.getSlides().size() to get the total number of slides then pres.getSlides().getLastSlidePosition() to calculate the difference between total slides and visible slides. I did it this way so I could open the file and verify the number of master slides and visible slides in the presentation. If total slides=10 and lastSlidePos=7 then the file should contain 3 master slides and 7 visible slides. If total slides=8 and lastSlidePos=8 then the file should not contain any master slides and should have 8 visible slides. Is this correct? If so, can I just loop from 1 to lastSlidePos and use getSlideByPosition(i)?
It's very rare, but occasionaly errors appear when cloning slides or the output presentation is corrupt and won't open. It can usually be fixed by modifying the offending slide. Do you want to see those errors?
// Takes URL with variable number of presentations and combines into single file. Simplified for clarity
// Declarations
FileInputStream source = null;
FileOutputStream destination = null;
boolean exists = false;
// Create new empty target presentation
Presentation target = new Presentation();
// Store list of presentation names in string array
// URL format http://server.domain.com/appname?pn=12345.ppt&pn=34567.ppt&pn=76543.ppt
String[] presList = request.getParameterValues("pn");
// Loop through presentation list
for (int p = 0; p < presList.length; p++){
String pptPath = "C:\\Directory\\" + presList[p];
// Check if file exists
exists = (new File(pptPath)).exists();
if (exists){
// Open file
source = new FileInputStream(pptPath);
Presentation pres=new Presentation(source);
TreeMap tm1 = new TreeMap();
// Get total number of slides
int presSize = pres.getSlides().size();
// Get difference between total number of slides and last slide number
int presDif = presSize - pres.getSlides().getLastSlidePosition();
// Loop through remaining slides
// i.e if total slides=10 and lastSlidePos=7, there's 3 master slides so just get slides 1-7
for (int i = 1; i <= (presSize - presDif); i++) {
pres.cloneSlide(pres.getSlideByPosition(i),
target.getSlides().getLastSlidePosition() + 1, target, tm1);
}
source.close();
}
exists = false;
}
// Remove blank slide added when empty file was created
target.getSlides().remove(target.getSlideByPosition(1));
//Write the presentation as a PPT file
String destPath = ".\\" + "demo_new.ppt";
destination = new FileOutputStream(destPath);
target.write(destination);
destination.close();
// Return file outputstream to browser
[omitted]