Lightweight ODF Library
Lightweight ODF Library (LODF) a simple to use, extremly small API to edit OpenDocument files.Open and close
Documents can be opened by calling the static method open. To save the document call save. It is save to overwrite a file used to open the document. When you are ready with modifying the document, you should call doc.close() to release resources used by LODF.Document doc = Document.open(new File("/tmp/template.odt")); doc.save(new File("/tmp/template.odt")); doc.close();
Templates
LODF was developed to fill templates. This can be done on document base or on single paragraphs by calling replaceText(pattern, replacement). The type of placeholders is not defined. You may use simple text, or for example ${NAME}, %%NAME%% or any other style you like.Document doc = Document.open(new File("/tmp/template.odt")); //Replace in the entire Document doc.replaceText("MYPATTERN", "HELLOWORLD"); //Replace in Paragraph 5 Paragraph pa = doc.getParagraphs().get(5); pa.replaceText("MYPATTERN2", "HELLO"); doc.save(new File("/tmp/out.odt")); doc.close();
Insert Pagebreak
To insert a page break simply call pageBreakAfter(Paragraph ref). The page break will be inserted after the referenced paragraph or at the end of the document if the referenced paragraph is nulldoc.pageBreakAfter(null);
Delete Paragraph
To delete a Paragraph deleteParagraph(Paragraph p) must be called.Add Paragraph
Before you can add a new Paragraph you have to create it. This can be done by calling createParagraph(). After creating the Paragraph you can insert the Paragraph to the Document by calling addParagraphAfter(Paragraph ref).Serial Letter
All major Word Processing applications support creation of serial letters. And they do it good! Unfortunately it depends very much on the used application how it is done in detail.LODF does not create serial letters as you may be used to. Instead it can create simple documents which will work with any Word Processing application. To do this you will simply open the template document as many times as needed. After you have done the required replacements you can simply append the documents to each other.
Document doc = open(new File("/tmp/serienbrief.odt")); Document doc2 = open(new File("/tmp/serienbrief.odt")); doc.replaceText("NAME", "Joe Smith"); doc.pageBreakAfter(null); doc.concat(doc2); doc2.close(); doc.save(new File("/tmp/x.odt")); doc.close();