You have done a wonderful job, please keep going!!
Does anyone have any additional example code of adding simple shapes and text to a slide and then adding that slide to a presentation package??
I get the example that is provided with the code to work just fine. But it is a bit cumbersome to figure out exactly what all needs to be added explicitly versus relying on defaults in the docx4j library.
Below is what I have done this afternoon to add a single shape to the existing slide and shape in the example.
It saves out the presentation well enough, but I am still not quite to the point where PowerPoint 2007 will successfully open and show the shapes. ATM, I just get a blank slide and the message about the file being corrupted (meaning that something is still missing).
Thanks for any suggestions in advance!!
- Code: Select all
public static void make() throws Exception {
// Where will we save our new .ppxt?
String outputfilepath = System.getProperty("user.dir") + "/sample-docs/pptx-ags-test2.pptx";
// Create skeletal package
PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
// It contains a first slide; get it ..
// TODO - add convenience methods?
SlidePart slidePart = (SlidePart) presentationMLPackage.getParts().getParts().get(
new PartName("/ppt/slides/slide1.xml"));
// Create and add shape
Shape sample = ((Shape) XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML));
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
// Add new shape to slide from scratch ...
List<Object> sldPart = slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame();
// create a new shape and set its properties and contents
Shape scratch = new Shape();
scratch.setNvSpPr(new NvSpPr());
scratch.getNvSpPr().setCNvPr(new CTNonVisualDrawingProps());
scratch.getNvSpPr().getCNvPr().setId(2);
scratch.getNvSpPr().getCNvPr().setName("roundrect2");
scratch.getNvSpPr().setCNvSpPr(new CTNonVisualDrawingShapeProps());
scratch.getNvSpPr().setNvPr(new NvPr());
CTShapeProperties ctSP = new CTShapeProperties();
CTPresetGeometry2D prg = new CTPresetGeometry2D();
prg.setPrst(STShapeType.ROUND_RECT);
prg.setAvLst(new CTGeomGuideList());
ctSP.setPrstGeom(prg);
ctSP.setXfrm(new CTTransform2D());
ctSP.getXfrm().setOff(new CTPoint2D());
ctSP.getXfrm().getOff().setX(1255000);
ctSP.getXfrm().getOff().setY(1255000);
ctSP.getXfrm().setExt(new CTPositiveSize2D());
ctSP.getXfrm().getExt().setCx(1155000);
ctSP.getXfrm().getExt().setCy(1155000);
ctSP.setNoFill(new CTNoFillProperties());
scratch.setSpPr(ctSP);
CTTextBody ctTB = new CTTextBody();
ctTB.setLstStyle(new CTTextListStyle());
List<CTTextParagraph> p = ctTB.getP();
CTTextParagraph ctTP = new CTTextParagraph();
List<Object> eGTextRun = ctTP.getEGTextRun();
eGTextRun.add("Example text run in round rect");
scratch.setTxBody(ctTB);
scratch.setStyle(new CTShapeStyle());
scratch.getStyle().setLnRef(new CTStyleMatrixReference());
scratch.getStyle().getLnRef().setSchemeClr(new CTSchemeColor());
scratch.getStyle().setFillRef(new CTStyleMatrixReference());
sldPart.add(scratch);
// All done: save it
presentationMLPackage.save(new java.io.File(outputfilepath));
System.out.println("\n\n done .. saved " + outputfilepath);
}
private static String SAMPLE_SHAPE =
"<p:sp xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">" +
"<p:nvSpPr>" +
"<p:cNvPr id=\"4\" name=\"Title 3\" />" +
"<p:cNvSpPr>" +
"<a:spLocks noGrp=\"1\" />" +
"</p:cNvSpPr>" +
"<p:nvPr>" +
"<p:ph type=\"title\" />" +
"</p:nvPr>" +
"</p:nvSpPr>" +
"<p:spPr />" +
"<p:txBody>" +
"<a:bodyPr />" +
"<a:lstStyle />" +
"<a:p>" +
"<a:r>" +
"<a:rPr lang=\"en-US\" smtClean=\"0\" />" +
"<a:t>Hello World</a:t>" +
"</a:r>" +
"<a:endParaRPr lang=\"en-US\" />" +
"</a:p>" +
"</p:txBody>" +
"</p:sp>";
}