Bug 60042

Summary: PartAlreadyExistsException thrown when removing and adding slides.
Product: POI Reporter: John Littlewood <john.littlewood>
Component: XSLFAssignee: POI Developers List <dev>
Status: RESOLVED DUPLICATE    
Severity: normal CC: john.littlewood
Priority: P2    
Version: 3.14-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Bug Depends on:    
Bug Blocks: 60810    
Attachments: Presentation file which triggers the bug when used with the code in the description.

Description John Littlewood 2016-08-25 10:13:03 UTC
Created attachment 34172 [details]
Presentation file which triggers the bug when used with the code in the description.

I have a use case which requires taking a template presentation, deleting some slides, and adding some more. When doing this, I get the following error message when attempting to add a new slide after removing an old one:
Exception in thread "main" org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException: A part with the name '/ppt/slides/slide2.xml' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]
	at org.apache.poi.openxml4j.opc.OPCPackage.createPart(OPCPackage.java:811)
	at org.apache.poi.openxml4j.opc.OPCPackage.createPart(OPCPackage.java:775)
	at org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:536)
	at org.apache.poi.xslf.usermodel.XMLSlideShow.createSlide(XMLSlideShow.java:220)
	at org.apache.poi.xslf.usermodel.XMLSlideShow.createSlide(XMLSlideShow.java:243)
	at com.thalesgroup.uk.airscape.investigations.briefinggenerator.PresentationBugger.main(PresentationBugger.java:35)

The following code results in the exception being thrown:

public class PresentationBugger {

  public static void main(String... args) throws IOException, OpenXML4JException, XmlException {
    try (XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(new File("pres.pptx")))) {
      pptx.removeSlide(0);
      pptx.createSlide();
    }
  }
}

"pres.pptx" is a presentation (tested with PowerPoint 2010). With one slide in the presentation, the bug doesn't occur, but with two or more, it does. I've attached an example of pres.pptx which triggers the bug.
Comment 1 Javen O'Neal 2016-08-25 15:56:19 UTC
Should searching for file name (slide2.xml) be case insensitive?
https://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java?revision=1753073&view=markup#l210

Also, slideNumber may not be computed correctly if the slides are out of order.

> for (CTSlideIdListEntry slideId : slideList.getSldIdArray()){
>     slideNumber = (int)Math.max(slideId.getId() + 1, slideNumber);
>     cnt++;
> }

It would be better to store slide numbers in a set and find the largest or smallest slide number and find the next unused number.
Comment 2 Dominik Stadler 2017-03-19 20:41:19 UTC
This seems to have been fixed shortly after version 3.14 was released, see bug 55791. I tried the test-code with the attached sample and it did work fine in current trunk.

*** This bug has been marked as a duplicate of bug 55791 ***