Heisenberg
Heisenberg

Reputation: 3193

Apache POI XSLFSlide page number

Is there any way to add page number to newly created slide with style inherited from previous slide?

XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream("templateFile.pptx"));
    final XSLFSlide[] slides = slideShow.getSlides();
    XSLFSlideMaster defaultMaster = slideShow.getSlideMasters()[0];
    XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE_ONLY);
    final XSLFSlide slide = slideShow.createSlide(titleLayout);
//how to set slide number for slide?

UPDATE Based on @Andreas Kühntopf answer I did some changes but it didn't help. Original slide uses TITLE_ONLY layout and has numbering in Powerpoint, but newly created slide doesn't.

Upvotes: 1

Views: 1490

Answers (2)

Heisenberg
Heisenberg

Reputation: 3193

Okay, combined with @Andreas Kuhntopf answer, I came to solution. First i need to select correct style for slide and later I need to manually copy spPr element of type numSld

        XSLFSlideMaster defaultMaster = slideShow.getSlideMasters()[0];
        XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE_ONLY);
        newSlide = slideShow.createSlide(titleLayout);
        final List<CTShape> spList = slide.getXmlObject().getCSld().getSpTree().getSpList();
        for (CTShape ctShape : spList) {
            try {
                final STPlaceholderType.Enum type;
                type = ctShape.getNvSpPr().getNvPr().getPh().getType();
                if (type == STPlaceholderType.SLD_NUM) {
                    final CTShape newSlideNumber = newSlide.getXmlObject().getCSld().getSpTree().addNewSp();
                    newSlideNumber.set(ctShape);
                    break;

                }
            } catch (NullPointerException e) {
                //Just ignore exception, this rather bad code style is used to avoid multiple checks
            }
        }

Upvotes: 0

Andreas K&#252;hntopf
Andreas K&#252;hntopf

Reputation: 151

I think you need to create the new XSLFSlide using

slideShow.createSlide(layout);

where layout is the XSLFSlideLayout you can get from the master sheet using

XSLFSlideMaster defaultMaster = slideShow.getSlideMasters()[0];    
XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);

Maybe you have to adjust where you get your layout from, but basically this should be the way to go.

Upvotes: 1

Related Questions