Page 1 of 1

Can't add an image to a slide

PostPosted: Fri Aug 26, 2011 12:25 am
by mjrobbins
Hi there. I've been trying to make the following code work for two days now, but I'm getting nowhere. It runs, but when I open the resulting pptx it says the target slide is corrupted - what am I missing/doing wrong?

Code: Select all
// BinaryPart
        BinaryPart binaryPart = new BinaryPart(new PartName("/ppt/media/imagereport.png"));
        binaryPart.setBinaryData(bytes);
        binaryPart.setContentType(new  org.docx4j.openpackaging.contenttype.ContentType(ContentTypes.IMAGE_PNG));
        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(983894378910L);
        pic.getNvPicPr().getCNvPr().setName("Report Image");
        pic.getNvPicPr().getCNvPr().setDescr("The report");
        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);


The slide I'm adding the image and table too already contains a title and some text which I want to preserve, hence I can't just add a new slide...

I'd be immensely grateful for any help with this.

Re: Can't add an image to a slide

PostPosted: Sat Aug 27, 2011 12:37 am
by jason
For a start, uncomment the lines:

Code: Select all
//Relationship bpRelationship = slidePart.addTargetPart(binaryPart);

//pic.getBlipFill().getBlip().setEmbed(bpRelationship.getId());


You definitely need them.

Instead of using a BinaryPart to represent the image, with docx4j 2.7.0 at least, you should be able to use BinaryPartAbstractImage's createImagePart method.

Re: Can't add an image to a slide

PostPosted: Tue Nov 01, 2011 12:38 am
by mjrobbins
Thanks Jason

I managed to get this working with BinaryPart in the end. BinaryPartAbstractImage seems to only have methods for Word? Although there doesn't seem to be any 2.7 javadoc available on this site. What does BinaryPartAbstractImage give you? Does it avoid all of the boilerplate relationship stuff you have to set up in BinaryPart?

Cheers,
Martin