Setting style on a paragraph using ODF toolkit

2019-09-06 21:08发布

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条回答
做自己的国王
2楼-- · 2019-09-06 21:44

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.

查看更多
登录 后发表回答