Java Open Office (INSTANT | 2027)

import com.sun.star.text.XTextDocument; import com.sun.star.text.XText; import com.sun.star.text.XTextRange; XDesktop desktop = getDesktop(context); XComponent document = desktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, new com.sun.star.beans.PropertyValue[0] ); XTextDocument textDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, document); XText text = textDoc.getText(); XTextRange cursor = text.getStart(); text.insertString(cursor, "Hello from Java OpenOffice API!", false); 5. Save Document import com.sun.star.frame.XStorable; import com.sun.star.beans.PropertyValue; XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); PropertyValue[] saveProps = new PropertyValue[0]; storable.storeAsURL("file:///C:/output/myfile.odt", saveProps); 6. Convert Document to PDF PropertyValue[] filterProps = new PropertyValue[2]; filterProps[0] = new PropertyValue(); filterProps[0].Name = "FilterName"; filterProps[0].Value = "writer_pdf_Export"; filterProps[1] = new PropertyValue(); filterProps[1].Name = "Overwrite"; filterProps[1].Value = true; storable.storeToURL("file:///C:/output/myfile.pdf", filterProps); 7. Open and Read an Existing Document XComponent doc = desktop.loadComponentFromURL( "file:///C:/input/myfile.odt", "_blank", 0, new PropertyValue[0] ); XTextDocument textDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, doc); String content = textDoc.getText().getString(); System.out.println(content); 8. Working with Spreadsheets (Calc) XComponent spreadsheet = desktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, new PropertyValue[0] ); XSpreadsheetDocument xssDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, spreadsheet); XSpreadsheets sheets = xssDoc.getSheets(); XSpreadsheet sheet = sheets.getByIndex(0); XCell cell = sheet.getCellByPosition(0, 0); cell.setValue(100.5); 9. Close Document and Disconnect import com.sun.star.util.XCloseable; XCloseable closeable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, document); closeable.close(false); 10. Common Issues & Tips | Problem | Solution | |---------|----------| | NoClassDefFoundError | Ensure all UNO JARs are in classpath | | Connection refused | Start OpenOffice with --accept and correct port | | Filter not found | Use correct filter name ( writer_pdf_Export , calc_pdf_Export ) | | Path errors | Use file:/// prefix with forward slashes |

public static XDesktop getDesktop(XComponentContext context) throws Exception XMultiComponentFactory mcf = context.getServiceManager(); Object desktop = mcf.createInstanceWithContext("com.sun.star.frame.Desktop", context); return (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop); java open office

Here’s a structured for working with Java and Apache OpenOffice (or LibreOffice), covering setup, document creation, reading, and conversion. 1. Introduction Apache OpenOffice provides a Universal Network Objects (UNO) API that can be accessed from Java to create, read, modify, and convert documents (Writer, Calc, Impress, Draw). 2. Setup Add UNO Dependencies You need juh.jar , jurt.jar , ridl.jar , unoil.jar from OpenOffice installation ( program/classes/ ). import com

<dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>4.1.14</version> <scope>system</scope> <systemPath>$project.basedir/lib/juh.jar</systemPath> </dependency> soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" 3. Connect to OpenOffice from Java import com.sun.star.uno.XComponentContext; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.frame.XDesktop; public class OpenOfficeConnector public static XComponentContext connect() throws Exception return Bootstrap.bootstrap(); Open and Read an Existing Document XComponent doc = desktop

Start a trial

Ignite your vision. Install ShortPoint directly on your site, or play in sandbox mode. No credit card required.

Get started today

World’s best intranet sites are designed using ShortPoint

Thousands of companies using ShortPoint everyday to design, brand and build award winning intranet sites.

Get started Learn more