Setting style on a paragraph using ODF toolkit

霸气de小男生 提交于 2019-12-12 04:49:00

问题


I'm trying to generate a well structured OpenDocument Text file with Apache's ODF tookit. I hope to achieve this by using styles for different portions of data. So I generated a template file that contains all of the styles I wish to use.

My next step was to try to use the Simple ODF API to setup my document. Apparently this is the recommended way to do this. For testing purposes I decided to keep things simple. So right now I am just trying to give one paragraph a predefined style.

Here's the code I wrote:

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

}

However, this doesn't seem to work. The 'Blah.' paragraph I added shows up with the default style. It looks as if a lot has changed in the last couple of releases so documentation is rather scarce.

Is what I want possible using the Simple ODF API? Or should I look into the actual ODFDOM API? If that's the case a code snippet for that would be much appreciated.

Thanks.


回答1:


I found a workaround by doing the following:

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

I am convinced that this is a bug, and the code from the original question should actually work. I have therefore filed a bug report that can be found here. From the response so far I gather that my assumption that the code in the original example should work is correct.



来源:https://stackoverflow.com/questions/26574174/setting-style-on-a-paragraph-using-odf-toolkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!