使用ODF工具包在段落上设置样式 [英] Setting style on a paragraph using ODF toolkit

查看:176
本文介绍了使用ODF工具包在段落上设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache的ODF takeit生成结构良好的OpenDocument文本文件.我希望通过对数据的不同部分使用样式来实现这一目标.因此,我生成了一个模板文件,其中包含我要使用的所有样式.

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.

我的下一步是尝试使用简单ODF API设置我的文档.显然,这是推荐的方法.出于测试目的,我决定使事情保持简单.所以现在我只是想给一个段落预定义样式.

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.

这是我写的代码:

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.

使用简单ODF API是否可以实现?还是应该研究实际的ODFDOM API?如果是这种情况,将非常感谢该代码段.

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.

谢谢.

推荐答案

我通过执行以下操作找到了一种解决方法:

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.

这篇关于使用ODF工具包在段落上设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆