ODFDOM Index > Quick Start

OpenDocument Format and the ODFDOM API

In this series of articles, you will learn how to use the ODFDOM Toolkit API to create documents in OpenDocument format (ODF) and extract information from ODF files.

If you’re the impatient type and want to get a really quick start with the toolkit, this tutorial is for you! In about thirty lines of code, you’ll be able to produce an OpenDocument Format text document named quick.odt that looks like this:

Document with two paragraphs and an image

Setting Up

In this tutorial, you can build and run the application from the command line. You can use a fancy IDE if you would like, but for now, let’s keep use the command line for an IDE-agnostic experience.

Since the toolkit is written in Java, you will need to have Java 11 or above installed on your system. Make sure you download the SDK (software development kit), not just the runtime version of Java.

You may download a zip file that contains the image, the source code, and all the libraries you need for this program. Specifically, you will need the ODFDOM library and Apache Xerces parser. If you download the binary file for odfdom, one of the files in this .zip file is odfdom-java-0.12.0-jar-with-dependencies.jar, which has everything we will need for our examples. (xercesImpl.jar).

Examining the Source

Ordinarily, you would put an application into a package, but, to keep things simple, this program doesn’t, so the code starts off with the import statements you need:

import org.odftoolkit.odfdom.doc.OdfTextDocument; import java.net.URI;

The OdfTextDocument class has these useful (and convenient) methods:

newTextDocument()
Creates a new text document that contains an empty paragraph.
newParagraph()
Creates a new, empty paragraph and appends it to the end of the document you’re building.
newParagraph(String content)
Creates a new paragraph with the given content and appends it to the end of the document you’re building.
addText(String content)
Appends the given content to the end of the document. If you don’t have a paragraph started, this method will create one for you.
newImage(URI location)
Puts the image at the given location at the end of the current paragraph. If you haven’t started a paragraph, this method will create one for you.
save(String path)
Saves the file at the given path. You can also give save() a File or OutputStream as its argument.

Here is all the code you need to create the document. The code is wrapped in a try/catch block in case the image file is missing or the output document can’t be created or saved. For simplicity, the path to the image file has been hard-coded in this program.

public static void main(String[] args) { OdfTextDocument outputOdt; try { outputOdt = OdfTextDocument.newTextDocument(); outputOdt.addText("I'm using the ODFDOM toolkit!"); outputOdt.newParagraph(); outputOdt.newImage(new URI("images/odf-community.jpg")); outputOdt.newParagraph("Now...it's your turn!"); outputOdt.save("quick.odt"); } catch (Exception e) { System.err.println("Unable to create output file."); System.err.println(e.getMessage()); } }

Note: Because newTextDocument() creates a file that already has an empty paragraph, the code starts with addText() rather than creating a newParagraph().

Building and Running

Wrap that code into a Class declaration (public class QuickOdt), compile it, and run it:

javac -cp lib/odfdom-java-0.12.0-jar-with-dependencies.jar QuickOdt.java java -cp lib/odfdom-java-0.12.0-jar-with-dependencies.jar:. QuickOdt

Really; that’s all there is to getting started with the ODFDOM toolkit. If you want something that looks better and has greater complexity (like tables, lists, etc.), you’ll need to use much more of the toolkit’s capabilities—but that’s what the other tutorials are for.

Getting the Files

You may download a .zip file that contains all the files and libraries used in this tutorial.

Next: Learn more about OpenDocument Format and the ODFDOM API.