how to create an odt file programmatically with java?
Asked Answered
S

8

26

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.

Sweetbread answered 25/1, 2011 at 8:41 Comment(0)
B
14

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.

Binette answered 25/1, 2011 at 8:46 Comment(3)
Note that JOpenDocument is GPL licensed (commercial license available) while Apache ODFDOM is (as expected) Apache licensed.Dulci
Hmm... ODF Toolkit 0.6-incubating was released June 2013, but it's not available in Maven central repositoryDulci
The ODF Toolkit project has been retired from the Apache incubator on November 27th, 2018: incubator.apache.org/projects/odftoolkit.htmlFortson
U
9

I have not tried it, but using JOpenDocument may be an option. (It seems to be a pure Java library to generate OpenDocument files.)

Ulani answered 25/1, 2011 at 8:46 Comment(3)
The problem with JOpenDocument is that it still supports primarily spreadsheets and doesn't even have full support for text.Whitson
JOpenDocument's latest release is 1.3 from Jul 2013 so it seems to be well maintained. ODFDOM seems no longer maintained, latest "release" on Jan 2012 (it's not even visible from the website) is 0.8.8-incubating.Dulci
Note that JOpenDocument is GPL licensed (commercial license available) while Apache ODFDOM is (as expected) Apache licensed.Dulci
R
3

A complement of previously given solutions would be JODReports, which allows creating office documents and reports in ODT format (from templates, composed using the LibreOffice/OpenOffice.org Writer word processor).

DocumentTemplateFactory templateFactory = new DocumentTemplateFactory();
DocumentTemplate template = templateFactory .getTemplate(new File("template.odt"));
Map data = new HashMap();
data.put("title", "Title of my doc");
data.put("picture", new RenderedImageSource(ImageIO.read(new File("/tmp/lena.png"))));
data.put("answer", "42");
//...
template.createDocument(data, new FileOutputStream("output.odt"));

Optionally the documents can then be converted to PDF, Word, RTF, etc. with JODConverter.

Edit/update

On GitHub, ansgarkonermann/jodreports-examples is a sample project using JODReports (with non-trivial formatting cases).

Recently answered 4/12, 2012 at 9:22 Comment(0)
D
2

I have written a jruby DSL for programmatically manipulating ODF documents.

https://github.com/noah/ocelot

It's not strictly java, but it aims to be much simpler to use than the ODFDOM.

Creating a hello world document is as easy as:

% cat examples/hello.rb
include OCELOT

Text::create "hello" do
  paragraph "Hello, world!"
end

There are a few more examples (including a spreadsheet example or two) here.

Dustidustie answered 21/6, 2013 at 18:2 Comment(0)
C
2

The ODF Toolkit project (code hosted at Github) is the new home of the former ODFDOM project, which was until 2018-11-27 a Apache Incubator project.

Cowbane answered 12/3, 2022 at 16:47 Comment(0)
T
1

I have been searching for an answer about this question for myself. I am working on a project for generating documents with different formats and I was in a bad need for library to generate ODT files. I finally can say the that ODFToolkit with the latest version of the simple-odf library is the answer for generating text documents. You can find the the official page here : Apache ODF Toolkit(Incubating) - Simple API Here is a page to download version 0.8.1 (the latest version of Simple API) as I didn't find the latest version at the official page, only version 0.6.1

And here you can find Apache ODF Toolkit (incubating) cookbook

Taiga answered 11/9, 2015 at 12:9 Comment(1)
The ODF Toolkit project has been retired from the Apache incubator on November 27th, 2018: incubator.apache.org/projects/odftoolkit.htmlFortson
D
0

You can try using JasperReports to generate your reports, then export it to ODS. The nice thing about this approach is

  1. you get broad support for all JasperReports output formats, e.g. PDF, XLS, HTML, etc.
  2. Jasper Studio makes it easy to design your reports
Dulci answered 1/10, 2013 at 8:36 Comment(0)
C
-1

the solution may be JODF Java API Independentsoft company. For example, if we want to create an Open Document file using this Java API we could do the following:

import com.independentsoft.office.odf.Paragraph;
import com.independentsoft.office.odf.TextDocument;

public class Example {

    public static void main(String[] args)
    {
        try
        {
            TextDocument doc = new TextDocument();

            Paragraph p1 = new Paragraph();
            p1.add("Hello World");

            doc.getBody().add(p1);

            doc.save("c:\\test\\output.odt", true);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

There are also .NET solutions for this API.

Calomel answered 10/9, 2019 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.