Page 1 of 1

Strange issue with chart in pptx

PostPosted: Wed May 30, 2012 8:11 pm
by arahant7
I have a pptx with a slide which has a chart. I want to copy the slide (with chart) to an output pptx file.
Currently, I use the source pptx, add the new slide to it, hide the first slide and save it to a new location.

First I load the existing slide:
Code: Select all
SlidePart slide = (SlidePart)presentationMLPackage.getParts().get(
   new PartName("/ppt/slides/slide3.xml") );


Then I load the existing chart:
Code: Select all
Chart chart1Part = (Chart)presentationMLPackage.getParts().get(
        new PartName("/ppt/charts/chart1.xml"));


Create a clone of the existing slide
Code: Select all
Sld sld = XmlUtils.deepCopy(slide.getJaxbElement(), slide.getJAXBContext());
PresentationMLPackage.createSlidePart(pp, layoutPart, new PartName(slideToAddPartName));
targetSlide.setJaxbElement(sld);


Then add the chart part
Code: Select all
targetSlide.addTargetPart(chart1Part);

I have tried the above with AddPartBehaviour.RENAME_IF_NAME_EXISTS also, but that does not affect anything

The issue is strange. When I open the output file, I see dialog box which is attached. When I click OK, I can see the chart no problem, but I cannot get rid of this dialog box.
Note, also that there are other slides in my ppt but this issue crops up only for slides with charts.
Any help would be appreciated!!

Re: Strange issue with chart in pptx

PostPosted: Wed May 30, 2012 9:52 pm
by jason
addTargetPart returns a Relations, from which you can get relId, which you need to use in your cloned slide (in place of the old relId reference).

Re: Strange issue with chart in pptx

PostPosted: Wed May 30, 2012 10:30 pm
by arahant7
When I do
Code: Select all
      Relationship chartRel = targetSlide.addTargetPart(chart1Part, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
      targetSlide.relationships.addRelationship(chartRel);

I get Refusing to add another rel with id rId3. Target is ../charts/chart1.xml

I think this maybe because I am cloning a slide, the relationships also get copied?

Re: Strange issue with chart in pptx

PostPosted: Wed May 30, 2012 10:50 pm
by jason
Don't do targetSlide.relationships.addRelationship; addTargetPart has done this for you already!

Do uses the relId from chartRel on your slide (ie in its contents). If you are unsure where, post the XML here.

Re: Strange issue with chart in pptx

PostPosted: Thu May 31, 2012 4:14 pm
by arahant7
Just as an update:
The problem I was having was caused because my slide3.xml.rels was referring to a notesSlide1.xml and the notesSlide1.xml.rels was referring back to the slide3.xml.
Now when I deleted the existing slides before saving (which included slide3.xml) using the following code, the dependency in notesSlide1.xml.rels to slide3.xml was failing because slide3.xml is now removed.

Code: Select all
      //Prevent the existing slides from showing up
      for (int i=4; i>=0; i--)  {
         SldId sldId = presentationMLPackage.getMainPresentationPart().getJaxbElement().
               getSldIdLst().getSldId().get(i);
         
         presentationMLPackage.getMainPresentationPart().getJaxbElement().
               getSldIdLst().getSldId().remove(i);

         Relationship r = presentationMLPackage.getMainPresentationPart().
            relationships.getRelationshipByID(sldId.getRid());
         
         presentationMLPackage.getMainPresentationPart().relationships.removeRelationship(r);
         
         PartName partToRemove = new PartName("/ppt/slides/slide" + new Integer(i+1).toString() + ".xml");
         presentationMLPackage.getParts().remove(partToRemove);
      }


What I am wodering is if this kind of cyclic dependency is expected or is it just an error in the original file

Re: Strange issue with chart in pptx

PostPosted: Thu May 31, 2012 7:42 pm
by jason
Probably expected. pptx have complex relationships, which you can see if you run one through PartsList.

The Wouter van Vugt book referenced in Getting Started and available from http://openxmldeveloper.org/blog/b/open ... /1970.aspx explains the structure.