Page 1 of 1

Adding images to a generated powerpoint document?

PostPosted: Tue May 18, 2010 11:20 am
by lokkju
Hey all, hoping someone can point me in the right direction. I'm trying to find how I can add linked imaged to a PowerPoint slide. I've written the necessary PML, but am unsure how to add the correct relationship to the file in the /ppt/media folder... ideas?

Re: Adding images to a generated powerpoint document?

PostPosted: Wed May 19, 2010 11:54 pm
by jason
Please have a look at the docx sample http://dev.plutext.org/svn/docx4j/trunk ... Image.java

You'll have to adapt it a bit, but the basic pattern (for adding the image part and a relationship pointing to it) should be the same

If you write something which could form part of pptx4j, please consider contributing it. Thanks!

.. Jason

Re: Adding images to a generated powerpoint document?

PostPosted: Thu May 20, 2010 6:06 am
by lokkju
The problem with the current docx implementation is that it depends on having a WordprocessingMLPackage passed in for all the image helper functions. I would suggest trying to move the code over to depend on a generic OpcPackage.

I eventually got it working using just the BinaryPart - here is a sample snippet:
Code: Select all
        // BinaryPart
        BinaryPart binaryPart = new BinaryPart(new PartName("/ppt/media/image" + this.imageId + ".jpeg"));
        binaryPart.setBinaryData(image);
        binaryPart.setContentType(new  org.docx4j.openpackaging.contenttype.ContentType(org.docx4j.openpackaging.contenttype.ContentTypes.IMAGE_JPEG));
        binaryPart.setRelationshipType(Namespaces.IMAGE);

        Relationship bpRelationship = slidePart.addTargetPart(binaryPart);


        Pic pic = new Pic();
        // nvPicPr
        pic.setNvPicPr(new Pic.NvPicPr());
        pic.getNvPicPr().setCNvPr(new CTNonVisualDrawingProps());
        pic.getNvPicPr().setCNvPicPr(new CTNonVisualPictureProperties());
        pic.getNvPicPr().getCNvPicPr().setPicLocks(new CTPictureLocking());
        pic.getNvPicPr().setNvPr(new NvPr());
        pic.getNvPicPr().getCNvPr().setId(this.imageId);
        pic.getNvPicPr().getCNvPr().setName(name);
        pic.getNvPicPr().getCNvPr().setDescr(description);
        pic.getNvPicPr().getCNvPicPr().getPicLocks().setNoChangeAspect(true);

        pic.setBlipFill(new CTBlipFillProperties());
        pic.getBlipFill().setBlip(new CTBlip());
        pic.getBlipFill().getBlip().setEmbed(bpRelationship.getId());
        pic.getBlipFill().getBlip().setCstate(STBlipCompression.PRINT);
        pic.getBlipFill().setStretch(new CTStretchInfoProperties());
        pic.getBlipFill().getStretch().setFillRect(new CTRelativeRect());

        pic.setSpPr(new CTShapeProperties());
        pic.getSpPr().setXfrm(new CTTransform2D());
        pic.getSpPr().getXfrm().setOff(new CTPoint2D());
        pic.getSpPr().getXfrm().getOff().setX(0);
        pic.getSpPr().getXfrm().getOff().setY(0);
        pic.getSpPr().getXfrm().setExt(new CTPositiveSize2D());
        pic.getSpPr().getXfrm().getExt().setCx(9144000);
        pic.getSpPr().getXfrm().getExt().setCy(6858000);
        pic.getSpPr().setPrstGeom(new CTPresetGeometry2D());
        pic.getSpPr().getPrstGeom().setPrst(STShapeType.RECT);
        pic.getSpPr().getPrstGeom().setAvLst(new CTGeomGuideList());
        slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(pic);
    }