Page 1 of 1

delete a slide

PostPosted: Thu Apr 26, 2012 10:55 am
by oscar.novillo
Does anyone know how to delete a slide?

I've tried this, but i think i've got to delete from somewhere else.

Iterator it = pMLPackage.getParts().getParts().keySet().iterator();

PartName sl1 = null;
while (it.hasNext()) {
PartName p = (PartName) it.next();
if (p.getName().equals("/ppt/slides/slide1.xml")) {
sl1 = p;
}
}


pMLPackage.getParts().remove(sl1);

thanks

Re: delete a slide

PostPosted: Thu Apr 26, 2012 10:46 pm
by jason
As you will see if you run your pptx through PartsList, slides are referenced in presentation.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<p:presentation>

  <p:sldIdLst>
    <p:sldId id="256" r:id="rId2"/>
    <p:sldId id="257" r:id="rId3"/>
  </p:sldIdLst>

 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


so you need to remove the relevant p:sldId entry, and the corresponding relationship from MainPresentationPart's relationship part.

Part /ppt/presentation.xml is class org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart containing JaxbElement org.pptx4j.pml.Presentation

The removePart method works recursively, which is probably what you want for a docx, but isn't for a pptx (since slides reference a layout which in turn references a master, which then references all layouts, and you won't want to delete all of them).

So instead, use the lower level removeRelationship method, and remove the actual part, as you have done.

So, in outline:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                MainPresentationPart presentation = presentationMLPackage.getMainPresentationPart();
               
                // First, delete the slide
                SldIdLst sldIdLst = presentation.getJaxbElement().getSldIdLst();
                // .. etc
               
                // Second, delete the rel
                RelationshipsPart rels = presentation.getRelationshipsPart();
                rels.removeRelationship // etc
               
                // Also, delete the specified part from the package.
                getPackage().getParts().remove(partName);
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: delete a slide

PostPosted: Fri Apr 27, 2012 10:05 am
by oscar.novillo
now its working

thanks a lot,