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"));