import java.io.File; import com.sun.star.beans.XPropertySet; import com.sun.star.bridge.XBridge; import com.sun.star.bridge.XBridgeFactory; import com.sun.star.comp.bridgefactory.BridgeFactory; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.connection.Connector; import com.sun.star.connection.XConnection; import com.sun.star.connection.XConnector; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XDesktop; import com.sun.star.frame.XStorable2; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.sheet.XCellAddressable; import com.sun.star.sheet.XSheetAnnotationAnchor; import com.sun.star.sheet.XSheetAnnotations; import com.sun.star.sheet.XSheetAnnotationsSupplier; import com.sun.star.sheet.XSpreadsheet; import com.sun.star.sheet.XSpreadsheetDocument; import com.sun.star.table.CellAddress; import com.sun.star.table.XCell; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class AnnotationsNotSavedTest { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 2002; String unoUrl = "socket,host=" + host + ",port=" + port; // Bootstrap initial component context XComponentContext xLocalContext = Bootstrap.createInitialComponentContext(null); // Create connector using initial component context XConnector xConnector = Connector.create(xLocalContext); // Use connector to create a connection XConnection connection = xConnector.connect(unoUrl); // Create bridge factory Object bridgeFactory = xLocalContext.getServiceManager().createInstanceWithContext(BridgeFactory.__serviceName, xLocalContext); XBridgeFactory xBridgeFactory = query_XBridgeFactory(bridgeFactory); // Use bridge factory to create a bridge XBridge xBridge = xBridgeFactory.createBridge("", "urp", connection, null); // Use bridge to obtain remote service manager Object o = xBridge.getInstance("StarOffice.ServiceManager"); XMultiComponentFactory xRemoteServiceManager = query_XMultiComponentFactory(o); // Use remote service manager to obtain remote context XPropertySet xPropertySet = query_XPropertySet(xRemoteServiceManager); Object context = xPropertySet.getPropertyValue("DefaultContext"); XComponentContext xRemoteContext = query_XComponentContext(context); // User remote service manager to obtain desktop service Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext); XDesktop xDesktop = query_XDesktop(desktop); // Get XComponentLoader interface to the desktop XComponentLoader desktopComponentLoader = query_XComponentLoader(xDesktop); // Use the desktop to open a blank spreadsheet document XComponent docComponent = desktopComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, null); XSpreadsheetDocument doc = query_XSpreadsheetDocument(docComponent); System.out.println("Created spreadsheet document"); // Add annotations to all three sheets for (int sheetNum=1; sheetNum<=3; sheetNum++) { String sheetName = "Sheet" + sheetNum; XSpreadsheet sheet = query_XSpreadsheet(doc.getSheets().getByName(sheetName)); System.out.println("Got sheet " + sheetName); // Get the annotations for this sheet XSheetAnnotationsSupplier annsSupplier = query_XSheetAnnotationsSupplier(sheet); XSheetAnnotations annotations = annsSupplier.getAnnotations(); System.out.println("Old number of annotations: " + annotations.getCount()); for (int row=0; row<10; row++) { for (int column=0; column<10; column++) { XCell cell = sheet.getCellByPosition(column, row); XCellAddressable cellAddressable = query_XCellAddressable(cell); CellAddress cellAddress = cellAddressable.getCellAddress(); String note = "Sheet " + sheetNum + ", row " + row + ", column " + column; annotations.insertNew(cellAddress, note); } } System.out.println("New number of annotations: " + annotations.getCount()); } // Save the spreadsheet document File file = new File("C:/test.ods"); XStorable2 docStorable = query_XStorable2(doc); docStorable.storeAsURL(file.toURL().toString(), null); System.out.println("Document saved"); // Close the bridge XComponent comp = query_XComponent(xBridge); comp.dispose(); } public static XBridgeFactory query_XBridgeFactory(Object object) { return (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, object); } public static XCellAddressable query_XCellAddressable(Object object) { return (XCellAddressable) UnoRuntime.queryInterface(XCellAddressable.class, object); } public static XComponent query_XComponent(Object object) { return (XComponent) UnoRuntime.queryInterface(XComponent.class, object); } public static XComponentContext query_XComponentContext(Object object) { return (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, object); } public static XComponentLoader query_XComponentLoader(Object object) { return (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, object); } public static XDesktop query_XDesktop(Object object) { return (XDesktop) UnoRuntime.queryInterface(XDesktop.class, object); } public static XMultiComponentFactory query_XMultiComponentFactory(Object object) { return (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, object); } public static XPropertySet query_XPropertySet(Object object) { return (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, object); } public static XSheetAnnotationAnchor query_XSheetAnnotationAnchor(Object object) { return (XSheetAnnotationAnchor) UnoRuntime.queryInterface(XSheetAnnotationAnchor.class, object); } public static XSheetAnnotationsSupplier query_XSheetAnnotationsSupplier(Object object) { return (XSheetAnnotationsSupplier) UnoRuntime.queryInterface(XSheetAnnotationsSupplier.class, object); } public static XSpreadsheet query_XSpreadsheet(Object object) { return (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, object); } public static XSpreadsheetDocument query_XSpreadsheetDocument(Object object) { return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, object); } public static XStorable2 query_XStorable2(Object object) { return (XStorable2) UnoRuntime.queryInterface(XStorable2.class, object); } }