正如尤金評論,我必須找到目標框架和做更多的工作。沒有辦法將圖像添加到框架,只能添加到幻燈片。我走進的方法和成功如下:
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,因爲我覺得我已經錯過了一些風格和更好的方式來找到幀。但無論如何,這並不壞。
至少你可以得到幻燈片的frame容器元素,嘗試遍歷它的所有子項並手動檢查它們的類和屬性以找到目標幀。 –