John
John

Reputation: 41

Duplicating multiple powerpoint slides using docx4j

I'm trying to duplicate a couple slides using docx4j but I am running into some trouble.

I have a .pptx file with 3 pages and I am looking to duplicate each page several times. This is my code so far (based off samples from here:

    PresentationMLPackage presentationMLPackage = (PresentationMLPackage) OpcPackage.load(new File(args[0]));

    MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
            new PartName("/ppt/presentation.xml"));

    List<SlidePart> slideParts = pp.getSlideParts();

    int index = 1;

    for(SlidePart slidePart : slideParts){

        SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(new PartName("/ppt/slideLayouts/slideLayout" + index + ".xml"));
        Sld copied = XmlUtils.deepCopy(slidePart.getContents(), slidePart.getJAXBContext());

        SlidePart slide = new SlidePart(new PartName("/ppt/slides/slide" + index + ".xml"));
        slide.setContents(copied);

        pp.addSlide(1, slide);

        slide.addTargetPart(layoutPart);

        index++;
    }

    presentationMLPackage.save(new java.io.File("test.pptx"));

My code runs fine without any errors and when I open the file, I am alerted of an error within the powerpoint and it asks to repair the file and when you do, the page is blank and the page did not duplicate.

How do I get this working properly?

Upvotes: 4

Views: 231

Answers (1)

JasonPlutext
JasonPlutext

Reputation: 15863

In the general case, this isn't so straight forward. You need to take care of the slide rels etc.

For this reason, we have a commercial solution in Docx4j Enterprise, which you can find at https://www.plutext.com/m/index.php/products

Upvotes: 0

Related Questions