Using JODConverter in your own Java application is very straightforward. The following example shows the skeleton code required to perform a one off conversion from a Word document to PDF:
File inputFile = new File("document.doc"); File outputFile = new File("document.pdf"); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // close the connection connection.disconnect();
To convert from/to other formats, simply change the file names and the formats will be determined based on file extensions; e.g. to convert an Excel file to OpenDocument Spreadsheet:
File inputFile = new File("spreadsheet.xls"); File outputFile = new File("spreadsheet.ods"); converter.convert(inputFile, outputFile);
Simple, isn’t it? Yet this example actually shows almost everything you need to know for most applications.
Almost because establishing a new connection each time you need to do a conversion, while perfectly acceptable, is not the best idea from a performance point of view. If you’re integrating JODConverter in a web application for example you may want to initialise a single OpenOfficeConnection instance when the app is started and disconnect it when the app is stopped.
There are many different ways to do this depending on which web framework (if any) you’re using so I’m not going to explain it here. For plain Servlet API you can use a context listener, for Spring you can initialise the OpenOfficeConnection and DocumentConverter beans in your applicationContext.xml, and so on.