Question

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?

  • org.odftoolkit.simple.PresentationDocument
  • org.odftoolkit.simple.presentation.Slide

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

  • .getOdfElement
  • .getFrameContainerElement

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"

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top