Page 1 of 1

Deleting a Slide (revisited)

PostPosted: Tue Sep 03, 2013 5:57 am
by jpallen
Hello,

I followed the recommendations in this thread: http://www.docx4java.org/forums/pptx-java-f14/delete-a-slide-t1070.html
but am getting errors when loading the resulting ppt.

As suggested in the link above, I perform the following steps:
Code: Select all
       MainPresentationPart presentation = ppt.getMainPresentationPart();

      Part slidePart = ppt.getParts().get(new PartName("/ppt/slides/slide" + slideNum + ".xml"));
      // remove slide from slide list[/color]
      SldIdLst sldIdLst = presentation.getJaxbElement().getSldIdLst();
      sldIdLst.getSldId().remove(slideNum);
      // remove relationship from presentation[/color]
      presentation.getRelationshipsPart().removeRelationship(slidePart.getPartName());
      // remove part from package[/color]
      ppt.getPackage().getParts().remove(slidePart.getPartName());
      ppt.getParts().remove(slidePart.getPartName());


Additionally, the last line winds up being redundant as the line before it seems to be completely removing the part (according to the log).
Any ideas what I'm missing here?

Thanks, Folks.
Jim

Re: Deleting a Slide (revisited)

PostPosted: Tue Sep 03, 2013 9:56 pm
by jason
If you'd like to email me your input pptx, and the value of slideNum, I'll take a look.

Re: Deleting a Slide (revisited)

PostPosted: Wed Sep 04, 2013 10:02 am
by jason
The most likely problem is a mismatch between the slide you are deleting (by name slideNum), and the entry in sldIdLst.

For example, if the first slide is slide1.xml, sldIdLst.getSldId().remove(1) would actually remove the second slide (since indexes are 0-based). This would corrupt the presentation.

Beyond that, the code is brittle, since it assumes slides are named and ordered slide1..N. If you add/delete/move slides around, this assumption gets challenged.

Instead, you should be keying everything off the relId (r:id), which appears in each list entry:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <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

Re: Deleting a Slide (revisited)

PostPosted: Thu Sep 05, 2013 2:36 am
by jpallen
Thanks for the info, Jason.

I understand where you are coming from, and would prefer to work off of slide Ids. However, I don't understand how to determine the relId for a given slide. Looking at the XML for a slide I didn't see a relId attribute/element anywhere -- perhaps, I just missed it?

So, how do I determine the relId for a given slide?

Sorry for the pptx4j-101 course question.

Thanks,
Jim

Re: Deleting a Slide (revisited)

PostPosted: Thu Sep 05, 2013 11:20 am
by jason
https://github.com/plutext/docx4j/commi ... f2121ee128 adds convenience methods to add and remove slides.

It'll make its way into a nightly in the next 24 hours.

It allows you to remove a slide by index (eg the 4th slide - though the index is zero-based), or by the slide's relationship in the main pres part (mpp).

That doesn't answer your question directly though.

If you have a reference to the slidePart, you can use:

Relationship rel = slidePart.getSourceRelationships().get(0);
mpp.removeSlide(rel);

If you have its name, you can use mpp.getRelationshipsPart().getRel(partName) etc

Re: Deleting a Slide (revisited)

PostPosted: Thu Sep 05, 2013 9:25 pm
by jason

Re: Deleting a Slide (revisited)

PostPosted: Sat Sep 14, 2013 1:50 am
by jpallen
Jason,

The new convenience "delete" method works like a champ.

For posterity here's a complete slide delete method using the nightly build:

Code: Select all
private void deleteSlide(PresentationMLPackage ppt, int slideNum) {
      MainPresentationPart presentation = ppt.getMainPresentationPart();
      Part slidePart = null;
      try {
         slidePart = ppt.getParts().get(
               new PartName("/ppt/slides/slide" + slideNum + ".xml"));
      } catch (InvalidFormatException e) {
         e.printStackTrace();
      }

      Relationship rel = slidePart.getSourceRelationships().get(0);
      try {
         presentation.removeSlide(rel);
      } catch (Pptx4jException e) {
         e.printStackTrace();
      }
   }


Thanks again for the help.

Jim