How can I create an odt (LibreOffice/OpenOffice Writer) file with Java programmatically? A "hello world" example will be sufficient. I looked at the OpenOffice website but the documentation wasn't clear.
The ODT extension designates text files that are based on the OASIS Open Document Format, or ODF for short. They are primarily used as a standard file format in the license-free office packages LibreOffice and OpenOffice.
You can open an ODP file with various presentation programs, including OpenOffice Impress (cross-platform), Microsoft PowerPoint (cross-platform), LibreOffice (cross-platform), and Google Slides (Web, iOS, Android, Chrome OS). You can also convert your ODP file to a PPTX file in all those programs.
Take a look at ODFDOM - the OpenDocument API
ODFDOM is a free OpenDocument Format (ODF) library. Its purpose is to provide an easy common way to create, access and manipulate ODF files, without requiring detailed knowledge of the ODF specification. It is designed to provide the ODF developer community with an easy lightwork programming API portable to any object-oriented language.
The current reference implementation is written in Java.
// Create a text document from a standard template (empty documents within the JAR) OdfTextDocument odt = OdfTextDocument.newTextDocument(); // Append text to the end of the document. odt.addText("This is my very first ODF test"); // Save document odt.save("MyFilename.odt");
later
As of this writing (2016-02), we are told that these classes are deprecated... big time, and the OdfTextDocument
API documentation tells you:
As of release 0.8.8, replaced by org.odftoolkit.simple.TextDocument in Simple API.
This means you still include the same active .jar file in your project, simple-odf-0.8.1-incubating-jar-with-dependencies.jar
, but you want to be unpacking the following .jar to get the documentation: simple-odf-0.8.1-incubating-javadoc.jar
, rather than odfdom-java-0.8.10-incubating-javadoc.jar
.
Incidentally, the documentation link downloads a bunch of jar files inside a .zip which says "0.6.1"... but most of the stuff inside appears to be more like 0.8.1. I have no idea why they say "as of 0.8.8" in the documentation for the "deprecated" classes: just about everything is already marked deprecated.
The equivalent simple code to the above is then:
odt_doc = org.odftoolkit.simple.TextDocument.newTextDocument() para = odt_doc.getParagraphByIndex( 0, False ) para.appendTextContent( 'stuff and nonsense' ) odt_doc.save( 'mySpankingNewFile.odt' )
PS am using Jython, but the Java should be obvious.
I have not tried it, but using JOpenDocument may be an option. (It seems to be a pure Java library to generate OpenDocument files.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With