At the moment I am developing an application which should merge different pptx files (each containing one single slide) into one big presentation file.
As I am new to docx4j/pptx4j and the OpenXML concept, I can just guess what is wrong about my code:
Using java Syntax Highlighting
File[] presentations = new File("C:/Temp/pptx").listFiles(); // pptx-files from source folder
File targetFile = new File("C:/Temp/merged.pptx"); // target file containing all slides
PresentationMLPackage targetPackage = PresentationMLPackage.createPackage();
MainPresentationPart pp = (MainPresentationPart) targetPackage.getParts().getParts()
.get(new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart) targetPackage.getParts().getParts()
.get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
int counter = 1;
for (File pptx : presentations) {
PresentationMLPackage sourcePackage = (PresentationMLPackage) OpcPackage.load(pptx);
Map<PartName, Part> partMap = sourcePackage.getParts().getParts();
for (PartName name : partMap.keySet()) {
Part part = partMap.get(name);
if (part instanceof SlidePart) {
SlidePart slide = (SlidePart) part;
SlidePart slidePart = PresentationMLPackage.createSlidePart(pp, layoutPart,
new PartName("/ppt/slides/slide" + counter++ + ".xml"));
slidePart.setJaxbElement(slide.getJaxbElement());
}
}
}
targetPackage.save(targetFile);
File targetFile = new File("C:/Temp/merged.pptx"); // target file containing all slides
PresentationMLPackage targetPackage = PresentationMLPackage.createPackage();
MainPresentationPart pp = (MainPresentationPart) targetPackage.getParts().getParts()
.get(new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart) targetPackage.getParts().getParts()
.get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
int counter = 1;
for (File pptx : presentations) {
PresentationMLPackage sourcePackage = (PresentationMLPackage) OpcPackage.load(pptx);
Map<PartName, Part> partMap = sourcePackage.getParts().getParts();
for (PartName name : partMap.keySet()) {
Part part = partMap.get(name);
if (part instanceof SlidePart) {
SlidePart slide = (SlidePart) part;
SlidePart slidePart = PresentationMLPackage.createSlidePart(pp, layoutPart,
new PartName("/ppt/slides/slide" + counter++ + ".xml"));
slidePart.setJaxbElement(slide.getJaxbElement());
}
}
}
targetPackage.save(targetFile);
Parsed in 0.016 seconds, using GeSHi 1.0.8.4
I think it is not enough to only merge the SlidePart-Objects. All other content is missing and the formatting of the resulting presentation differs from the input files.
So my questions are:
1. How can I change the format settings of the resulting presentation to the source presentations' settings ?
2. What (and how) has to be copied/cloned from the source presentations into the target presentation without losing embedded objects (pictures) ?
Thank you very much for your help!