Search in sources :

Example 1 with SlidePart

use of org.docx4j.openpackaging.parts.PresentationML.SlidePart in project Java-Tutorial by gpcodervn.

the class LinkToSlides method startPptx.

private static void startPptx() throws Exception {
    // generate Ppt file
    String outFileName = "output/LinkToSlides.pptx";
    PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
    MainPresentationPart pp = (MainPresentationPart) presentationMLPackage.getParts().getParts().get(new PartName("/ppt/presentation.xml"));
    SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage.getParts().getParts().get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
    // OK, now we can create a slide
    SlidePart slidePart = null;
    slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide1.xml"));
    org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory();
    // first Link relation
    org.docx4j.relationships.Relationship rel1 = factory.createRelationship();
    rel1.setType(Namespaces.PRESENTATIONML_SLIDE);
    rel1.setTarget("/ppt/slides/slide" + 5 + ".xml");
    slidePart.getRelationshipsPart().addRelationship(rel1);
    String linkedTable = table1.replaceFirst("replaceID", rel1.getId());
    // second link relation
    org.docx4j.relationships.Relationship rel2 = factory.createRelationship();
    rel2.setType(Namespaces.PRESENTATIONML_SLIDE);
    rel2.setTarget("/ppt/slides/slide" + 10 + ".xml");
    slidePart.getRelationshipsPart().addRelationship(rel2);
    linkedTable = linkedTable.replaceFirst("replaceID", rel2.getId());
    CTGraphicalObjectFrame graphicFrame2 = (CTGraphicalObjectFrame) XmlUtils.unmarshalString(linkedTable, Context.jcPML, CTGraphicalObjectFrame.class);
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(graphicFrame2);
    for (int i = 2; i < 15; i++) {
        slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide" + i + ".xml"));
        Shape sample = ((Shape) XmlUtils.unmarshalString(getTextData(i), Context.jcPML));
        slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
    }
    presentationMLPackage.save(new java.io.File(outFileName));
    System.out.println("Done");
}
Also used : CTGraphicalObjectFrame(org.pptx4j.pml.CTGraphicalObjectFrame) Shape(org.pptx4j.pml.Shape) PresentationMLPackage(org.docx4j.openpackaging.packages.PresentationMLPackage) SlideLayoutPart(org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart) SlidePart(org.docx4j.openpackaging.parts.PresentationML.SlidePart) PartName(org.docx4j.openpackaging.parts.PartName) MainPresentationPart(org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart)

Aggregations

PresentationMLPackage (org.docx4j.openpackaging.packages.PresentationMLPackage)1 PartName (org.docx4j.openpackaging.parts.PartName)1 MainPresentationPart (org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart)1 SlideLayoutPart (org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart)1 SlidePart (org.docx4j.openpackaging.parts.PresentationML.SlidePart)1 CTGraphicalObjectFrame (org.pptx4j.pml.CTGraphicalObjectFrame)1 Shape (org.pptx4j.pml.Shape)1