package de.all4net.openoffice; import com.sun.star.awt.Point; import com.sun.star.awt.Size; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.bridge.XUnoUrlResolver; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.container.XNamed; import com.sun.star.drawing.XDrawPage; import com.sun.star.drawing.XDrawPageSupplier; import com.sun.star.drawing.XShape; import com.sun.star.frame.XComponentLoader; import com.sun.star.graphic.XGraphic; import com.sun.star.graphic.XGraphicProvider; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.text.XText; import com.sun.star.text.XTextContent; import com.sun.star.text.XTextDocument; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.uri.XExternalUriReferenceTranslator; import java.io.File; public class Test { public static void main(String[] args) throws Throwable { // Connect to OpenOffice.org XComponentContext localContext = Bootstrap.createInitialComponentContext(null); XMultiComponentFactory localServiceManager = localContext.getServiceManager(); Object urlResolver = localServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext ); XUnoUrlResolver unoUrlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver); Object initialObject = unoUrlResolver.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"); XPropertySet propertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, initialObject); Object context = propertySet.getPropertyValue("DefaultContext"); XComponentContext componentContext=(XComponentContext)UnoRuntime.queryInterface(XComponentContext.class, context); XMultiComponentFactory serviceManager = componentContext.getServiceManager(); // Retrieve Desktop Object desktop = serviceManager.createInstanceWithContext("com.sun.star.frame.Desktop", componentContext); // Open empty document XComponentLoader componentLoader = (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, desktop); XComponent document = componentLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new PropertyValue[] {} ); // Create a shape XMultiServiceFactory msFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, document); Object graphicObjectShape = msFactory.createInstance("com.sun.star.drawing.GraphicObjectShape"); XShape shape = (XShape)UnoRuntime.queryInterface(XShape.class, graphicObjectShape); shape.setPosition(new Point(0, 0)); XDrawPageSupplier drawPageSupplier = (XDrawPageSupplier)UnoRuntime.queryInterface(XDrawPageSupplier.class, document); XDrawPage drawPage = drawPageSupplier.getDrawPage(); drawPage.add(shape); // Set image-properties for the shape String IMAGE = "bild1.tif"; Object externalUriRT = serviceManager.createInstanceWithContext("com.sun.star.uri.ExternalUriReferenceTranslator", componentContext); XExternalUriReferenceTranslator translator = (XExternalUriReferenceTranslator)UnoRuntime .queryInterface(XExternalUriReferenceTranslator.class, externalUriRT); PropertyValue url = new PropertyValue(); url.Name = "URL"; url.Value = translator.translateToInternal(new File(IMAGE).toURL().toExternalForm()); PropertyValue[] mediaProperties = new PropertyValue[] { url }; Object graphicProviderObject = serviceManager.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", componentContext); XGraphicProvider graphicProvider = (XGraphicProvider)UnoRuntime.queryInterface(XGraphicProvider.class, graphicProviderObject); // get image ... XGraphic graphic = graphicProvider.queryGraphic(mediaProperties); // ... set shape ... propertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, graphicObjectShape); propertySet.setPropertyValue("Graphic", graphic); // ... and remember graphic-URL String graphicURL = (String)propertySet.getPropertyValue("GraphicURL"); // Insert first graphic into document XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, document); XText text = xTextDocument.getText(); Object graphicObject = msFactory.createInstance("com.sun.star.text.GraphicObject"); propertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, graphicObject); propertySet.setPropertyValue("GraphicURL", graphicURL); text.insertTextContent(text.getStart() ,(XTextContent)UnoRuntime.queryInterface(XTextContent.class, graphicObject) ,false ); // Write name and width of the new graphic to console String graphicName = ((XNamed)UnoRuntime.queryInterface(XNamed.class, graphicObject)).getName(); int graphicWidth = ((Size)propertySet.getPropertyValue("ActualSize")).Width; System.out.println("inserted graphic[" + graphicName + "], width:" + graphicWidth); // Insert second graphic from same File graphicObject = msFactory.createInstance("com.sun.star.text.GraphicObject"); propertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, graphicObject); propertySet.setPropertyValue("GraphicURL", graphicURL); text.insertTextContent(text.getStart() ,(XTextContent)UnoRuntime.queryInterface(XTextContent.class, graphicObject) ,false ); // Write name and width of the second new graphic to console // You will see, that width is now 0. You must save, close and open the document, // then you can get the width of the second graphic. graphicName = ((XNamed)UnoRuntime.queryInterface(XNamed.class, graphicObject)).getName(); propertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, graphicObject); graphicWidth = ((Size)propertySet.getPropertyValue("ActualSize")).Width; System.out.println("inserted graphic[" + graphicName + "], width:" + graphicWidth); System.exit(0); } }