问题
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