javaopenoffice.orglibreofficeodfodftoolkit

Add content to frames of an OpenOffice odp presentation in ODFToolkit


I want to use a Open Office / Libre Office Presentation as a template and insert text and images into slides. I am trying to use odftoolkit. If I have a slide with the boxes, they are represented as <draw:frame> in the XML

How do I access those to place an image in them? Should I use these classes?

When I have a slide open the related methods I see are:

But I am not able to see how to select frames in the slide. When I open the XML I have the 5 frames under <draw:page>.

The have attributes like: presentation:style-name="pr2" draw:layer="layout"


Solution

  • As Eugene commented, I had to find the target frame and do more work. There is no method to add images to a frame, only to a slide. I went into the methods and succeeded as follow:

    DrawPageElement drawPageElement = slide.getOdfElement();
    DrawFrameElement drawFrame = OdfElement.findFirstChildNode(DrawFrameElement.class, drawPageElement);
    DrawImageElement image = drawFrame.newDrawImageElement();
    OdfPackage mOdfPackage = odp.getPackage();
    String imageRef = "/some/path/to/chart.png";
    
    String packagePath = odp.getDocumentPath() + OdfPackage.OdfFile.IMAGE_DIRECTORY.getPath() + "/" + someMethodToCreateRandomString();
    
    mOdfPackage.insert(new URI(imageRef), packagePath, OdfFileEntry.getMediaTypeString(imageRef));
    packagePath = packagePath.replaceFirst(odp.getDocumentPath(), "");
    URI uri = new URI(AnyURI.encodePath(packagePath).toString());
    image.setXlinkHrefAttribute(AnyURI.decodePath(uri.toString()));
    image.setXlinkActuateAttribute("onLoad");
    image.setXlinkShowAttribute("embed");
    image.setXlinkTypeAttribute("simple");
    

    I was hoping for something closer to the GUI because I think I have missed some styles and better way to find frames. But anyway it is not bad.