- Start NetBeans IDE
- Create a new project by calling "File" > "New Project..."
- Right-click with the mouse on the brand new project name and select "Properties" from the context menu
- Select in the Project Properties from Categories the categorie "Libraries" and there the tab "Compile"
- Press the button "Add JAR/Folder" and locate some of OOo's JAR files from OOo's installation directory which is on Windows for example "c:\program files\openoffice.org 2.3\program\classes". Add at least juh.jar, jurt.jar, ridl.jar and unoil.jar. Adding these JAR files exactly here in your Project Properties is very important!
- Create a new class containing the following code:
Code: Select all
package oootest;
import com.sun.star.beans.PropertyValue;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public class BootstrapConnectionOdtToPdfQuickAndDirty {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String loadUrl="file:///c:/dev/netbeans/oootest/mydoc.odt";
String storeUrl="file:///c:/dev/netbeans/oootest/mydoc.pdf";
try {
XComponentContext xContext = Bootstrap.bootstrap();
XMultiComponentFactory xMultiComponentFactory = xContext.getServiceManager();
XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xContext));
Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(loadUrl, "_blank", 0, new PropertyValue[0]);
PropertyValue[] conversionProperties = new PropertyValue[1];
conversionProperties[0] = new PropertyValue();
conversionProperties[0].Name = "FilterName";
conversionProperties[0].Value = "writer_pdf_Export";
XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,objectDocumentToStore);
xstorable.storeToURL(storeUrl,conversionProperties);
}
catch (java.lang.Exception e) {
e.printStackTrace();
}
finally {
System.exit(0);
}
}
}
Besides establishing the connection to OOo the only useful task this code performs is converting a document from ODT to PDF.
If you want to convert the document into a different format, you have to change the FilterName of the conversionProperties. For example to convert a document to DOC change
Code: Select all
conversionProperties[0].Value = "writer_pdf_Export";
Code: Select all
conversionProperties[0].Value = "MS WinWord 6.0";