简体   繁体   中英

Apache POI XSLF creating section (s)

I'm struggling with section creation in apache POI. I want to be able to defina sections with slides, how can I manage to do that? I can append slides without any problem so far.

Here is an PowerPoint part of presentation saved as an XML where you can see how the sections are stored:

<pkg:part pkg:name="/ppt/presentation.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml">
    <pkg:xmlData>
        <p:presentation xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" showSpecialPlsOnTitleSld="0" embedTrueTypeFonts="1" saveSubsetFonts="1">
            (... cut out unrelevant nodes...)
            <p:sldIdLst>
                <p:sldId id="296" r:id="rId10"/>
                <p:sldId id="312" r:id="rId11"/>
                <p:sldId id="274" r:id="rId12"/>
                <p:sldId id="311" r:id="rId13"/>
                <p:sldId id="275" r:id="rId14"/>
                <p:sldId id="276" r:id="rId15"/>
                <p:sldId id="317" r:id="rId16"/>
                <p:sldId id="313" r:id="rId17"/>
                <p:sldId id="318" r:id="rId18"/>
                <p:sldId id="319" r:id="rId19"/>
                <p:sldId id="307" r:id="rId20"/>
                <p:sldId id="314" r:id="rId21"/>
                <p:sldId id="315" r:id="rId22"/>
                <p:sldId id="321" r:id="rId23"/>
                <p:sldId id="320" r:id="rId24"/>
                <p:sldId id="308" r:id="rId25"/>
                <p:sldId id="322" r:id="rId26"/>
                <p:sldId id="303" r:id="rId27"/>
                <p:sldId id="264" r:id="rId28"/>
                <p:sldId id="300" r:id="rId29"/>
                <p:sldId id="287" r:id="rId30"/>
                <p:sldId id="309" r:id="rId31"/>
                <p:sldId id="289" r:id="rId32"/>
            </p:sldIdLst>

            <p:extLst>
                <p:ext uri="{521415D9-36F7-43E2-AB2F-B90AF26B5E84}">
                    <p14:sectionLst xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main">
                        <p14:section name="Default Section" id="{F7FF9A22-6035-4F7F-86B4-79EDB5AF0A91}">
                            <p14:sldIdLst>
                                <p14:sldId id="296"/>
                            </p14:sldIdLst>
                        </p14:section>
                        <p14:section name="PPT section example" id="{376F793D-518A-4B6E-AAA6-8CD5D37CFC8B}">
                            <p14:sldIdLst>
                                <p14:sldId id="312"/>
                                <p14:sldId id="274"/>
                                <p14:sldId id="311"/>
                                <p14:sldId id="275"/>
                                <p14:sldId id="276"/>
                                <p14:sldId id="317"/>
                                <p14:sldId id="313"/>
                                <p14:sldId id="318"/>
                                <p14:sldId id="319"/>
                                <p14:sldId id="307"/>
                                <p14:sldId id="314"/>
                                <p14:sldId id="315"/>
                                <p14:sldId id="321"/>
                                <p14:sldId id="320"/>
                                <p14:sldId id="308"/>
                                <p14:sldId id="322"/>
                                <p14:sldId id="303"/>
                                <p14:sldId id="264"/>
                                <p14:sldId id="300"/>
                                <p14:sldId id="287"/>
                                <p14:sldId id="309"/>
                                <p14:sldId id="289"/>
                            </p14:sldIdLst>
                        </p14:section>
                    </p14:sectionLst>
                </p:ext> (...more contents not relevant I guess ...)

Does anyone know how to create a section in Apache POI and append slides to it? How to append multiple section and single slides after that? Any help appreciated.

Ok So I managed the whole thing :) You need to create a default section like this:

    CTExtensionList extensionsList = ppt.getCTPresentation().getExtLst();//create default section            
    CTExtension extension = extensionsList.insertNewExt(0);
    extension.setUri("{521415D9-36F7-43E2-AB2F-B90AF26B5E84}");
    Node ext = extension.getDomNode();
    Element sectionLst = ext.getOwnerDocument().createElementNS(p14Xmlns, "p14:sectionLst");
    sectionLst.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:p14", p14Xmlns);
    ext.appendChild(sectionLst);
    Element section = sectionLst.getOwnerDocument().createElementNS(p14Xmlns, "p14:section");
    section.setAttribute("name", "Default section");
    section.setAttribute("id", "{" + UUID.randomUUID().toString().toUpperCase() + "}");
    Element sldIdLst = section.getOwnerDocument().createElementNS(p14Xmlns, "p14:sldIdLst");
    section.appendChild(sldIdLst);
    sectionLst.appendChild(section);
    return sldIdLst;

Then you can use this method to create additional sections if needed:

private Element createSection(Element sldIdLst, Node parent) { String sectionName = ""; if (parent.getAttributes() != null && parent.getAttributes().getNamedItem("title") != null) { sectionName = parent.getAttributes().getNamedItem("title").getNodeValue(); } Node sectionLst = sldIdLst.getParentNode().getParentNode();

    Element section = sectionLst.getOwnerDocument().createElementNS(p14Xmlns, "p14:section");
    section.setAttribute("name", sectionName);
    section.setAttribute("id", "{" + UUID.randomUUID().toString().toUpperCase() + "}");
    Element newSldIdLst = section.getOwnerDocument().createElementNS(p14Xmlns, "p14:sldIdLst");
    section.appendChild(newSldIdLst);
    sectionLst.appendChild(section);
    sldIdLst = newSldIdLst;
    return sldIdLst;
}

Then I had to manually relate the XSLSlide to the sldIdLst liek this:

 XSLFSlide slide = createSlideFromXml(parent, ppt);
        if (slide != null)
        {
            List<CTSlideIdListEntry> sldIdList = ppt.getCTPresentation().getSldIdLst().getSldIdList();
            long slideInternalId = 0;
            for (CTSlideIdListEntry entry : sldIdList)
            {
                if (entry.getId2().equals(slide.getPackageRelationship().getId()))
                {
                    slideInternalId = entry.getId();
                    break;
                }
            }
            Element sldId = sldIdLst.getOwnerDocument().createElementNS("http://schemas.microsoft.com/office/powerpoint/2010/main", "p14:sldId");
            sldId.setAttribute("id", String.valueOf(slideInternalId));
            sldIdLst.appendChild(sldId);
        }

I hope you will get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM