'Copy slideshow slides to new slideshow including master and box locations(Apache POI)

Is there any way to completely copy a slide from one slideshow to another slideshow including all of the box locations, colors, fonts, etc.?

Referencing the cookbook I have the below

    private void copySlides(XMLSlideShow fromPptx, XMLSlideShow toPptx) {
        for(XSLFSlide fromSlide : fromPptx.getSlides()){
            toPptx.createSlide().importContent(fromSlide);
        }
    }

However the background doesn't match the 'from' pptx and the a number of the text boxes are shifted. I'm guessing the colors are due to me not modifying the master and the shift is because something related to the anchors. I have also tried what I thought would force the master of one slide onto the other, but it does the same thing as above.

    private void copySlides(XMLSlideShow fromPptx, XMLSlideShow toPptx) {
        for(XSLFSlide fromSlide : fromPptx.getSlides()){
            XSLFSlide toSlide = toPptx.createSlide();
            copySlide(fromSlide, toSlide);
            toSlide.importContent(fromSlide);
        }
    }

    private static void copySlide(
            final XSLFSlide fromSlide, final XSLFSlide toSlide) {

        XSLFSlideLayout fromLayout = fromSlide.getSlideLayout();
        XSLFSlideMaster fromMaster = fromSlide.getSlideMaster();
        XSLFSlideLayout toLayout = toSlide.getSlideLayout();
        XSLFSlideMaster toMaster = toSlide.getSlideMaster();
        toLayout.importContent(fromLayout);
        toMaster.importContent(fromMaster);
    }


Solution 1:[1]

If I set either toSlide.setFollowMasterGraphics(false); or toSlide.setFollowMasterObjects(false); I can set the background color.

For the boxes not being in the correct location it seems that the boxes were in the correct location, but the slides were different sizes. In my case I'm fine with using whatever is the 'final' slide size, and if multiple sizes show up someone will have to deal with it.

    private void copySlides(XMLSlideShow fromPptx, XMLSlideShow toPptx) {
        for(XSLFSlide fromSlide : fromPptx.getSlides()){
            XSLFSlide toSlide = toPptx.createSlide();
            copySlide(fromSlide, toSlide);
            toSlide.importContent(fromSlide);
        }
        toPptx.setPageSize(fromPptx.getPageSize());
    }

    private static void copySlide(
            final XSLFSlide fromSlide, final XSLFSlide toSlide) {
        toSlide.setFollowMasterGraphics(false);
        toSlide.setFollowMasterObjects(false);

        XSLFSlideLayout fromLayout = fromSlide.getSlideLayout();
        XSLFSlideMaster fromMaster = fromSlide.getSlideMaster();
        XSLFBackground fromBackground = fromSlide.getBackground();
        XSLFSlideLayout toLayout = toSlide.getSlideLayout();
        XSLFSlideMaster toMaster = toSlide.getSlideMaster();
        XSLFBackground toBackground = toSlide.getBackground();

        toLayout.importContent(fromLayout);
        toMaster.importContent(fromMaster);
        toBackground.setFillColor(fromBackground.getFillColor());
    }

This does not fully answer my question (which may be unobtainable based on some forum comments I read) but this solves the problem I was up against.

For anyone else that comes across this I also played with getting all of the shapes in the 'from' and 'to' slides and trying to copy things from one to the other, but was not necessary for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nanotek