{"id":691,"date":"2010-06-29T06:35:56","date_gmt":"2010-06-29T04:35:56","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/2010\/06\/simple-openoffice-templating-with-the-odf-toolkit\/"},"modified":"2010-06-29T06:35:56","modified_gmt":"2010-06-29T04:35:56","slug":"simple-openoffice-templating-with-the-odf-toolkit","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2010\/06\/simple-openoffice-templating-with-the-odf-toolkit\/","title":{"rendered":"Simple OpenOffice templating with the ODF toolkit"},"content":{"rendered":"<p>To open an OpenOffice text document and access the text you first have to download odfdom-0.8.5-binaries.zip (<a href=\"http:\/\/odftoolkit.org\/\">Homepage of ODF TOOLKIT<\/a>) and add odfdom.jar to your classpath together with a recent Xerces implementation like xercesImpl-2.9.0.jar. Then you can start with Java:<\/p>\n<p>File file = new File( &quot;C:\/Users\/Christian\/Desktop\/TEMPLATE.odt&quot; );   <br \/>OdfTextDocument odf = (OdfTextDocument) OdfTextDocument.loadDocument( file );    <br \/>OdfOfficeText contentRoot = odf.getContentRoot();<\/p>\n<p>The OdfOfficeText gives access to the XML-structure of the document. If you print it out<\/p>\n<p>System.out.println( contentRoot );<\/p>\n<p>it looks like this:<\/p>\n<p>&lt;office:text&gt;   <br \/>&#160;&#160;&#160; &lt;office:forms form:apply-design-mode=&quot;false&quot;    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; form:automatic-focus=&quot;false&quot;&gt;&lt;\/office:forms&gt;    <br \/>&#160;&#160;&#160; &lt;text:variable-decls&gt;<\/p>\n<p>\u2026<\/p>\n<p>&#160;&#160;&#160; &lt;\/text:p&gt;   <br \/>&lt;\/office:text&gt;<\/p>\n<p>With regular XML-DOM operations you can now modify the document.<\/p>\n<p>To use a template you have to a) find a certain element you want to fill b) modify it and c) save the document.<\/p>\n<p>a) For templates you can use user variables. If you set a variable it looks like this in XML:<\/p>\n<p>&lt;text:variable-set office:value-type=&quot;string&quot; text:name=&quot;COURSETITLE&quot;&gt;COURSETITLE&lt;\/text:variable-set&gt; <\/p>\n<p>(Date and other formats look different!)<\/p>\n<p>b) After your found the XML element either by hand or XPath, you can modify an entry with setNodeValue() or different method if the value is stored in attributes.<\/p>\n<p>c) Save the modified document with odf.save( file );<\/p>\n<p>A simple but complete example to read a document, set variables and write the modified ODT:<\/p>\n<p>import java.io.File;   <br \/>import java.util.*;    <br \/>import org.odftoolkit.odfdom.doc.OdfTextDocument;    <br \/>import org.odftoolkit.odfdom.doc.office.OdfOfficeText;    <br \/>import org.w3c.dom.*; <\/p>\n<p>public class OdtTemplate   <br \/>{    <br \/>&#160; private OdfTextDocument odf;    <br \/>&#160; private Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); <\/p>\n<p>&#160; public static void main( String[] args ) throws Exception   <br \/>&#160; {    <br \/>&#160;&#160;&#160; File file1 = new File( &quot;C:\/Users\/Christian\/Desktop\/TEMPLATE.odt&quot; );    <br \/>&#160;&#160;&#160; File file2 = new File( &quot;C:\/Users\/Christian\/Desktop\/result.odt&quot; ); <\/p>\n<p>&#160;&#160;&#160; OdtTemplate template = new OdtTemplate();   <br \/>&#160;&#160;&#160; template.readOdt( file1 );    <br \/>&#160;&#160;&#160; template.setVariable( &quot;COURSETITLE&quot;, &quot;Donuts backen f\u00fcr Kernkraftfahrer&quot; );    <br \/>&#160;&#160;&#160; template.setVariable( &quot;COURSECODE&quot;, &quot;DONUT&quot; );    <br \/>&#160;&#160;&#160; template.setVariable( &quot;CREATOR&quot;, &quot;ull&quot; );    <br \/>&#160;&#160;&#160; template.saveOdt( file2 );    <br \/>&#160; } <\/p>\n<p>&#160; public void readOdt( File file ) throws Exception   <br \/>&#160; {    <br \/>&#160;&#160;&#160; odf = (OdfTextDocument) OdfTextDocument.loadDocument( file );    <br \/>&#160; } <\/p>\n<p>&#160; public void setVariable( String key, String value )   <br \/>&#160; {    <br \/>&#160;&#160;&#160; map.put( key, value );    <br \/>&#160; } <\/p>\n<p>&#160; public void saveOdt( File file ) throws Exception   <br \/>&#160; {    <br \/>&#160;&#160;&#160; OdfOfficeText contentRoot = odf.getContentRoot();    <br \/>&#160;&#160;&#160; iteratorOverEveryVariableSet( contentRoot.getChildNodes() );    <br \/>&#160;&#160;&#160; odf.save( file );    <br \/>&#160; } <\/p>\n<p>&#160; private void iteratorOverEveryVariableSet( NodeList childNodes )   <br \/>&#160; {    <br \/>&#160;&#160;&#160; for ( int i = 0; i &lt; childNodes.getLength(); i++ )    <br \/>&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160; Node item = childNodes.item( i ); <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; if ( item instanceof NodeList )   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; iteratorOverEveryVariableSet( item.getChildNodes() ); <\/p>\n<p>&#160;&#160;&#160;&#160;&#160; if ( item.getNodeName().equals( &quot;text:variable-set&quot; ) )   <br \/>&#160;&#160;&#160;&#160;&#160; {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; String nodeValue = item.getAttributes().getNamedItem( &quot;text:name&quot; ).getNodeValue();    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; item.getChildNodes().item( 0 ).setNodeValue( map.get( nodeValue ) );    <br \/>&#160;&#160;&#160;&#160;&#160; }    <br \/>&#160;&#160;&#160; }    <br \/>&#160; }    <br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To open an OpenOffice text document and access the text you first have to download odfdom-0.8.5-binaries.zip (Homepage of ODF TOOLKIT) and add odfdom.jar to your classpath together with a recent Xerces implementation like xercesImpl-2.9.0.jar. Then you can start with Java: File file = new File( &quot;C:\/Users\/Christian\/Desktop\/TEMPLATE.odt&quot; ); OdfTextDocument odf = (OdfTextDocument) OdfTextDocument.loadDocument( file ); OdfOfficeText [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[4],"tags":[],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-open-source"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=691"}],"version-history":[{"count":0,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/691\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}