在ODFToolkit中向OpenOffice odp表示的帧添加内容

时间:2014-02-28 18:03:50

标签: java openoffice.org libreoffice odf odftoolkit

我想使用Open Office / Libre Office Presentation作为模板,并将文本和图像插入幻灯片中。我正在尝试使用odftoolkit。如果我有一个带框的幻灯片,它们在XML

中表示为<draw:frame>

如何访问那些放置图像?我应该使用这些课吗?

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

当我打开幻灯片时,我看到的相关方法是:

  • .getOdfElement
  • .getFrameContainerElement

但我无法看到如何在幻灯片中选择框架。当我打开XML时,我在<draw:page>下有5个框架。

具有以下属性:presentation:style-name="pr2" draw:layer="layout"

1 个答案:

答案 0 :(得分:4)

正如Eugene评论的那样,我必须找到目标框架并做更多工作。没有方法可以将图像添加到框架,只能添加到幻灯片。我进入了方法并成功如下:

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");

我希望能有更接近GUI的东西因为我觉得我错过了一些样式和更好的方法来找到帧。但无论如何它还不错。