Setting style on a paragraph using ODF toolkit

2.3k views Asked by At

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

There are 1 answers

0
StanB123 On BEST ANSWER

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.