Add content to frames of an OpenOffice odp presentation in ODFToolkit
Asked Answered
F

1

6

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"

Flavoprotein answered 28/2, 2014 at 18:3 Comment(1)
At least you can get frame container element of the Slide, try to iterate over all its children and manually check their classes and attributes to find a target frame.Sump
F
4

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.

Flavoprotein answered 5/3, 2014 at 16:8 Comment(1)
Is it possible to render the first slide as an image using odftoolkit?Misology

© 2022 - 2024 — McMap. All rights reserved.