Search code examples
javaodtopendocumentjopendocument

New line in field with jOpenDocument in ODT document


I'm trying to fill out a template from java with jOpenDocument library.

I want to start a new line in a field. The normal new line sign (\n) does not work.

When I do this manually in a document and look in the xml file (content.xml), I see why this does not work. For every new line in the field, there is a separate paragraph.

So maybe there is a way to combine multiple paragraphs in one object and then call setField() with the multiple paragraphs object. But until now I haven't found any solution.

Code Example:

EngineTemplate template = new RhinoTemplate(templateFile);
template.setField("nameField", "this is the content\nI wish to be multilined"); 

Solution

  • Basically \n are skipped by default, but you can change it by starting your field name in your template with %enc:.

    Here is explanation from org.jopendocument.dom.template.engine.Processor class documentation:

    Generates the final document content from the preprocessed template content. The behaviour of the substitution of fields can be controlled with prefixes:

    AS_STR the following expression will be substitued as a String. For an XML element its tree will be outputed.

    ENCODE the following expression will be converted to a String with toString() and then encoded using org.jopendocument.dom.OOXML.encodeWS(java.lang.String).

    OO_XML the following expression will be converted to a String with toString() and then parsed as OO XML.

    If none of these is specified, an XML element will be treated as OO XML to be grafted (only its children), else the value will simply be set as text of the field.

    And ENCODE field is declared:

    public static final String ENCODE = "%enc:";