Page 1 of 1

Merging different pptx files into one single presentation

PostPosted: Tue Jul 16, 2013 11:42 pm
by cyberdiver
Hello!

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:

Syntax: [ Download ] [ Hide ]
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);
 
Parsed in 0.018 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!

Re: Merging different pptx files into one single presentatio

PostPosted: Wed Jul 17, 2013 12:52 pm
by jason
You can learn more about the format from the PDF book http://openxmldeveloper.org/cfs-file.as ... lained.pdf

Things go wrong if you don't copy all the necessary parts, preserving their rels.

For this reason, we offer a commercial extension to do this, called MergePptx. See further http://www.plutext.com/m/index.php/products

If you want to have a try at writing similar code yourself, you'll need to familiarise yourself with RelationshipsPart, the addTargetPart method, and the intricacies of PresentationML.