Search code examples
javajasper-reportsapache-poiodt

Convert ODT to doc/rtf format in java


I have an application which kicks out a various documents in various formats. This is it's main purpose. I am using japser for most of the non excel documents and now I realise I may have made a mistake! (I can't afford Aspose !). I am operating in a Windows environment.

Essentially many of the documents are produced in both odt and doc format. The requirement for the Word version is for doc so as to be compatible for the maximum number of users (who are outside my organisation).

For this I am exporting in rtf format to a doc suffixed file. So far so good except the tables are exported as text input boxes not tables and the users are very upset that they can't tab between cells (we missed this in testing). This seems like something that cannot be sorted out simply in report configuration. the rtf export simply does not export tables as tables.

I considered saving as docx and using poi to convert to doc but this doesn't seem so simple

  • lack of an interface between xwpf and hwpf.
  • there seems to be a bug in jasper export to docx which sets the margins to 0 and causes printing warnings. Probably surmountable but as that was a quick & dirty fix idea it is beginning to seem more effort than such a messy solution is worth.

This leaves me with the idea of converting the odt files to doc. I've had a quick look at jodconverter but it seems so long since anyone has done anything with this that I'm not convinced it is the way to go (I'm also not sure it is compatible with OpenOffice 4.1 which is what I've downloaded although I guess I could back out of that?).

So the main part of this question is - is there a reasonably simple way to convert odt to doc/rtf in java code (I don't really want the overhead of sending documents to external conversion sites). I guess an aside would be for any serious Jasper guru's - is there a way of exporting to rtf/doc format with tables as table objects rather than collections of adjacent text input boxes ?

I've tried to lay this out in full, apologies if it's a little verbose. Any help greatly appreciated (the simpler the solution the better!).


Solution

  • You can try JODConverter which does conversion between ODT and DOC(X) but there's litte documentation about this library and it's no longer maintained.

    According to the little doc there is (here and here), here's a little example to convert an odt to a doc file (you need to have OpenOffice or LibreOffice installed locally)

    OfficeManager officeManager = new DefaultOfficeManagerConfiguration().setOfficeHome(new File("<path to office home>")).buildOfficeManager();
    officeManager.start();
    
    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
    converter.convert(new File("test.odt"), new File("test.doc"));
    
    officeManager.stop();
    

    <path to office home> must be replaced with the path to the home directory of LibreOffice or OpenOffice (the directory that contains the soffice.bin file).