Page 1 of 1

Adding a slide to template based output.

PostPosted: Fri Jun 01, 2012 10:38 pm
by SmithaRSanthosh
I have a pptx template which I will use to create an output pptx. When creating from an existing template is it possible to add a slide ? I tried using the code to create a slide part and tried to add Shape objects but there was some relationship conflict which I could not resolve.
This is the sample code that I used. I have a template which has a slide . I created a new slide part and tried to add the previous slide contents to the new slide. But the output is corrupted. On examining the output I see that there is only one slide.xml in the slides folder.

Code: Select all
String inputfilepath = "Inputs\\S1.pptx";
         PresentationMLPackage pMLPackage =
            (PresentationMLPackage)OpcPackage.load(new java.io.File(inputfilepath));

            SlidePart slide = (SlidePart)pMLPackage.getParts().get(new PartName("/ppt/slides/slide1.xml") );
            
            Sld  sld = slide.getJaxbElement();
            CommonSlideData csld = sld.getCSld();
            GroupShape gsp = csld.getSpTree();
            List<Object> lstObjs = gsp.getSpOrGrpSpOrGraphicFrame();
            
            for (Object o2 : lstObjs ){
               Shape objShape =  (Shape)o2;
                CTTextBody ctText = objShape.getTxBody();
                List<CTTextParagraph> CtParas = ctText.getP();
                for (CTTextParagraph ctPara : CtParas){
                   List<Object> txtRuns = ctPara.getEGTextRun();
                   for(Object o1 : txtRuns){
                      CTRegularTextRun ctr = (CTRegularTextRun)o1;
                       ctr.setT("TESTING SUCCESS 3");
                   }
                }
            }
            MainPresentationPart pp = (MainPresentationPart)pMLPackage.getParts().getParts().get(
                  new PartName("/ppt/presentation.xml"));
            
            SlideLayoutPart layoutPart = (SlideLayoutPart)pMLPackage.getParts().getParts().get(
                  new PartName("/ppt/slideLayouts/slideLayout1.xml"));
            SlidePart slidePart = pMLPackage.createSlidePart(pp, layoutPart,
                                 new PartName("/ppt/slides/slide1.xml"));
            
            for (Object o2 : lstObjs ){
               Shape objShape =  (Shape)o2;
                CTTextBody ctText = objShape.getTxBody();
                List<CTTextParagraph> CtParas = ctText.getP();
                for (CTTextParagraph ctPara : CtParas){
                   List<Object> txtRuns = ctPara.getEGTextRun();
                   for(Object o1 : txtRuns){
                      CTRegularTextRun ctr = (CTRegularTextRun)o1;
                      ctr.setT("TESTING SUCCESS 3");
                   }
                }
                System.out.println(slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame());
                System.out.println(o2);
                slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(objShape);
            }
            
            
            
            pMLPackage.save(new File(System.getProperty("user.dir") + "/Outputs/S2_Out.pptx"));


Re: Adding a slide to template based output.

PostPosted: Mon Jun 04, 2012 8:10 pm
by SmithaRSanthosh
I modified the code as follows , for a more simple solution . I still get a corrupted output file.

Code: Select all
public static void main(String[] args) throws Exception {
         String inputfilepath = "Inputs\\S1.pptx";
         PresentationMLPackage pMLPackage =
            (PresentationMLPackage)OpcPackage.load(new java.io.File(inputfilepath));

            SlidePart slide = (SlidePart)pMLPackage.getParts().get(new PartName("/ppt/slides/slide1.xml") );
            
            Sld  sld = slide.getJaxbElement();
            CommonSlideData csld = sld.getCSld();
            GroupShape gsp = csld.getSpTree();
            List<Object> lstObjs = gsp.getSpOrGrpSpOrGraphicFrame();
            
            for (Object o2 : lstObjs ){
               Shape objShape =  (Shape)o2;
                CTTextBody ctText = objShape.getTxBody();
                List<CTTextParagraph> CtParas = ctText.getP();
                for (CTTextParagraph ctPara : CtParas){
                   List<Object> txtRuns = ctPara.getEGTextRun();
                   for(Object o1 : txtRuns){
                      CTRegularTextRun ctr = (CTRegularTextRun)o1;
                       ctr.setT("TESTING SUCCESS 3");
                   }
                }
            }
//            MainPresentationPart pp = (MainPresentationPart)pMLPackage.getParts().getParts().get(
//                  new PartName("/ppt/presentation.xml"));
//            
//            SlideLayoutPart layoutPart = (SlideLayoutPart)pMLPackage.getParts().getParts().get(
//                  new PartName("/ppt/slideLayouts/slideLayout2.xml"));
//            SlidePart slidePart = pMLPackage.createSlidePart(pp, layoutPart,
//                                 new PartName("/ppt/slides/slide2.xml"));
//            
//            for (Object o2 : lstObjs ){
//               Shape objShape =  (Shape)o2;
//                CTTextBody ctText = objShape.getTxBody();
//                List<CTTextParagraph> CtParas = ctText.getP();
//                for (CTTextParagraph ctPara : CtParas){
//                   List<Object> txtRuns = ctPara.getEGTextRun();
//                   for(Object o1 : txtRuns){
//                      CTRegularTextRun ctr = (CTRegularTextRun)o1;
//                      ctr.setT("TESTING SUCCESS 3");
//                   }
//                }
//                System.out.println(slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame());
//                System.out.println(o2);
//                slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(objShape);
//            }
//            
            
            MainPresentationPart pp = (MainPresentationPart)pMLPackage.getParts().getParts().get(
                  new PartName("/ppt/presentation.xml"));
            SlideLayoutPart layoutPart = (SlideLayoutPart)pMLPackage.getParts().getParts().get(
                  new PartName("/ppt/slideLayouts/slideLayout1.xml"));
            SlidePart slidePart1 = pMLPackage.createSlidePart(pp, layoutPart,
                  new PartName("/ppt/slides/slide2.xml"));
                  
            // Add image part
            File file1 = new File(System.getProperty("user.dir") + "/images/TestJfeeChartImage.png" );
              BinaryPartAbstractImage imagePart1
                 = BinaryPartAbstractImage.createImagePart(pMLPackage, slidePart1, file1);
            
            // Create p:pic        
              java.util.HashMap<String, String>mappings1 = new java.util.HashMap<String, String>();
             
              mappings1.put("id1", "4");
              mappings1.put("name", "Picture 3");
              mappings1.put("descr", "TestJfeeChartImage.png");
              mappings1.put("rEmbedId", imagePart1.getSourceRelationship().getId() );
              mappings1.put("offx", Long.toString(2209800));
              mappings1.put("offy", Long.toString(2091531));
              mappings1.put("extcx", Long.toString(3581400));
              mappings1.put("extcy", Long.toString(2686050));
             
              Object o1 = org.docx4j.XmlUtils.unmarshallFromTemplate(SAMPLE_PICTURE,
                    mappings1, Context.jcPML, Pic.class ) ;       
                   
              // Add p:pic to slide
            slidePart1.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(o1);
            
            
            pMLPackage.save(new File(System.getProperty("user.dir") + "/Outputs/TemplateAdd_Out.pptx"));
      }