jOpenDocument


A pure Java library for OASIS Open Document files manipulation

Add content to a document

With jOpenDocument you can create a complete document from an empty page. We provide a high level API to create the elements of your document.

 

The complete source code (provided in a packaged eclipse project) is available in the benchmark section.

 

The key class is ODSingleXMLDocument, it represents a document with content and style. The easy way to get one is to use an empty document from OpenOffice, and load it :

  ODPackage p = new ODPackage(new File("styles.odt"))
  ODSingleXMLDocument doc = p.toSingle();

To create a paragraph or a heading, we provide the classes: Paragraph and Heading.

In this example the style of the heading is defined in the styles.odt file, we just need to reference it from its name. The paragraph is created the same way. After creating and configuring the Paragrah and Heading, we can add them to the ODSingleXMLDocument.

 

  final Heading heading = new Heading();
  heading.setStyle("Movie_20_Heading");
  heading.addContent("Here is my title");
  doc.add(heading);

  final Paragraph paragraph = new Paragraph();
  paragraph.setStyle("Synopsis_20_Para");
  paragraph.addContent("Here is my paragraph")
  doc.add(paragraph);

The last step in done by calling saveAs on our ODPackage.