2014-10-26 45 views
0

我試圖用Apache的ODF grabit生成結構良好的OpenDocument文本文件。我希望通過對不同部分的數據使用樣式來實現這一點。所以我生成了一個包含我希望使用的所有樣式的模板文件。使用ODF工具包在段落上設置樣式

我的下一步是嘗試使用簡單的ODF API設置我的文檔。顯然這是推薦的方法。出於測試目的,我決定保持簡單。所以現在我只是想給一個預定義的風格。

這是我寫的代碼:

public static void main(String[] args) throws Exception { 

    TextDocument odt = TextDocument.loadDocument("template.ott"); 

    // List the paragraph styles, just to check if 'Abc' is actually there. 
    // Which it is. 
    OdfOfficeStyles styles = odt.getOrCreateDocumentStyles(); 
    for (OdfStyle e : styles.getStylesForFamily(OdfStyleFamily.Paragraph)) { 
     System.out.println(e.getStyleNameAttribute()); 
    } 

    // Create a paragraph, and give it the style 'Abc' 
    Paragraph p = odt.addParagraph("Blah."); 
    p.setStyleName("Abc"); 

    // Save the file 
    odt.save("result.odt"); 

} 

然而,這似乎並沒有工作。 'Blah'。我添加的段落顯示爲默認樣式。看起來好像在過去的幾個版本中很多都發生了變化,所以文檔相當稀少。

是我想要使用Simple ODF API的可能性嗎?或者我應該看看實際的ODFDOM API?如果是這種情況,那麼代碼片段將非常感謝。

謝謝。

回答

1

我找到了一個解決方法通過執行以下操作:

Paragraph p = odt.addParagraph("Blah."); 
p.getOdfElement().setStyleName("Abc"); 

我相信,這是一個錯誤,並從原來的問題代碼應該實際工作。因此,我提交了一個bug報告,可以找到here。從迄今爲止的迴應中我收集到,我假設原始示例中的代碼應該正常工作。

相關問題