Setting style on a paragraph using ODF toolkit simple-odf.0.8.1

924 views Asked by At

This question has been already asked by another member in octobre 2014, but the incubating simple-odf.0.8.1 does not seem to solve the problem.

I'm trying to generate an ODF text document (*.odt), applying styles to the newly generated paragraphs. When opening the generated document with LibreOffice 5, the newly generated paragraphs appear with the default style and not the wanted one.

Am I doing something wrong or is there an, as yet, not corrected bug ?

import java.io.FileOutputStream;

import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeStyles;
import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
import org.odftoolkit.simple.TextDocument;
import org.odftoolkit.simple.text.Paragraph;

public class Test {

    public static void main(String[] args) {

        try {

            // Loading template document with styles
            TextDocument doc = (TextDocument)TextDocument.loadDocument("Template.odt");

            // Checking if "MyStyle" is included
            OdfOfficeStyles styles = doc.getOrCreateDocumentStyles();
            for (OdfStyle e : styles.getStylesForFamily(OdfStyleFamily.Paragraph)) {
                System.out.println(e.getStyleNameAttribute());
            }

            // Adding a styled paragraph, first way
            Paragraph paragraph = doc.addParagraph("Text before setting style");
            paragraph.setStyleName("MyStyle");

            // adding a styled paragraph, other way just to be sure
            paragraph = doc.addParagraph("");
            paragraph.setStyleName("MyStyle");
            paragraph.setTextContent("Text after setting style");

            // Saving the result
            doc.save(new FileOutputStream("Result.odt"));
            System.out.println("saved");
        } catch (Exception ex) {
            System.err.println("ex=" + ex.getMessage());
        }
    }
}
0

There are 0 answers