2014-02-28 27 views
6

我想將Open Office /自由Office演示文稿用作模板,並將文本和圖像插入到幻燈片中。我正在嘗試使用odftoolkit。如果我有一個包含這些框的幻燈片,它們在XML中被表示爲<draw:frame>將內容添加到ODFToolkit中的OpenOffice odp演示文稿的框架

如何訪問這些文件以在其中放置圖像?我應該使用這些類嗎?

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

當我打開相關的方法我看幻燈片是:

  • .getOdfElement
  • .getFrameContainerElement

但我無法看到如何選擇幻燈片中的幀。當我打開XML時,我有<draw:page>下的5幀。

該有這樣的屬性:presentation:style-name="pr2" draw:layer="layout"

+1

至少你可以得到幻燈片的frame容器元素,嘗試遍歷它的所有子項並手動檢查它們的類和屬性以找到目標幀。 –

回答

4

正如尤金評論,我必須找到目標框架和做更多的工作。沒有辦法將圖像添加到框架,只能添加到幻燈片。我走進的方法和成功如下:

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,因爲我覺得我已經錯過了一些風格和更好的方式來找到幀。但無論如何,這並不壞。

相關問題